BigW Consortium Gitlab

uploads_controller.rb 1.28 KB
Newer Older
1
class Projects::UploadsController < Projects::ApplicationController
2
  skip_before_action :reject_blocked!, :project,
3
    :repository, if: -> { action_name == 'show' && image? }
4

Douwe Maan committed
5
  before_action :authorize_upload_file!, only: [:create]
6

Douwe Maan committed
7
  def create
Douwe Maan committed
8
    link_to_file = ::Projects::UploadService.new(project, params[:file]).
Douwe Maan committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
      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

24
  def show
25
    return render_404 if uploader.nil? || !uploader.file.exists?
26 27 28 29 30

    disposition = uploader.image? ? 'inline' : 'attachment'
    send_file uploader.file.path, disposition: disposition
  end

31 32
  private

33 34 35
  def uploader
    return @uploader if defined?(@uploader)

36 37
    namespace = params[:namespace_id]
    id = params[:project_id]
Douwe Maan committed
38

39
    file_project = Project.find_with_namespace("#{namespace}/#{id}")
40

41
    if file_project.nil?
42
      @uploader = nil
43 44
      return
    end
45

46 47
    @uploader = FileUploader.new(file_project, params[:secret])
    @uploader.retrieve_from_store!(params[:filename])
48

49
    @uploader
50
  end
51

52 53
  def image?
    uploader && uploader.file.exists? && uploader.image?
54
  end
Douwe Maan committed
55
end