8000 fix: don't create last saved path if none exists by trop[bot] · Pull Request #27806 · electron/electron · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: don't create last saved path if none exists #27806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions shell/browser/electron_download_manager_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,27 @@ base::FilePath CreateDownloadPath(const GURL& url,
const std::string& content_disposition,
const std::string& suggested_filename,
const std::string& mime_type,
const base::FilePath& last_saved_directory,
const base::FilePath& default_download_path) {
auto generated_name =
net::GenerateFileName(url, content_disposition, std::string(),
suggested_filename, mime_type, "download");

if (!base::PathExists(default_download_path))
base::CreateDirectory(default_download_path);
base::FilePath download_path;

return default_download_path.Append(generated_name);
// If the last saved directory is a non-empty existent path, use it as the
// default.
if (last_saved_directory.empty() || !base::PathExists(last_saved_directory)) {
download_path = default_download_path;

if (!base::PathExists(download_path))
base::CreateDirectory(download_path);
} else {
// Otherwise use the global default.
download_path = last_saved_directory;
}

return download_path.Append(generated_name);
}

} // namespace
Expand Down Expand Up @@ -153,10 +165,7 @@ void ElectronDownloadManagerDelegate::OnDownloadSaveDialogDone(
if (!canceled) {
if (result.Get("filePath", &path)) {
// Remember the last selected download directory.
auto* browser_context = static_cast<ElectronBrowserContext*>(
download_manager_->GetBrowserContext());
browser_context->prefs()->SetFilePath(prefs::kDownloadDefaultDirectory,
path.DirName());
last_saved_directory_ = path.DirName();

api::DownloadItem* download = api::DownloadItem::FromDownloadItem(item);
if (download)
Expand Down Expand Up @@ -222,7 +231,7 @@ bool ElectronDownloadManagerDelegate::DetermineDownloadTarget(
base::BindOnce(&CreateDownloadPath, download->GetURL(),
download->GetContentDisposition(),
download->GetSuggestedFilename(), download->GetMimeType(),
default_download_path),
last_saved_directory_, default_download_path),
base::BindOnce(&ElectronDownloadManagerDelegate::OnDownloadPathGenerated,
weak_ptr_factory_.GetWeakPtr(), download->GetId(),
std::move(*callback)));
Expand Down
2 changes: 2 additions & 0 deletions shell/browser/electron_download_manager_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class ElectronDownloadManagerDelegate
content::DownloadTargetCallback download_callback,
gin_helper::Dictionary result);

base::FilePath last_saved_directory_;

content::DownloadManager* download_manager_;
base::WeakPtrFactory<ElectronDownloadManagerDelegate> weak_ptr_factory_;

Expand Down
0