gitlab-org--gitlab-foss/app/controllers/projects/uploads_controller.rb

56 lines
1.3 KiB
Ruby
Raw Normal View History

class Projects::UploadsController < Projects::ApplicationController
2016-03-20 16:03:53 -04:00
skip_before_action :reject_blocked!, :project,
:repository, if: -> { action_name == 'show' && image? }
2016-03-21 19:09:20 -04:00
before_action :authorize_upload_file!, only: [:create]
2016-03-20 16:03:53 -04:00
2015-02-16 13:58:40 -05:00
def create
2015-02-20 09:56:12 -05:00
link_to_file = ::Projects::UploadService.new(project, params[:file]).
2015-02-16 13:58:40 -05:00
execute
respond_to do |format|
if link_to_file
format.json do
render json: { link: link_to_file }
end
else
format.json do
render json: 'Invalid file.', status: :unprocessable_entity
end
end
end
end
def show
2015-10-09 13:07:29 -04:00
return render_404 if uploader.nil? || !uploader.file.exists?
disposition = uploader.image? ? 'inline' : 'attachment'
send_file uploader.file.path, disposition: disposition
end
2016-03-20 16:03:53 -04:00
private
2015-04-17 05:33:53 -04:00
def uploader
return @uploader if defined?(@uploader)
namespace = params[:namespace_id]
id = params[:project_id]
2015-02-16 13:58:40 -05:00
file_project = Project.find_with_namespace("#{namespace}/#{id}")
2015-04-17 05:33:53 -04:00
if file_project.nil?
@uploader = nil
2015-04-17 05:33:53 -04:00
return
end
2015-04-17 05:33:53 -04:00
@uploader = FileUploader.new(file_project, params[:secret])
@uploader.retrieve_from_store!(params[:filename])
2015-04-17 05:33:53 -04:00
@uploader
end
2015-04-17 05:33:53 -04:00
def image?
uploader && uploader.file.exists? && uploader.image?
end
2015-02-20 08:39:35 -05:00
end