ticket_articles_controller.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class TicketArticlesController < ApplicationController
  3. before_action :authentication_check
  4. # GET /articles
  5. def index
  6. return if deny_if_not_role(Z_ROLENAME_ADMIN)
  7. model_index_render(Ticket::Article, params)
  8. end
  9. # GET /articles/1
  10. def show
  11. # permission check
  12. article = Ticket::Article.find(params[:id])
  13. return if !article_permission(article)
  14. if params[:expand]
  15. result = article.attributes_with_relation_names
  16. # add attachments
  17. result[:attachments] = article.attachments
  18. render json: result, status: :ok
  19. return
  20. end
  21. if params[:full]
  22. full = Ticket::Article.full(params[:id])
  23. render json: full
  24. return
  25. end
  26. render json: article
  27. end
  28. # POST /articles
  29. def create
  30. form_id = params[:form_id]
  31. clean_params = Ticket::Article.param_association_lookup(params)
  32. clean_params = Ticket::Article.param_cleanup(clean_params, true)
  33. article = Ticket::Article.new(clean_params)
  34. # permission check
  35. return if !article_permission(article)
  36. # find attachments in upload cache
  37. if form_id
  38. article.attachments = Store.list(
  39. object: 'UploadCache',
  40. o_id: form_id,
  41. )
  42. end
  43. if article.save
  44. # remove attachments from upload cache
  45. Store.remove(
  46. object: 'UploadCache',
  47. o_id: form_id,
  48. )
  49. render json: article, status: :created
  50. else
  51. render json: article.errors, status: :unprocessable_entity
  52. end
  53. end
  54. # PUT /articles/1
  55. def update
  56. # permission check
  57. article = Ticket::Article.find(params[:id])
  58. return if !article_permission(article)
  59. clean_params = Ticket::Article.param_association_lookup(params)
  60. clean_params = Ticket::Article.param_cleanup(clean_params, true)
  61. if article.update_attributes(clean_params)
  62. render json: article, status: :ok
  63. else
  64. render json: article.errors, status: :unprocessable_entity
  65. end
  66. end
  67. # DELETE /articles/1
  68. def destroy
  69. article = Ticket::Article.find(params[:id])
  70. return if !article_permission(article)
  71. article.destroy
  72. head :ok
  73. end
  74. # DELETE /ticket_attachment_upload
  75. def ticket_attachment_upload_delete
  76. if params[:store_id]
  77. Store.remove_item(params[:store_id])
  78. render json: {
  79. success: true,
  80. }
  81. return
  82. elsif params[:form_id]
  83. Store.remove(
  84. object: 'UploadCache',
  85. o_id: params[:form_id],
  86. )
  87. render json: {
  88. success: true,
  89. }
  90. return
  91. end
  92. render json: { message: 'No such store_id or form_id!' }, status: :unprocessable_entity
  93. end
  94. # POST /ticket_attachment_upload
  95. def ticket_attachment_upload_add
  96. # store file
  97. file = params[:File]
  98. content_type = file.content_type
  99. if !content_type || content_type == 'application/octet-stream'
  100. content_type = if MIME::Types.type_for(file.original_filename).first
  101. MIME::Types.type_for(file.original_filename).first.content_type
  102. else
  103. 'application/octet-stream'
  104. end
  105. end
  106. headers_store = {
  107. 'Content-Type' => content_type
  108. }
  109. store = Store.add(
  110. object: 'UploadCache',
  111. o_id: params[:form_id],
  112. data: file.read,
  113. filename: file.original_filename,
  114. preferences: headers_store
  115. )
  116. # return result
  117. render json: {
  118. success: true,
  119. data: {
  120. store_id: store.id,
  121. filename: file.original_filename,
  122. size: store.size,
  123. }
  124. }
  125. end
  126. # GET /ticket_attachment/:ticket_id/:article_id/:id
  127. def attachment
  128. # permission check
  129. ticket = Ticket.lookup(id: params[:ticket_id])
  130. if !ticket_permission(ticket)
  131. render json: 'No such ticket.', status: :unauthorized
  132. return
  133. end
  134. article = Ticket::Article.find(params[:article_id])
  135. if ticket.id != article.ticket_id
  136. render json: 'No access, article_id/ticket_id is not matching.', status: :unauthorized
  137. return
  138. end
  139. list = article.attachments || []
  140. access = false
  141. list.each {|item|
  142. if item.id.to_i == params[:id].to_i
  143. access = true
  144. end
  145. }
  146. if !access
  147. render json: 'Requested file id is not linked with article_id.', status: :unauthorized
  148. return
  149. end
  150. # find file
  151. file = Store.find(params[:id])
  152. send_data(
  153. file.content,
  154. filename: file.filename,
  155. type: file.preferences['Content-Type'] || file.preferences['Mime-Type'],
  156. disposition: 'inline'
  157. )
  158. end
  159. # GET /ticket_article_plain/1
  160. def article_plain
  161. # permission check
  162. article = Ticket::Article.find(params[:id])
  163. return if !article_permission(article)
  164. list = Store.list(
  165. object: 'Ticket::Article::Mail',
  166. o_id: params[:id],
  167. )
  168. # find file
  169. return if !list
  170. file = Store.find(list.first)
  171. send_data(
  172. file.content,
  173. filename: file.filename,
  174. type: 'message/rfc822',
  175. disposition: 'inline'
  176. )
  177. end
  178. end