fix: don't create last saved path if none exists (#27745)
This commit is contained in:
parent
599f398ddc
commit
b12e47b798
2 changed files with 19 additions and 8 deletions
|
@ -37,15 +37,27 @@ base::FilePath CreateDownloadPath(const GURL& url,
|
||||||
const std::string& content_disposition,
|
const std::string& content_disposition,
|
||||||
const std::string& suggested_filename,
|
const std::string& suggested_filename,
|
||||||
const std::string& mime_type,
|
const std::string& mime_type,
|
||||||
|
const base::FilePath& last_saved_directory,
|
||||||
const base::FilePath& default_download_path) {
|
const base::FilePath& default_download_path) {
|
||||||
auto generated_name =
|
auto generated_name =
|
||||||
net::GenerateFileName(url, content_disposition, std::string(),
|
net::GenerateFileName(url, content_disposition, std::string(),
|
||||||
suggested_filename, mime_type, "download");
|
suggested_filename, mime_type, "download");
|
||||||
|
|
||||||
if (!base::PathExists(default_download_path))
|
base::FilePath download_path;
|
||||||
base::CreateDirectory(default_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
|
} // namespace
|
||||||
|
@ -153,10 +165,7 @@ void ElectronDownloadManagerDelegate::OnDownloadSaveDialogDone(
|
||||||
if (!canceled) {
|
if (!canceled) {
|
||||||
if (result.Get("filePath", &path)) {
|
if (result.Get("filePath", &path)) {
|
||||||
// Remember the last selected download directory.
|
// Remember the last selected download directory.
|
||||||
auto* browser_context = static_cast<ElectronBrowserContext*>(
|
last_saved_directory_ = path.DirName();
|
||||||
download_manager_->GetBrowserContext());
|
|
||||||
browser_context->prefs()->SetFilePath(prefs::kDownloadDefaultDirectory,
|
|
||||||
path.DirName());
|
|
||||||
|
|
||||||
api::DownloadItem* download = api::DownloadItem::FromDownloadItem(item);
|
api::DownloadItem* download = api::DownloadItem::FromDownloadItem(item);
|
||||||
if (download)
|
if (download)
|
||||||
|
@ -222,7 +231,7 @@ bool ElectronDownloadManagerDelegate::DetermineDownloadTarget(
|
||||||
base::BindOnce(&CreateDownloadPath, download->GetURL(),
|
base::BindOnce(&CreateDownloadPath, download->GetURL(),
|
||||||
download->GetContentDisposition(),
|
download->GetContentDisposition(),
|
||||||
download->GetSuggestedFilename(), download->GetMimeType(),
|
download->GetSuggestedFilename(), download->GetMimeType(),
|
||||||
default_download_path),
|
last_saved_directory_, default_download_path),
|
||||||
base::BindOnce(&ElectronDownloadManagerDelegate::OnDownloadPathGenerated,
|
base::BindOnce(&ElectronDownloadManagerDelegate::OnDownloadPathGenerated,
|
||||||
weak_ptr_factory_.GetWeakPtr(), download->GetId(),
|
weak_ptr_factory_.GetWeakPtr(), download->GetId(),
|
||||||
std::move(*callback)));
|
std::move(*callback)));
|
||||||
|
|
|
@ -52,6 +52,8 @@ class ElectronDownloadManagerDelegate
|
||||||
content::DownloadTargetCallback download_callback,
|
content::DownloadTargetCallback download_callback,
|
||||||
gin_helper::Dictionary result);
|
gin_helper::Dictionary result);
|
||||||
|
|
||||||
|
base::FilePath last_saved_directory_;
|
||||||
|
|
||||||
content::DownloadManager* download_manager_;
|
content::DownloadManager* download_manager_;
|
||||||
base::WeakPtrFactory<ElectronDownloadManagerDelegate> weak_ptr_factory_{this};
|
base::WeakPtrFactory<ElectronDownloadManagerDelegate> weak_ptr_factory_{this};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue