ticket_articles_controller.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. @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. # DELETE /ticket_attachment_upload
  53. def ticket_attachment_upload_delete
  54. if params[:store_id]
  55. Store.remove_item(params[:store_id])
  56. render json: {
  57. success: true,
  58. }
  59. return
  60. elsif params[:form_id]
  61. Store.remove(
  62. object: 'UploadCache',
  63. o_id: params[:form_id],
  64. )
  65. render json: {
  66. success: true,
  67. }
  68. return
  69. end
  70. render json: { message: 'No such store_id or form_id!' }, status: :unprocessable_entity
  71. end
  72. # POST /ticket_attachment_upload
  73. def ticket_attachment_upload_add
  74. # store file
  75. file = params[:File]
  76. content_type = file.content_type
  77. if !content_type || content_type == 'application/octet-stream'
  78. content_type = if MIME::Types.type_for(file.original_filename).first
  79. MIME::Types.type_for(file.original_filename).first.content_type
  80. else
  81. 'application/octet-stream'
  82. end
  83. end
  84. headers_store = {
  85. 'Content-Type' => content_type
  86. }
  87. store = Store.add(
  88. object: 'UploadCache',
  89. o_id: params[:form_id],
  90. data: file.read,
  91. filename: file.original_filename,
  92. preferences: headers_store
  93. )
  94. # return result
  95. render json: {
  96. success: true,
  97. data: {
  98. store_id: store.id,
  99. filename: file.original_filename,
  100. size: store.size,
  101. }
  102. }
  103. end
  104. # GET /ticket_attachment/1
  105. def attachment
  106. # permission check
  107. ticket = Ticket.find( params[:ticket_id] )
  108. if !ticket_permission(ticket)
  109. render( json: 'No such ticket.', status: :unauthorized )
  110. return
  111. end
  112. article = Ticket::Article.find( params[:article_id] )
  113. if ticket.id != article.ticket_id
  114. render( json: 'No access, article_id/ticket_id is not matching.', status: :unauthorized )
  115. return
  116. end
  117. list = article.attachments || []
  118. access = false
  119. list.each {|item|
  120. if item.id.to_i == params[:id].to_i
  121. access = true
  122. end
  123. }
  124. if !access
  125. render( json: 'Requested file id is not linked with article_id.', status: :unauthorized )
  126. return
  127. end
  128. # find file
  129. file = Store.find(params[:id])
  130. send_data(
  131. file.content,
  132. filename: file.filename,
  133. type: file.preferences['Content-Type'] || file.preferences['Mime-Type'],
  134. disposition: 'inline'
  135. )
  136. end
  137. # GET /ticket_article_plain/1
  138. def article_plain
  139. # permission check
  140. article = Ticket::Article.find( params[:id] )
  141. return if !ticket_permission( article.ticket )
  142. list = Store.list(
  143. object: 'Ticket::Article::Mail',
  144. o_id: params[:id],
  145. )
  146. # find file
  147. return if !list
  148. file = Store.find(list.first)
  149. send_data(
  150. file.content,
  151. filename: file.filename,
  152. type: 'message/rfc822',
  153. disposition: 'inline'
  154. )
  155. end
  156. end