1
0

media_controller.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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!(updateable_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.where(status_id: nil).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 updateable_media_attachment_params
  36. params.permit(:thumbnail, :description, :focus)
  37. end
  38. def file_type_error
  39. { error: 'File type of uploaded media could not be verified' }
  40. end
  41. def processing_error
  42. { error: 'Error processing thumbnail for uploaded media' }
  43. end
  44. end