2015-04-26 12:48:37 +05:30
|
|
|
class Projects::UploadsController < Projects::ApplicationController
|
2016-06-02 11:05:42 +05:30
|
|
|
skip_before_action :reject_blocked!, :project,
|
2016-08-24 12:49:21 +05:30
|
|
|
:repository, if: -> { action_name == 'show' && image_or_video? }
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
before_action :authorize_upload_file!, only: [:create]
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def create
|
|
|
|
link_to_file = ::Projects::UploadService.new(project, params[:file]).
|
|
|
|
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-24 18:46:33 +05:30
|
|
|
return render_404 if uploader.nil? || !uploader.file.exists?
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
disposition = uploader.image_or_video? ? 'inline' : 'attachment'
|
2015-04-26 12:48:37 +05:30
|
|
|
send_file uploader.file.path, disposition: disposition
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
private
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def uploader
|
|
|
|
return @uploader if defined?(@uploader)
|
|
|
|
|
|
|
|
namespace = params[:namespace_id]
|
|
|
|
id = params[:project_id]
|
|
|
|
|
|
|
|
file_project = Project.find_with_namespace("#{namespace}/#{id}")
|
|
|
|
|
|
|
|
if file_project.nil?
|
2015-09-11 14:41:01 +05:30
|
|
|
@uploader = nil
|
2015-04-26 12:48:37 +05:30
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
@uploader = FileUploader.new(file_project, params[:secret])
|
|
|
|
@uploader.retrieve_from_store!(params[:filename])
|
|
|
|
|
|
|
|
@uploader
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def image_or_video?
|
|
|
|
uploader && uploader.file.exists? && uploader.image_or_video?
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|