media_controller.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # frozen_string_literal: true
  2. class Api::V1::MediaController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :write, :'write:media' }
  4. before_action :require_user!
  5. before_action :set_media_attachment, except: [:create]
  6. before_action :check_processing, except: [:create]
  7. def show
  8. render json: @media_attachment, serializer: REST::MediaAttachmentSerializer, status: status_code_for_media_attachment
  9. end
  10. def create
  11. @media_attachment = current_account.media_attachments.create!(media_attachment_params)
  12. render json: @media_attachment, serializer: REST::MediaAttachmentSerializer
  13. rescue Paperclip::Errors::NotIdentifiedByImageMagickError
  14. render json: file_type_error, status: 422
  15. rescue Paperclip::Error => e
  16. Rails.logger.error "#{e.class}: #{e.message}"
  17. render json: processing_error, status: 500
  18. end
  19. def update
  20. @media_attachment.update!(updateable_media_attachment_params)
  21. render json: @media_attachment, serializer: REST::MediaAttachmentSerializer, status: status_code_for_media_attachment
  22. end
  23. private
  24. def status_code_for_media_attachment
  25. @media_attachment.not_processed? ? 206 : 200
  26. end
  27. def set_media_attachment
  28. @media_attachment = current_account.media_attachments.where(status_id: nil).find(params[:id])
  29. end
  30. def check_processing
  31. render json: processing_error, status: 422 if @media_attachment.processing_failed?
  32. end
  33. def media_attachment_params
  34. params.permit(:file, :thumbnail, :description, :focus)
  35. end
  36. def updateable_media_attachment_params
  37. params.permit(:thumbnail, :description, :focus)
  38. end
  39. def file_type_error
  40. { error: 'File type of uploaded media could not be verified' }
  41. end
  42. def processing_error
  43. { error: 'Error processing thumbnail for uploaded media' }
  44. end
  45. end