Commits

Anonymous committed bafdcb8

add ability to prefetch URIs

  • Participants
  • Parent commits fbbe7d6

Comments (0)

Files changed (2)

lib/mrt/ingest/iobject.rb

 require 'mrt/ingest'
 require 'tempfile'
 require 'uri'
+require 'open-uri'
+require 'digest/md5'
 
 module Mrt
   module Ingest
         @digest = options[:digest]
         @mime_type = options[:mime_type]
         @size = options[:size]
-        
+        @prefetch = options[:prefetch] || false
+
         case where
         when File, Tempfile
           @name = File.basename(where.path) if @name.nil?
           @size = File.size(where.path) if @size.nil?
         when URI
           @name = File.basename(where.to_s) if @name.nil?
-          @uri = where
+          if @prefetch then
+            digest = Digest::MD5.new()
+            @uri = server.add_file do |f|
+              open(where) do |u|
+                while (buff = u.read(1024)) do
+                  f << buff
+                  digest << buff
+                end
+              end
+            end
+            @digest = Mrt::Ingest::MessageDigest::MD5.new(digest.hexdigest)
+          else
+            @uri = where
+          end
         else
           raise IngestException.new("Trying to add a component that is not a File or URI")
         end

test/test_iobject.rb

 require 'mrt/ingest'
 require 'shoulda'
 require 'open-uri'
+require 'socket'
 
 class TestIObject < Test::Unit::TestCase
   def parse_object_manifest(iobject)
       @iobject.add_component(URI.parse("http://example.org/file"))
     end
 
+    should "be able to add a URI component with prefetching, served locally" do
+      @iobject.add_component(URI.parse("http://example.org/file"), :prefetch=>true)
+      manifest = parse_object_manifest(@iobject)
+      manifest.entries.each do |entry|
+        # check that all files are served locally
+        uri = URI.parse(entry.values[0])
+        assert_equal(Socket.gethostname, uri.host)
+      end
+    end
+
     should "not be able to add a non-URI component" do
       assert_raise(Mrt::Ingest::IngestException) do
         @iobject.add_component("http://example.org/file")