fix download

This commit is contained in:
wgh19
2024-05-14 18:10:30 +08:00
parent 9ba859d668
commit 07ff6d9526
5 changed files with 37 additions and 21 deletions

View File

@@ -3,6 +3,7 @@ import 'dart:io';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pixes/utils/io.dart';
import 'foundation/app.dart';
import 'network/models.dart';
@@ -43,12 +44,13 @@ class _Appdata {
}
}
String get downloadPath => settings["downloadPath"];
Future<String> get _defaultDownloadPath async{
if(App.isAndroid) {
var externalStoragePaths = await getExternalStorageDirectories(type: StorageDirectory.downloads);
var res = externalStoragePaths?.first.path;
String? downloadPath = "/storage/emulated/0/download";
if (!Directory(downloadPath).havePermission()) {
downloadPath = null;
}
var res = downloadPath;
res ??= (await getExternalStorageDirectory())!.path;
return "$res/pixes";
} else if (App.isWindows){
@@ -58,7 +60,7 @@ class _Appdata {
}
} else if (App.isMacOS || App.isLinux) {
var downloadPath = (await getDownloadsDirectory())?.path;
if(downloadPath != null) {
if(downloadPath != null && Directory(downloadPath).havePermission()) {
return "$downloadPath/pixes";
}
}

View File

@@ -113,18 +113,19 @@ class DownloadingTask {
static String _generateFilePath(Illust illust, int index, String ext) {
final String downloadPath = appdata.settings["downloadPath"];
final String subPathPatten = appdata.settings["downloadSubPath"];
String subPathPatten = appdata.settings["downloadSubPath"];
final tags = appdata.settings["useTranslatedNameForDownload"] == false
? illust.tags.map((e) => e.name).toList()
: illust.tags.map((e) => e.translatedName ?? e.name).toList();
final tagsWeight = (appdata.settings["tagsWeight"] as String).split(' ');
tags.sort((a, b) => tagsWeight.indexOf(a) - tagsWeight.indexOf(b));
subPathPatten.replaceAll(r"${id}", illust.id.toString());
subPathPatten.replaceAll(r"${title}", illust.title);
subPathPatten.replaceAll(r"${author}", illust.author.name);
subPathPatten.replaceAll(r"${ext}", ext);
subPathPatten = subPathPatten.replaceAll(r"${id}", illust.id.toString());
subPathPatten = subPathPatten.replaceAll(r"${title}", illust.title);
subPathPatten = subPathPatten.replaceAll(r"${author}", illust.author.name);
subPathPatten = subPathPatten.replaceAll(r"${index}", index.toString());
subPathPatten = subPathPatten.replaceAll(r"${ext}", ext);
for(int i=0; i<tags.length; i++) {
subPathPatten.replaceAll("\${tag$i}", tags[i]);
subPathPatten = subPathPatten.replaceAll("\${tag$i}", tags[i]);
}
return "$downloadPath$subPathPatten";
}
@@ -203,7 +204,7 @@ class DownloadManager {
var res = _db.select('''
select * from images
where illust_id = ? and image_index = ?;
''');
''', [illustId, index]);
if (res.isEmpty) return null;
var file = File(res.first["path"] as String);
if (!file.existsSync()) return null;

View File

@@ -74,11 +74,6 @@ class _IllustPageState extends State<IllustPage> {
}
Widget buildImage(double width, double height, int index) {
File? downloadFile;
if(widget.illust.downloaded) {
downloadFile = DownloadManager().getImage(widget.illust.id, index);
}
if (index == 0) {
return Text(
widget.illust.title,
@@ -86,6 +81,10 @@ class _IllustPageState extends State<IllustPage> {
).paddingVertical(8).paddingHorizontal(12);
}
index--;
File? downloadFile;
if(widget.illust.downloaded) {
downloadFile = DownloadManager().getImage(widget.illust.id, index);
}
if (index == widget.illust.images.length) {
return const SizedBox(
height: _kBottomBarHeight,
@@ -107,6 +106,7 @@ class _IllustPageState extends State<IllustPage> {
? widget.illust.images[index].original
: "file://${downloadFile.path}"),
child: Image(
key: ValueKey(index),
image: downloadFile == null
? CachedImageProvider(widget.illust.images[index].large) as ImageProvider
: FileImage(downloadFile) as ImageProvider,

View File

@@ -53,9 +53,9 @@ class _ImagePageState extends State<ImagePage> with WindowListener{
@override
Widget build(BuildContext context) {
return ColoredBox(
color: FluentTheme.of(context).micaBackgroundColor.withOpacity(1),
child: Stack(
return ScaffoldPage(
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
content: Stack(
children: [
Positioned.fill(child: PhotoView(
backgroundDecoration: const BoxDecoration(
@@ -82,7 +82,8 @@ class _ImagePageState extends State<ImagePage> with WindowListener{
const Expanded(
child: DragToMoveArea(child: SizedBox.expand(),),
),
WindowButtons(key: ValueKey(windowButtonKey),),
if(App.isDesktop)
WindowButtons(key: ValueKey(windowButtonKey),),
],
),
),

View File

@@ -21,6 +21,18 @@ extension FSExt on FileSystemEntity {
}
}
extension DirectoryExt on Directory {
bool havePermission() {
if(!existsSync()) return false;
try {
listSync();
return true;
} catch (e) {
return false;
}
}
}
String bytesToText(int bytes) {
if(bytes < 1024) {
return "$bytes B";