Sha256: 4ad92fc5e7c3a480cb7eb596ca253d013bf992a8448af7e0cbeabb9b1a9151cc

Contents?: true

Size: 1.44 KB

Versions: 1

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!
        unless 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
      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|
  String === target && target =~ Paperclip::Tus::Adapter::REGEXP
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
paperclip-tus-0.1.0 lib/paperclip/tus/adapter.rb