ticket_articles_controller.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class TicketArticlesController < ApplicationController
  3. before_filter :authentication_check
  4. # GET /articles
  5. def index
  6. @articles = Ticket::Article.all
  7. render :json => @articles
  8. end
  9. # GET /articles/1
  10. def show
  11. @article = Ticket::Article.find( params[:id] )
  12. render :json => @article
  13. end
  14. # POST /articles
  15. def create
  16. form_id = params[:ticket_article][:form_id]
  17. params[:ticket_article].delete(:form_id)
  18. @article = Ticket::Article.new( Ticket::Article.param_validation( params[:ticket_article] ) )
  19. # find attachments in upload cache
  20. if form_id
  21. @article.attachments = Store.list(
  22. :object => 'UploadCache',
  23. :o_id => form_id,
  24. )
  25. end
  26. if @article.save
  27. # remove attachments from upload cache
  28. Store.remove(
  29. :object => 'UploadCache',
  30. :o_id => form_id,
  31. )
  32. render :json => @article, :status => :created
  33. else
  34. render :json => @article.errors, :status => :unprocessable_entity
  35. end
  36. end
  37. # PUT /articles/1
  38. def update
  39. @article = Ticket::Article.find( params[:id] )
  40. if @article.update_attributes( Ticket::Article.param_validation( params[:ticket_article] ) )
  41. render :json => @article, :status => :ok
  42. else
  43. render :json => @article.errors, :status => :unprocessable_entity
  44. end
  45. end
  46. # DELETE /articles/1
  47. def destroy
  48. @article = Ticket::Article.find( params[:id] )
  49. @article.destroy
  50. head :ok
  51. end
  52. # POST /ticket_attachment/new
  53. def attachment_new
  54. # store file
  55. # content_type = request.content_type
  56. content_type = request[:content_type]
  57. puts 'content_type: ' + content_type.inspect
  58. if !content_type || content_type == 'application/octet-stream'
  59. if MIME::Types.type_for(params[:qqfile]).first
  60. content_type = MIME::Types.type_for(params[:qqfile]).first.content_type
  61. else
  62. content_type = 'application/octet-stream'
  63. end
  64. end
  65. headers_store = {
  66. 'Content-Type' => content_type
  67. }
  68. Store.add(
  69. :object => 'UploadCache',
  70. :o_id => params[:form_id],
  71. :data => request.body.read,
  72. :filename => params[:qqfile],
  73. :preferences => headers_store
  74. )
  75. # return result
  76. render :json => {
  77. :success => true,
  78. }
  79. end
  80. # GET /ticket_attachment/1
  81. def attachment
  82. # permissin check
  83. ticket = Ticket.find( params[:ticket_id] )
  84. if !ticket_permission(ticket)
  85. render( :json => 'No such ticket.', :status => :unauthorized )
  86. return
  87. end
  88. article = Ticket::Article.find( params[:article_id] )
  89. if ticket.id != article.ticket_id
  90. render( :json => 'No access, article_id/ticket_id is not matching.', :status => :unauthorized )
  91. return
  92. end
  93. list = article.attachments || []
  94. access = false
  95. list.each {|item|
  96. if item.id.to_i == params[:id].to_i
  97. access = true
  98. end
  99. }
  100. if !access
  101. render( :json => 'Requested file id is not linked with article_id.', :status => :unauthorized )
  102. return
  103. end
  104. # find file
  105. file = Store.find(params[:id])
  106. send_data(
  107. file.content,
  108. :filename => file.filename,
  109. :type => file.preferences['Content-Type'] || file.preferences['Mime-Type'],
  110. :disposition => 'inline'
  111. )
  112. end
  113. # GET /ticket_article_plain/1
  114. def article_plain
  115. # permissin check
  116. article = Ticket::Article.find( params[:id] )
  117. return if !ticket_permission( article.ticket )
  118. list = Store.list(
  119. :object => 'Ticket::Article::Mail',
  120. :o_id => params[:id],
  121. )
  122. # find file
  123. if list
  124. file = Store.find(list.first)
  125. send_data(
  126. file.content,
  127. :filename => file.filename,
  128. :type => 'message/rfc822',
  129. :disposition => 'inline'
  130. )
  131. end
  132. end
  133. end