Reimplement downloadItem.getFilename API.

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.
This commit is contained in:
Haojian Wu 2015-09-24 16:31:41 +08:00
parent 0861d5d44b
commit 1879392c7b
6 changed files with 25 additions and 16 deletions

View 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);
}

View 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);

View 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()