[Conkeror] [PATCH 2/2 (v3)] save_uri: optionally use a temporary download file

David Kettler kettler at internode.on.net
Wed Feb 18 04:01:52 PST 2009


This is useful when it is undesirable for some other tool to attempt
to open the downloaded file before it is complete.

If $temp_file is specified then the uri is downloaded to that file and
then renamed to the output_file when the download is finished.  If
$temp_file is the value true then a new temporary file in the same
directory as output_file is used.

The download manager indicates this by showing the temp_file name with
the output_file name following parenthetically.  When the download is
complete and the file is renamed, the download manager will update to
show the new name.

Note that this save_uri usage of the term "temporary" is different to
the download-manager usage; in that case the file is deleted, not
renamed.
---

This third version differs from the second in that:

  - The proper local hook is used.  This lets us update before the
    download buffer and means we don't need to remove the function
    from the hook.  It also prevents the hook firing on the wrong
    download.

  - The target file name shown in the download buffer will update
    after the rename.

  - The download manager changes are separate.

  - Correct test for temp_file===true.
---
 modules/save.js |   28 +++++++++++++++++++++++++---
 1 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/modules/save.js b/modules/save.js
index 23e7115..9ed4794 100644
--- a/modules/save.js
+++ b/modules/save.js
@@ -12,7 +12,7 @@ require("load-spec.js");
 require("suggest-file-name.js");
 
 /* buffer is used only to associate with the download */
-define_keywords("$use_cache", "$buffer", "$prepare_download");
+define_keywords("$use_cache", "$buffer", "$prepare_download", "$temp_file");
 function save_uri(lspec, output_file) {
     keywords(arguments, $use_cache = true);
 
@@ -22,6 +22,19 @@ function save_uri(lspec, output_file) {
 
     var prepare_download = arguments.$prepare_download;
 
+    var download_file = output_file;
+
+    var temp_file = arguments.$temp_file;
+    if (temp_file === true) {
+        temp_file = Cc["@mozilla.org/file/local;1"]
+            .createInstance(Ci.nsILocalFile);
+        temp_file.initWithFile(output_file);
+        temp_file.leafName = "temp";
+        temp_file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
+    }
+    if (temp_file)
+        download_file = temp_file;
+
     var cache_key = null;
     var uri = load_spec_uri(lspec);
     var referrer_uri = load_spec_referrer(lspec);
@@ -29,7 +42,7 @@ function save_uri(lspec, output_file) {
     if (use_cache)
         cache_key = load_spec_cache_key(lspec);
 
-    var file_uri = make_uri(output_file);
+    var file_uri = make_uri(download_file);
 
     var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
         .createInstance(Ci.nsIWebBrowserPersist);
@@ -44,7 +57,16 @@ function save_uri(lspec, output_file) {
     else
         persist.persistFlags |= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_BYPASS_CACHE;
 
-    var info = register_download(buffer, uri);
+    var info = register_download(buffer, uri, download_file);
+    if (temp_file) {
+        // We want this hook so that we run before the download buffer updates.
+        add_hook.call(info, "download_state_change_hook", function() {
+            if (info.state == DOWNLOAD_FINISHED) {
+                temp_file.moveTo(null, output_file.leafName);
+                info.target_file.leafName = output_file.leafName;
+            }
+        });
+    }
     if (prepare_download)
         prepare_download(info);
 
-- 
1.5.6.5



More information about the Conkeror mailing list