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

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

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

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

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

Douwe Maan's avatar
Douwe Maan committed
29 30 31
  def uploader
    return @uploader if defined?(@uploader)

32 33
    namespace = params[:namespace_id]
    id = params[:project_id]
Douwe Maan's avatar
Douwe Maan committed
34

35
    file_project = Project.find_with_namespace("#{namespace}/#{id}")
36

Douwe Maan's avatar
Douwe Maan committed
37
    if file_project.nil?
38
      @uploader = nil
Douwe Maan's avatar
Douwe Maan committed
39 40
      return
    end
41

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

Douwe Maan's avatar
Douwe Maan committed
45
    @uploader
46
  end
47

Douwe Maan's avatar
Douwe Maan committed
48 49
  def image?
    uploader && uploader.file.exists? && uploader.image?
50
  end
Douwe Maan's avatar
Douwe Maan committed
51
end