Sha256: 3de518d4eb8a0138c11ab285f51c6411a1fdd9076586971b98394648bd70b936

Contents?: true

Size: 1.42 KB

Versions: 22

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module Shimmer
  class FileProxy
    attr_reader :blob_id, :resize

    delegate :message_verifier, to: :class
    delegate :content_type, :filename, to: :blob

    class << self
      def restore(id)
        blob_id, resize = message_verifier.verified(id)
        new blob_id: blob_id, resize: resize
      end
    end

    def initialize(blob_id:, resize: nil, width: nil, height: nil)
      @blob_id = blob_id
      if !resize && width
        resize = if height
          "#{width}x#{height}>"
        else
          "#{width}x"
        end
      end
      @resize = resize
    end

    class << self
      def message_verifier
        @message_verifier ||= ApplicationRecord.signed_id_verifier
      end
    end

    def path
      Rails.application.routes.url_helpers.file_path(id, locale: nil)
    end

    def url(protocol: Rails.env.production? ? :https : :http)
      Rails.application.routes.url_helpers.file_url(id, locale: nil, protocol: protocol)
    end

    def blob
      @blob ||= ActiveStorage::Blob.find(blob_id)
    end

    def resizeable
      resize.present? && blob.content_type.exclude?("svg")
    end

    def variant
      @variantvariant ||= resizeable ? blob.representation(resize: resize).processed : blob
    end

    def file
      @file ||= blob.service.download(variant.key)
    end

    private

    def id
      @id ||= message_verifier.generate([blob_id, resize])
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
shimmer-0.0.32 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.31 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.30 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.29 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.28 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.27 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.26 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.25 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.24 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.23 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.22 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.21 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.20 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.19 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.18 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.17 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.16 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.15 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.14 lib/shimmer/utils/file_proxy.rb
shimmer-0.0.13 lib/shimmer/utils/file_proxy.rb