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

View File

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

View File

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

View File

@@ -53,9 +53,9 @@ class _ImagePageState extends State<ImagePage> with WindowListener{
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ColoredBox( return ScaffoldPage(
color: FluentTheme.of(context).micaBackgroundColor.withOpacity(1), padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
child: Stack( content: Stack(
children: [ children: [
Positioned.fill(child: PhotoView( Positioned.fill(child: PhotoView(
backgroundDecoration: const BoxDecoration( backgroundDecoration: const BoxDecoration(
@@ -82,6 +82,7 @@ class _ImagePageState extends State<ImagePage> with WindowListener{
const Expanded( const Expanded(
child: DragToMoveArea(child: SizedBox.expand(),), child: DragToMoveArea(child: SizedBox.expand(),),
), ),
if(App.isDesktop)
WindowButtons(key: ValueKey(windowButtonKey),), 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) { String bytesToText(int bytes) {
if(bytes < 1024) { if(bytes < 1024) {
return "$bytes B"; return "$bytes B";