2015-02-20 07:13:48 -05:00
|
|
|
class Projects::UploadsController < Projects::ApplicationController
|
2017-02-01 13:21:28 -05:00
|
|
|
skip_before_action :project, :repository,
|
|
|
|
if: -> { action_name == 'show' && image_or_video? }
|
2015-02-20 07:13:48 -05:00
|
|
|
|
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
|
|
|
|
|
2015-02-20 07:13:48 -05:00
|
|
|
def show
|
2015-10-09 13:07:29 -04:00
|
|
|
return render_404 if uploader.nil? || !uploader.file.exists?
|
2015-04-14 11:02:17 -04:00
|
|
|
|
2016-04-03 01:00:06 -04:00
|
|
|
disposition = uploader.image_or_video? ? 'inline' : 'attachment'
|
2015-04-14 11:02:17 -04:00
|
|
|
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)
|
|
|
|
|
2015-04-14 11:02:17 -04:00
|
|
|
namespace = params[:namespace_id]
|
|
|
|
id = params[:project_id]
|
2015-02-16 13:58:40 -05:00
|
|
|
|
2017-02-02 18:43:19 -05:00
|
|
|
file_project = Project.find_by_full_path("#{namespace}/#{id}")
|
2015-02-20 07:13:48 -05:00
|
|
|
|
2015-04-17 05:33:53 -04:00
|
|
|
if file_project.nil?
|
2015-04-16 08:03:37 -04:00
|
|
|
@uploader = nil
|
2015-04-17 05:33:53 -04:00
|
|
|
return
|
|
|
|
end
|
2015-04-14 11:02:17 -04:00
|
|
|
|
2015-04-17 05:33:53 -04:00
|
|
|
@uploader = FileUploader.new(file_project, params[:secret])
|
|
|
|
@uploader.retrieve_from_store!(params[:filename])
|
2015-02-20 07:13:48 -05:00
|
|
|
|
2015-04-17 05:33:53 -04:00
|
|
|
@uploader
|
2015-04-14 11:02:17 -04:00
|
|
|
end
|
2015-02-20 09:37:37 -05:00
|
|
|
|
2016-04-03 01:00:06 -04:00
|
|
|
def image_or_video?
|
|
|
|
uploader && uploader.file.exists? && uploader.image_or_video?
|
2015-02-20 07:13:48 -05:00
|
|
|
end
|
2015-02-20 08:39:35 -05:00
|
|
|
end
|