Skip to content

Commit

Permalink
禁止选择无权限的路径作为下载路径
Browse files Browse the repository at this point in the history
Disable the selection of unprivileged path as download path
  • Loading branch information
jiangtian616 committed Jul 14, 2022
1 parent 2cff0bf commit 895f73b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lib/src/l18n/en_US.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions lib/src/l18n/zh_CN.dart
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,11 @@ class zh_CN {

/// download setting page
'downloadPath': '下载路径',
'changeDownloadPathHint': '注意!改变下载路径会自动复制已下载的画廊到新路径,并清空原文件夹。长按来改变。',
'resetDownloadPath': '重置下载路径',
'longPress2Reset': '长按以重置',
'needPermissionToChangeDownloadPath': '需要权限来改变下载路径',
'invalidPath': '无效的路径',
'downloadTaskConcurrency': '同时下载图片数量',
'needRestart': '需要重启',
'downloadTimeout': '单次下载超时时间',
Expand Down
37 changes: 29 additions & 8 deletions lib/src/pages/setting/download/setting_download_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:io' as io;
import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}

Expand All @@ -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(),
Expand All @@ -227,11 +237,16 @@ class SettingDownloadPage extends StatelessWidget {
io.Directory oldDir = io.Directory(oldDownloadPath);
List<io.FileSystemEntity> gallerys = oldDir.listSync();

/// copy
future = future.then((_) async {
/// copy
List<Future> 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<io.FileSystemEntity> files = (oldGallery as io.Directory).listSync();
Expand All @@ -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));
});

Expand All @@ -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();
}
}

0 comments on commit 895f73b

Please sign in to comment.