Sha256: c1eab9596ee3419c9b32316b8beafade33aae97ef42266209673e6471a38bd1d

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'mime/types'
require 'net/http'
require 'cgi'

module AssetHostCore
  module Loaders
    class URL < Base
      SOURCE = "URL"

      #-----------

      def self.build_from_url(url)
        uri = URI.parse(url)
        return nil unless uri.is_a?(URI::HTTP)

        response  = Net::HTTP.get_response(uri)

        # Check that it's actually an image we're grabbing
        if response.content_type.match(/image/)
          self.new(url: url, id: url)
        else
          nil
        end
      end

      #----------

      def load
        filename = File.basename(@url)

        # build asset
        asset = AssetHostCore::Asset.new(
          :title    => filename,
          :url      => @url,
          :notes    => "Fetched from URL: #{@url}",
          :image    => image_file
        )

        asset.save!
        asset
      end

      #----------

      private

      def image_file
        @image_file ||= begin
          response = open(@url)

          tempfile = Tempfile.new('ah-brightcove', encoding: "ascii-8bit")
          tempfile.write response.read

          tempfile
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
asset_host_core-2.0.0.beta lib/asset_host_core/loaders/url.rb