Sha256: 9d41b45248b709f6d5a82a1b0d30b5ff1f47b9df38b000bfd4e4a24df11b9dba

Contents?: true

Size: 1.4 KB

Versions: 9

Compression:

Stored size: 1.4 KB

Contents

require 'securerandom'

# This handler can be used for fast, client-side upload of images (or canvas) directly to storage.
# When client is taking snapshot of image or canvas, we don't need to use PhantomJS.
module Shutterbug
  module Handlers
    class DirectUploadHandler

      def skip_direct_upload
        return true if Configuration.instance.skip_direct_upload
        return true unless Configuration.instance.use_s3?
        return false
      end

      def self.regex
        /#{Configuration.instance.path_prefix}\/img_upload_url/
      end

      # Returns put_url and get_url for a new file that should be uploaded by the client.
      # Of course get_url will work after file is uploaded.
      def handle(helper, req, env)
        if skip_direct_upload
          not_available_response(helper)
        else
          format = req.GET()['format'] || 'png'
          object_name = "img-#{SecureRandom.uuid}.#{format}"
          storage = Configuration.instance.storage
          unless storage.respond_to? :put_url
            not_available_response(helper)
          end
          helper.response({
            put_url: storage.put_url(object_name),
            get_url: storage.get_url(object_name),
          }.to_json, 'application/json')
        end
      end

      def not_available_response(helper)
        return helper.response('direct upload not available', 'text/plain', 400)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
shutterbug-0.5.9 lib/shutterbug/handlers/direct_upload_handler.rb
shutterbug-0.5.8 lib/shutterbug/handlers/direct_upload_handler.rb
shutterbug-0.5.7 lib/shutterbug/handlers/direct_upload_handler.rb
shutterbug-0.5.6 lib/shutterbug/handlers/direct_upload_handler.rb
shutterbug-0.5.5 lib/shutterbug/handlers/direct_upload_handler.rb
shutterbug-0.5.4 lib/shutterbug/handlers/direct_upload_handler.rb
shutterbug-0.5.3 lib/shutterbug/handlers/direct_upload_handler.rb
shutterbug-0.5.2 lib/shutterbug/handlers/direct_upload_handler.rb
shutterbug-0.5.1 lib/shutterbug/handlers/direct_upload_handler.rb