Sha256: aa4daf4f08e609b57b8a682ae0672642f929be40f2fffb6cdc2ba4cb606aad22

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 KB

Contents

require 'sufia/single_use_error'

module Sufia
  module SingleUseLinksViewerControllerBehavior
    extend ActiveSupport::Concern
    include Sufia::DownloadsControllerBehavior
    included do
      skip_before_action :load_file, except: :download
      rescue_from Sufia::SingleUseError, with: :render_single_use_error
      rescue_from CanCan::AccessDenied, with: :render_single_use_error
      rescue_from ActiveRecord::RecordNotFound, with: :render_single_use_error
      class_attribute :presenter_class
      self.presenter_class = Sufia::GenericFilePresenter
    end

    def download
      raise not_found_exception unless single_use_link.path == sufia.download_path(id: @asset)
      send_content
    end

    def show
      raise not_found_exception unless single_use_link.path == sufia.polymorphic_path(@asset)

      # show the file
      @presenter = presenter

      # create a dowload link that is single use for the user since we do not just want to show metadata we want to access it too
      @su = single_use_link.create_for_path sufia.download_path(id: @asset)
      @download_link = sufia.download_single_use_link_path(@su.downloadKey)
    end

    protected

      def content_options
        super.tap do |options|
          options[:disposition] = 'attachment' if action_name == 'download'
        end
      end

      def presenter
        presenter_class.new(@asset)
      end

      def authorize_download!
        authorize! :read, asset
      end

      def single_use_link
        @single_use_link ||= SingleUseLink.find_by_downloadKey!(params[:id])
      end

      def not_found_exception
        Sufia::SingleUseError.new('Single-Use Link Not Found')
      end

      def asset
        @asset ||= ActiveFedora::Base.find(single_use_link.itemId)
      end

      def current_ability
        @current_ability ||= SingleUseLinksViewerController::Ability.new current_user, single_use_link
      end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sufia-6.7.0 app/controllers/concerns/sufia/single_use_links_viewer_controller_behavior.rb
sufia-6.6.1 app/controllers/concerns/sufia/single_use_links_viewer_controller_behavior.rb
sufia-6.6.0 app/controllers/concerns/sufia/single_use_links_viewer_controller_behavior.rb
sufia-6.5.0 app/controllers/concerns/sufia/single_use_links_viewer_controller_behavior.rb
sufia-6.4.0 app/controllers/concerns/sufia/single_use_links_viewer_controller_behavior.rb
sufia-6.3.0 app/controllers/concerns/sufia/single_use_links_viewer_controller_behavior.rb