ticket_articles_controller.rb 4.0 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. @article = Ticket::Article.new(params[:ticket_article])
  16. @article.created_by_id = current_user.id
  17. @article.updated_by_id = current_user.id
  18. # find attachments in upload cache
  19. @article['attachments'] = Store.list(
  20. :object => 'UploadCache::TicketZoom::' + current_user.id.to_s,
  21. :o_id => @article.ticket_id
  22. )
  23. if @article.save
  24. # remove attachments from upload cache
  25. Store.remove(
  26. :object => 'UploadCache::TicketZoom::' + current_user.id.to_s,
  27. :o_id => @article.ticket_id
  28. )
  29. render :json => @article, :status => :created
  30. else
  31. render :json => @article.errors, :status => :unprocessable_entity
  32. end
  33. end
  34. # PUT /articles/1
  35. def update
  36. @article = Ticket::Article.find( params[:id] )
  37. params[:ticket_article][:updated_by_id] = current_user.id
  38. if @article.update_attributes(params[:ticket_article])
  39. render :json => @article, :status => :ok
  40. else
  41. render :json => @article.errors, :status => :unprocessable_entity
  42. end
  43. end
  44. # DELETE /articles/1
  45. def destroy
  46. @article = Ticket::Article.find( params[:id] )
  47. @article.destroy
  48. head :ok
  49. end
  50. # POST /ticket_attachment/new
  51. def attachment_new
  52. # puts '-------'
  53. # puts params.inspect
  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::' + params[:form] + '::' + current_user.id.to_s,
  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 = Store.list( :object => 'Ticket::Article', :o_id => params[:article_id] ) || []
  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.store_file.data,
  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.store_file.data,
  127. :filename => file.filename,
  128. :type => 'message/rfc822',
  129. :disposition => 'inline'
  130. )
  131. end
  132. end
  133. end