change StoredFile.create interface
authorLunar <lunar@anargeek.net>
Sat, 17 Mar 2012 10:26:17 +0000 (11:26 +0100)
committerLunar <lunar@anargeek.net>
Fri, 23 Mar 2012 17:11:28 +0000 (18:11 +0100)
What matters is actually how the caller will feed the file content, and not
where or how the StoredFile is written.

lib/coquelicot/app.rb
lib/coquelicot/depot.rb
lib/coquelicot/stored_file.rb
tools/migrate_jyraphe.rb

index 8a23593..961c5b4 100644 (file)
@@ -143,14 +143,15 @@ module Coquelicot
         pass = params[:file_key]
       end
       src = params[:file][:tempfile]
+      src.rewind
       link = Coquelicot.depot.add_file(
-         src, pass,
+         pass,
          { "Expire-at" => expire_at.to_i,
            "One-time-only" => one_time_only,
            "Filename" => params[:file][:filename],
            "Length" => src.stat.size,
            "Content-Type" => params[:file][:type],
-         })
+         }) { src.eof? ? nil : src.read }
       redirect to("/ready/#{link}-#{pass}") if params[:file_key].nil? or params[:file_key].empty?
       redirect to("/ready/#{link}")
     end
index 2acc9b1..649e7cb 100644 (file)
@@ -25,16 +25,14 @@ module Coquelicot
       @path = path
     end
 
-    def add_file(src, pass, options)
+    def add_file(pass, options, &block)
       dst = nil
       lockfile.lock do
         dst = gen_random_file_name
         File.open(full_path(dst), 'w').close
       end
       begin
-        File.open(full_path(dst), 'w') do |dest|
-          StoredFile.create(src, pass, options) { |data| dest.write data }
-        end
+        StoredFile.create(full_path(dst), pass, options, &block)
       rescue
         File.unlink full_path(dst)
         raise
index 7e2656e..c1e34b8 100644 (file)
@@ -43,22 +43,24 @@ module Coquelicot
       @meta['One-time-only'] == 'true'
     end
 
-    def self.create(src, pass, meta)
+    def self.create(dest, pass, meta)
       salt = gen_salt
       clear_meta = { "Coquelicot" => COQUELICOT_VERSION,
                      "Salt" => Base64.encode64(salt).strip,
                      "Expire-at" => meta.delete('Expire-at'),
                    }
-      yield YAML.dump(clear_meta) + YAML_START
-
-      cipher = get_cipher(pass, salt, :encrypt)
-      yield cipher.update(YAML.dump(meta.merge("Created-at" => Time.now.to_i)) +
-                          YAML_START)
-      src.rewind
-      while not (buf = src.read(BUFFER_LEN)).nil?
-        yield cipher.update(buf)
+      File.open(dest, 'w') do |dest|
+        dest.write(YAML.dump(clear_meta) + YAML_START)
+
+        cipher = get_cipher(pass, salt, :encrypt)
+        dest.write(cipher.update(
+            YAML.dump(meta.merge("Created-at" => Time.now.to_i)) +
+            YAML_START))
+        while not (buf = yield).nil?
+          dest.write(cipher.update(buf))
+        end
+        dest.write(cipher.final)
       end
-      yield cipher.final
     end
 
     def empty!
index e1eda71..ef6ab73 100755 (executable)
@@ -52,13 +52,13 @@ class JyrapheMigrator
         begin
           coquelicot_link = File.open("#{@var}/files/#{filename}") do |src|
             Coquelicot::depot.add_file(
-              src, file_key || random_pass,
+              file_key || random_pass,
               { "Expire-at" => expire_at,
                 "One-time-only" => one_time_only,
                 "Filename" => filename,
                 "Length" => length,
                 "Content-Type" => mime_type
-              })
+              }) { src.eof? ? nil : src.read }
           end
           @redirects[link_name] = "#{coquelicot_link}"
           @redirects[link_name] << "-#{random_pass}" if file_key.empty?