ticket_articles_controller.rb 3.9 KB

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