Skip to content
Snippets Groups Projects
uploads_controller.rb 1.31 KiB
Newer Older
  • Learn to ignore specific revisions
  • class Projects::UploadsController < Projects::ApplicationController
    
      skip_before_action :reject_blocked!, :project,
    
        :repository, if: -> { action_name == 'show' && image_or_video? }
    
    Douwe Maan's avatar
    Douwe Maan committed
      before_action :authorize_upload_file!, only: [:create]
    
    Douwe Maan's avatar
    Douwe Maan committed
      def create
    
    Douwe Maan's avatar
    Douwe Maan committed
        link_to_file = ::Projects::UploadService.new(project, params[:file]).
    
    Douwe Maan's avatar
    Douwe Maan committed
          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
    
    
        return render_404 if uploader.nil? || !uploader.file.exists?
    
        disposition = uploader.image_or_video? ? 'inline' : 'attachment'
    
        send_file uploader.file.path, disposition: disposition
      end
    
    
      def uploader
        return @uploader if defined?(@uploader)
    
    
        namespace = params[:namespace_id]
        id = params[:project_id]
    
    Douwe Maan's avatar
    Douwe Maan committed
    
    
        file_project = Project.find_with_namespace("#{namespace}/#{id}")
    
        if file_project.nil?
    
          @uploader = nil
    
          return
        end
    
        @uploader = FileUploader.new(file_project, params[:secret])
        @uploader.retrieve_from_store!(params[:filename])
    
        @uploader
    
      def image_or_video?
        uploader && uploader.file.exists? && uploader.image_or_video?
    
    Douwe Maan's avatar
    Douwe Maan committed
    end