Module: Api::Upload
- Extended by:
- ActiveSupport::Concern
- Included in:
- ChallengesController, NeedsController, PeerReviewsController, PostsController, ProgramsController, ProjectsController, SpacesController, UsersController, WorkgroupsController
- Defined in:
- app/controllers/concerns/api/upload.rb
Instance Method Summary collapse
- #remove_avatar ⇒ Object
- #remove_banner ⇒ Object
- #remove_document ⇒ Object
- #upload_avatar ⇒ Object
- #upload_banner ⇒ Object
- #upload_document ⇒ Object
Instance Method Details
#remove_avatar ⇒ Object
82 83 84 85 86 87 |
# File 'app/controllers/concerns/api/upload.rb', line 82 def remove_avatar raise ApiExceptions::ResourceNotFound.new(message: 'No avatar attached') unless @obj.avatar.attached? @obj.avatar.purge render json: { data: 'Avatar removed' }, status: :ok end |
#remove_banner ⇒ Object
89 90 91 92 93 94 |
# File 'app/controllers/concerns/api/upload.rb', line 89 def raise ApiExceptions::ResourceNotFound.new(message: 'No banner attached') unless @obj..attached? @obj..purge render json: { data: 'Banner removed' }, status: :ok end |
#remove_document ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/controllers/concerns/api/upload.rb', line 63 def remove_document doc_id = params[:document_id] raise ApiExceptions::UnprocessableEntity.new(message: 'Missing document id') if doc_id.nil? raise ApiExceptions::ResourceNotFound.new(message: 'Object has no documents!') unless @obj.documents.attached? found = false @obj.documents.each do |document| if document.blob.id == doc_id.to_i found = true document.purge end end raise ApiExceptions::ResourceNotFound.new(message: 'Cannot find document!') unless found render json: { data: 'Document removed' }, status: :ok end |
#upload_avatar ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/controllers/concerns/api/upload.rb', line 12 def upload_avatar raise ApiExceptions::UnprocessableEntity.new(message: 'Missing avatar') if params[:avatar].nil? raise ApiExceptions::UnprocessableEntity.new(message: 'Invalid image type') unless params[:avatar] @obj.avatar.attach(params[:avatar]) raise ApiExceptions::UnprocessableEntity.new(message: 'Something went wrong') unless @obj.avatar.attached? @obj.avatar.filename = @obj.avatar.filename.to_s.gsub('(', '-').gsub(')', '-') begin @obj.avatar.save! variant = @obj.avatar.variant(resize: '200x200^') logo_url = rails_representation_url(variant) render json: { data: 'Avatar uploaded', url: logo_url }, status: :ok @obj.save! rescue render json: { data: 'Avatar failed to upload' }, status: :internal_service_error end end |
#upload_banner ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/controllers/concerns/api/upload.rb', line 33 def raise ApiExceptions::UnprocessableEntity.new(message: 'Missing banner') if params[:banner].nil? raise ApiExceptions::UnprocessableEntity.new(message: 'Invalid image type') unless params[:banner] @obj..attach(params[:banner]) raise ApiExceptions::UnprocessableEntity.new(message: 'Something went wrong') unless @obj..attached? @obj..filename = @obj..filename.to_s.gsub('(', '-').gsub(')', '-') @obj..save! render json: { data: 'Banner uploaded', url: Rails.application.routes.url_helpers.rails_blob_url(@obj.) }, status: :ok @obj.save! end |
#upload_document ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/controllers/concerns/api/upload.rb', line 47 def upload_document documents = params[:documents] raise ApiExceptions::UnprocessableEntity.new(message: 'No documents') if documents.nil? documents.each do |document| @obj.documents.attach(document) doc = @obj.documents.last doc.filename = doc.filename.to_s.gsub('(', '-').gsub(')', '-') doc.save! end raise ApiExceptions::UnprocessableEntity.new(message: 'Something went wrong') unless @obj.documents.attached? render json: { data: 'documents uploaded' }, status: :ok end |