From 895f73b1d0f58815546b91ab8ac7ddab93af25ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=85=B1=E5=A4=A9=E5=B0=8F=E7=A6=BD=E5=85=BD?= Date: Fri, 15 Jul 2022 00:40:57 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A6=81=E6=AD=A2=E9=80=89=E6=8B=A9=E6=97=A0?= =?UTF-8?q?=E6=9D=83=E9=99=90=E7=9A=84=E8=B7=AF=E5=BE=84=E4=BD=9C=E4=B8=BA?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E8=B7=AF=E5=BE=84=20Disable=20the=20selectio?= =?UTF-8?q?n=20of=20unprivileged=20path=20as=20download=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/l18n/en_US.dart | 2 + lib/src/l18n/zh_CN.dart | 2 + .../download/setting_download_page.dart | 37 +++++++++++++++---- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/src/l18n/en_US.dart b/lib/src/l18n/en_US.dart index f76410f5..e043bb13 100644 --- a/lib/src/l18n/en_US.dart +++ b/lib/src/l18n/en_US.dart @@ -339,9 +339,11 @@ class en_US { /// download setting page 'downloadPath': 'Download Path', + 'changeDownloadPathHint': 'Attention! Change download path will copy downloaded gallerys automatically and empty old directory. Long press to change.', 'resetDownloadPath': 'Reset download path', 'longPress2Reset': 'Long Press to Reset', 'needPermissionToChangeDownloadPath': 'Need permission to change download path', + 'invalidPath': 'Invalid Path', 'downloadTaskConcurrency': 'Download Concurrency', 'needRestart': 'Need Restart', 'speedLimit': 'Speed Limit', diff --git a/lib/src/l18n/zh_CN.dart b/lib/src/l18n/zh_CN.dart index 328c736f..4c69fb2c 100644 --- a/lib/src/l18n/zh_CN.dart +++ b/lib/src/l18n/zh_CN.dart @@ -339,9 +339,11 @@ class zh_CN { /// download setting page 'downloadPath': '下载路径', + 'changeDownloadPathHint': '注意!改变下载路径会自动复制已下载的画廊到新路径,并清空原文件夹。长按来改变。', 'resetDownloadPath': '重置下载路径', 'longPress2Reset': '长按以重置', 'needPermissionToChangeDownloadPath': '需要权限来改变下载路径', + 'invalidPath': '无效的路径', 'downloadTaskConcurrency': '同时下载图片数量', 'needRestart': '需要重启', 'downloadTimeout': '单次下载超时时间', diff --git a/lib/src/pages/setting/download/setting_download_page.dart b/lib/src/pages/setting/download/setting_download_page.dart index bb587eb2..2733fa04 100644 --- a/lib/src/pages/setting/download/setting_download_page.dart +++ b/lib/src/pages/setting/download/setting_download_page.dart @@ -1,4 +1,5 @@ import 'dart:io' as io; +import 'dart:io'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; @@ -34,7 +35,8 @@ class SettingDownloadPage extends StatelessWidget { ListTile( title: Text('downloadPath'.tr), subtitle: Text(DownloadSetting.downloadPath.value), - onTap: _handleChangeDownloadPath, + onTap: () => toast('changeDownloadPathHint'.tr, isShort: false), + onLongPress: _handleChangeDownloadPath, ), if (!GetPlatform.isIOS) ListTile( @@ -200,10 +202,10 @@ class SettingDownloadPage extends StatelessWidget { return; } - /// request external storage permission - bool hasStoragePermission = await Permission.manageExternalStorage.request().isGranted; + /// request storage permission + bool hasStoragePermission = await Permission.manageExternalStorage.request().isGranted && await Permission.storage.request().isGranted; if (!hasStoragePermission) { - toast('needPermissionToChangeDownloadPath'.tr); + toast('needPermissionToChangeDownloadPath'.tr, isShort: false); return; } @@ -219,6 +221,14 @@ class SettingDownloadPage extends StatelessWidget { return; } + try { + _checkPermissionForNewPath(newDownloadPath); + } on FileSystemException catch (e) { + toast('invalidPath'.tr); + Log.error('${'invalidPath'.tr}:$newDownloadPath', e); + return; + } + Future future = Future.wait([ galleryDownloadService.pauseAllDownloadGallery(), archiveDownloadService.pauseAllDownloadArchive(), @@ -227,11 +237,16 @@ class SettingDownloadPage extends StatelessWidget { io.Directory oldDir = io.Directory(oldDownloadPath); List gallerys = oldDir.listSync(); + /// copy future = future.then((_) async { - /// copy List futures = []; for (io.FileSystemEntity oldGallery in gallerys) { - io.Directory newGallery = io.Directory(join(newDownloadPath!, basename(oldGallery.path))); + io.FileSystemEntity newGallery = io.Directory(join(newDownloadPath!, basename(oldGallery.path))); + + /// other directory + if (newGallery is! io.Directory || !basename(oldGallery.path).startsWith(RegExp(r'\d'))) { + continue; + } futures.add(newGallery.create(recursive: true).then((_) async { List files = (oldGallery as io.Directory).listSync(); @@ -247,13 +262,13 @@ class SettingDownloadPage extends StatelessWidget { DownloadSetting.saveDownloadPath(newDownloadPath); + /// To be compatible with the previous version, update the database. future = future.then((_) async { - /// To be compatible with the previous version, update the database. await galleryDownloadService.updateImagePathAfterDownloadPathChanged(); }); + /// Empty old directory future = future.then((_) async { - /// Empty old directory await oldDir.delete(recursive: true).then((_) => oldDir.create(recursive: true)); }); @@ -279,4 +294,10 @@ class SettingDownloadPage extends StatelessWidget { '${'restoredGalleryCount'.tr}: $restoredGalleryCount, ${'restoredArchiveCount'.tr}: $restoredArchiveCount', ); } + + void _checkPermissionForNewPath(String newDownloadPath) { + io.File file = io.File(join(newDownloadPath, 'test')); + file.createSync(recursive: true); + file.deleteSync(); + } }