ticket_articles_controller.rb 4.1 KB

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