1
0

media_controller.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 create
  8. @media_attachment = current_account.media_attachments.create!(media_attachment_params)
  9. render json: @media_attachment, serializer: REST::MediaAttachmentSerializer
  10. rescue Paperclip::Errors::NotIdentifiedByImageMagickError
  11. render json: file_type_error, status: 422
  12. rescue Paperclip::Error
  13. render json: processing_error, status: 500
  14. end
  15. def show
  16. render json: @media_attachment, serializer: REST::MediaAttachmentSerializer, status: status_code_for_media_attachment
  17. end
  18. def update
  19. @media_attachment.update!(media_attachment_params)
  20. render json: @media_attachment, serializer: REST::MediaAttachmentSerializer, status: status_code_for_media_attachment
  21. end
  22. private
  23. def status_code_for_media_attachment
  24. @media_attachment.not_processed? ? 206 : 200
  25. end
  26. def set_media_attachment
  27. @media_attachment = current_account.media_attachments.unattached.find(params[:id])
  28. end
  29. def check_processing
  30. render json: processing_error, status: 422 if @media_attachment.processing_failed?
  31. end
  32. def media_attachment_params
  33. params.permit(:file, :thumbnail, :description, :focus)
  34. end
  35. def file_type_error
  36. { error: 'File type of uploaded media could not be verified' }
  37. end
  38. def processing_error
  39. { error: 'Error processing thumbnail for uploaded media' }
  40. end
  41. end