Sha256: ad89f3d658c103e090feb7c61082418d040f324fa243298e76355bd9a3e42e37

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

require 'paperclip'
require 'tus-server'

module Paperclip
  module Tus
    class Adapter < Paperclip::AbstractAdapter
      REGEXP = /\A[\da-f]{32}\Z/

      def initialize(target)
        ensure_tus_filesystem_storage!

        @uid = target
        @file_path = tus_file_path.to_s
        @info = tus_info
        cache_current_values
      end

      private

      def cache_current_values
        self.original_filename = @info.metadata['filename']
        @tempfile = copy_to_tempfile(@file_path)
        @content_type = Paperclip::ContentTypeDetector.new(@file_path).detect
        @size = @info.length
      end

      def tus_info
        ::Tus::Info.new(tus_storage.read_info(@uid))
      end

      def tus_file_path
        # FIXME: private method
        tus_storage.send(:file_path, @uid)
      end

      def tus_storage
        ::Tus::Server.opts[:storage]
      end

      def ensure_tus_filesystem_storage!
        return if tus_storage.class == ::Tus::Storage::Filesystem

        raise 'Paperclip tus adapter does not support ' \
          "#{tus_storage.class.name}! Please set Tus::Server.opts[:storage] " \
          'to Tus::Storage::Filesystem.new(cache_directory)'
      end

      def copy_to_tempfile(src_path)
        FileUtils.cp(src_path, destination.path)
        destination
      end
    end
  end
end

Paperclip.io_adapters.register Paperclip::Tus::Adapter do |target|
  target.is_a?(String) && target =~ Paperclip::Tus::Adapter::REGEXP
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
paperclip-tus-0.2.0 lib/paperclip/tus/adapter.rb
paperclip-tus-0.1.1 lib/paperclip/tus/adapter.rb