GitLab steht Mittwoch, den 23. September, zwischen 10:00 und 12:00 Uhr aufgrund von Wartungsarbeiten nicht zur Verfügung.

uploads_controller.rb 1.31 KB
Newer Older
1
class Projects::UploadsController < Projects::ApplicationController
Douwe Maan's avatar
Douwe Maan committed
2
  skip_before_action :reject_blocked!, :project,
3
    :repository, if: -> { action_name == 'show' && image_or_video? }
4

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

Douwe Maan's avatar
Douwe Maan committed
7
  def create
Douwe Maan's avatar
Douwe Maan committed
8
    link_to_file = ::Projects::UploadService.new(project, params[:file]).
Douwe Maan's avatar
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
    disposition = uploader.image_or_video? ? 'inline' : 'attachment'
28 29 30
    send_file uploader.file.path, disposition: disposition
  end

Douwe Maan's avatar
Douwe Maan committed
31 32
  private

Douwe Maan's avatar
Douwe Maan committed
33 34 35
  def uploader
    return @uploader if defined?(@uploader)

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

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

Douwe Maan's avatar
Douwe Maan committed
41
    if file_project.nil?
42
      @uploader = nil
Douwe Maan's avatar
Douwe Maan committed
43 44
      return
    end
45

Douwe Maan's avatar
Douwe Maan committed
46 47
    @uploader = FileUploader.new(file_project, params[:secret])
    @uploader.retrieve_from_store!(params[:filename])
48

Douwe Maan's avatar
Douwe Maan committed
49
    @uploader
50
  end
51

52 53
  def image_or_video?
    uploader && uploader.file.exists? && uploader.image_or_video?
54
  end
Douwe Maan's avatar
Douwe Maan committed
55
end