]> khamidou — GitHub mirrors - electron.git/commitdiff
Reimplement downloadItem.getFilename API.
authorHaojian Wu <hokein.wu@gmail.com>
Thu, 24 Sep 2015 08:31:41 +0000 (16:31 +0800)
committerHaojian Wu <hokein.wu@gmail.com>
Thu, 24 Sep 2015 08:39:02 +0000 (16:39 +0800)
Previously, the suggested file name(Always 'empty') returned by
'download_item->GetSuggestedFilename' is not the same with the default one saved
in local disk.

The patch reimplement this API allowing it to return the default file name, which
is more expected from user.

atom/browser/api/atom_api_download_item.cc
atom/browser/api/atom_api_download_item.h
atom/browser/api/lib/app.coffee
docs/api/download-item.md
spec/api-session-spec.coffee
spec/static/main.js

index 3d150462bde7c4b199391e84b161962d2bbd6663..6b3dfbf3f7db132f4537f6a03dabf55b923dd2e8 100644 (file)
@@ -11,7 +11,9 @@
 #include "atom/common/native_mate_converters/gurl_converter.h"
 #include "atom/common/node_includes.h"
 #include "base/memory/linked_ptr.h"
+#include "base/strings/utf_string_conversions.h"
 #include "native_mate/dictionary.h"
+#include "net/base/filename_util.h"
 
 namespace mate {
 
@@ -112,8 +114,13 @@ bool DownloadItem::HasUserGesture() {
   return download_item_->HasUserGesture();
 }
 
-std::string DownloadItem::GetSuggestedFilename() {
-  return download_item_->GetSuggestedFilename();
+std::string DownloadItem::GetFilename() {
+  return base::UTF16ToUTF8(net::GenerateFileName(GetUrl(),
+                           GetContentDisposition(),
+                           std::string(),
+                           download_item_->GetSuggestedFilename(),
+                           GetMimeType(),
+                           std::string()).LossyDisplayName());
 }
 
 std::string DownloadItem::GetContentDisposition() {
@@ -147,7 +154,7 @@ mate::ObjectTemplateBuilder DownloadItem::GetObjectTemplateBuilder(
       .SetMethod("getUrl", &DownloadItem::GetUrl)
       .SetMethod("getMimeType", &DownloadItem::GetMimeType)
       .SetMethod("hasUserGesture", &DownloadItem::HasUserGesture)
-      .SetMethod("getSuggestedFilename", &DownloadItem::GetSuggestedFilename)
+      .SetMethod("getFilename", &DownloadItem::GetFilename)
       .SetMethod("getContentDisposition", &DownloadItem::GetContentDisposition)
       .SetMethod("setSavePath", &DownloadItem::SetSavePath);
 }
index 3e90ced8a81190cfbbc7ee417406c16e1ca7e25e..e7b30dd13aa2a14fbc1cb9c43e145dd21f4da063 100644 (file)
@@ -44,7 +44,7 @@ class DownloadItem : public mate::EventEmitter,
   int64 GetTotalBytes();
   std::string GetMimeType();
   bool HasUserGesture();
-  std::string GetSuggestedFilename();
+  std::string GetFilename();
   std::string GetContentDisposition();
   const GURL& GetUrl();
   void SetSavePath(const base::FilePath& path);
index 48ddd1768e8e2fc4885441f90cfa4046a0eb447f..18c80dc2b1f4701fbd5057e2de3d9b5b10cc6008 100644 (file)
@@ -16,7 +16,7 @@ wrapDownloadItem = (download_item) ->
   download_item.__proto__ = EventEmitter.prototype
   # Be compatible with old APIs.
   download_item.url = download_item.getUrl()
-  download_item.filename = download_item.getSuggestedFilename()
+  download_item.filename = download_item.getFilename()
   download_item.mimeType = download_item.getMimeType()
   download_item.hasUserGesture = download_item.hasUserGesture()
 
index d18433c3fe273d7ff3c9a4b3f4bec1bd2dcc8775..722dc5d8cf78b7413dfda5c5f43c2baff3d0e198 100644 (file)
@@ -10,10 +10,10 @@ win.webContents.session.on('will-download', function(event, item, webContents) {
   // Set the save path, making Electron not to prompt a save dialog.
   item.setSavePath('/tmp/save.pdf');
   console.log(item.getMimeType());
-  console.log(item.getSuggestedFilename());
+  console.log(item.getFilename());
   console.log(item.getTotalBytes());
   item.on('updated', function() {
-    console.log('Recived bytes: ' + item.getReceivedBytes());
+    console.log('Received bytes: ' + item.getReceivedBytes());
   });
   item.on('done', function(e, state) {
     if (state == "completed") {
@@ -24,7 +24,6 @@ win.webContents.session.on('will-download', function(event, item, webContents) {
   });
 ```
 
-
 ## Events
 
 ### Event: 'updated'
@@ -79,13 +78,13 @@ Returns a `String` represents the mime type.
 
 Returns a `Boolean` indicates whether the download has user gesture.
 
-### `downloadItem.getSuggestedFilename()`
+### `downloadItem.getFilename()`
 
-Returns a `String` represents the suggested file name of the download file.
+Returns a `String` represents the file name of the download item.
 
-**Note:** The suggested file name is not always the same as the actual one saved
-in local disk. If user changes the file name in a prompted download saving
-dialog, the actual name of saved file will be different with the suggested one.
+**Note:** The file name is not always the same as the actual one saved in local
+disk. If user changes the file name in a prompted download saving dialog, the
+actual name of saved file will be different.
 
 ### `downloadItem.getTotalBytes()`
 
index fb036bc412b5e42b2b04d0d0055d6dcaa71dfa97..9e083d27c0f7d4235f080ddd308c1d357ea7fdb5 100644 (file)
@@ -95,8 +95,9 @@ describe 'session module', ->
         ipc.sendSync 'set-download-option', false
         w.loadUrl "#{url}:#{port}"
         ipc.once 'download-done', (state, url, mimeType, receivedBytes,
-            totalBytes, disposition) ->
+            totalBytes, disposition, filename) ->
           assert.equal state, 'completed'
+          assert.equal filename, 'mock.pdf'
           assert.equal url, "http://127.0.0.1:#{port}/"
           assert.equal mimeType, 'application/pdf'
           assert.equal receivedBytes, mockPDF.length
@@ -112,8 +113,9 @@ describe 'session module', ->
         ipc.sendSync 'set-download-option', true
         w.loadUrl "#{url}:#{port}/"
         ipc.once 'download-done', (state, url, mimeType, receivedBytes,
-            totalBytes, disposition) ->
+            totalBytes, disposition, filename) ->
           assert.equal state, 'cancelled'
+          assert.equal filename, 'mock.pdf'
           assert.equal mimeType, 'application/pdf'
           assert.equal receivedBytes, 0
           assert.equal totalBytes, mockPDF.length
index 6f84c3f1c7c6e4c5be0ae72d72810c6c99b6e056..5b10bb6d43d73ed8ca2bd799c82d6b07d8a1648f 100644 (file)
@@ -89,7 +89,8 @@ app.on('ready', function() {
                                     item.getMimeType(),
                                     item.getReceivedBytes(),
                                     item.getTotalBytes(),
-                                    item.getContentDisposition());
+                                    item.getContentDisposition(),
+                                    item.getFilename());
           });
           if (need_cancel)
             item.cancel();