attachments_controller.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class AttachmentsController < ApplicationController
  3. include CalendarPreview
  4. prepend_before_action :authorize!, only: %i[show destroy]
  5. prepend_before_action :authentication_check, except: %i[show destroy]
  6. prepend_before_action :authentication_check_only, only: %i[show destroy]
  7. def show
  8. return render_calendar_preview if params[:preview].present? && params[:type] == 'calendar'
  9. view_type = params[:preview] ? 'preview' : nil
  10. send_data(
  11. download_file.content(view_type),
  12. filename: download_file.filename,
  13. type: download_file.content_type,
  14. disposition: download_file.disposition
  15. )
  16. end
  17. def create
  18. file = params[:File]
  19. content_type = file.content_type
  20. if !content_type || content_type == 'application/octet-stream'
  21. content_type = if MIME::Types.type_for(file.original_filename).first
  22. MIME::Types.type_for(file.original_filename).first.content_type
  23. else
  24. 'application/octet-stream'
  25. end
  26. end
  27. headers_store = {
  28. 'Content-Type' => content_type
  29. }
  30. store = Store.create!(
  31. object: 'UploadCache',
  32. o_id: params[:form_id],
  33. data: file.read,
  34. filename: file.original_filename,
  35. preferences: headers_store
  36. )
  37. render json: {
  38. success: true,
  39. data: {
  40. id: store.id,
  41. filename: file.original_filename,
  42. size: store.size,
  43. }
  44. }
  45. end
  46. def destroy
  47. Store.remove_item(download_file.id)
  48. render json: {
  49. success: true,
  50. }
  51. end
  52. def destroy_form
  53. Store.remove(
  54. object: 'UploadCache',
  55. o_id: params[:form_id],
  56. )
  57. render json: {
  58. success: true,
  59. }
  60. end
  61. private
  62. def render_calendar_preview
  63. data = parse_calendar(download_file)
  64. render json: data, status: :ok
  65. rescue => e
  66. logger.error e
  67. render json: { error: e.message }, status: :unprocessable_entity
  68. end
  69. def user_not_authorized(e)
  70. not_found(e)
  71. end
  72. end