fix cbz import

This commit is contained in:
2024-12-02 20:55:47 +08:00
parent 867b2a4b64
commit 9b821f1b46
4 changed files with 81 additions and 73 deletions

View File

@@ -104,14 +104,14 @@ abstract class CBZ {
FilePath.join(LocalManager().path, sanitizeFileName(metaData.title)),
);
dest.createSync();
coverFile.copy(
FilePath.join(dest.path, 'cover.${coverFile.path.split('.').last}'));
coverFile.copyMem(
FilePath.join(dest.path, 'cover.${coverFile.extension}'));
if (metaData.chapters == null) {
for (var i = 0; i < files.length; i++) {
var src = files[i];
var dst = File(
FilePath.join(dest.path, '${i + 1}.${src.path.split('.').last}'));
await src.copy(dst.path);
await src.copyMem(dst.path);
}
} else {
dest.createSync();
@@ -129,7 +129,7 @@ abstract class CBZ {
var src = chapter.value[i];
var dst = File(FilePath.join(
chapterDir.path, '${i + 1}.${src.path.split('.').last}'));
await src.copy(dst.path);
await src.copyMem(dst.path);
}
}
}
@@ -142,10 +142,9 @@ abstract class CBZ {
directory: dest.name,
chapters: cpMap,
downloadedChapters: cpMap?.keys.toList() ?? [],
cover: 'cover.${coverFile.path.split('.').last}',
cover: 'cover.${coverFile.extension}',
createdAt: DateTime.now(),
);
LocalManager().add(comic);
await cache.delete(recursive: true);
return comic;
}
@@ -164,7 +163,7 @@ abstract class CBZ {
var dstName =
'${i.toString().padLeft(width, '0')}.${image.split('.').last}';
var dst = File(FilePath.join(cache.path, dstName));
await src.copy(dst.path);
await src.copyMem(dst.path);
i++;
}
} else {
@@ -192,13 +191,13 @@ abstract class CBZ {
var dstName =
'${i.toString().padLeft(width, '0')}.${image.split('.').last}';
var dst = File(FilePath.join(cache.path, dstName));
await src.copy(dst.path);
await src.copyMem(dst.path);
i++;
}
}
var cover = comic.coverFile;
await cover
.copy(FilePath.join(cache.path, 'cover.${cover.path.split('.').last}'));
.copyMem(FilePath.join(cache.path, 'cover.${cover.path.split('.').last}'));
await File(FilePath.join(cache.path, 'metadata.json')).writeAsString(
jsonEncode(
ComicMetaData(

View File

@@ -13,7 +13,7 @@ class FileType {
var mime = lookupMimeType('no-file.$ext') ?? 'application/octet-stream';
// Android doesn't support some mime types
mime = switch(mime) {
'text/javascript' => 'application/javascript',
'text/javascript' => 'application/octet-stream',
'application/x-cbr' => 'application/octet-stream',
_ => mime,
};

View File

@@ -34,7 +34,7 @@ class ImportComic {
App.rootContext.showMessage(message: e.toString());
}
controller.close();
return registerComics(imported, true);
return registerComics(imported, false);
}
Future<bool> ehViewer() async {
@@ -105,8 +105,7 @@ class ImportComic {
if (cancelled) {
break;
}
var folderName =
tag == '' ? '(EhViewer)Default'.tl : '(EhViewer)$tag';
var folderName = tag == '' ? '(EhViewer)Default'.tl : '(EhViewer)$tag';
var comicList = db.select("""
SELECT *
FROM DOWNLOAD_DIRNAME DN
@@ -176,8 +175,7 @@ class ImportComic {
String? title,
String? subtitle,
List<String>? tags,
DateTime? createTime})
async {
DateTime? createTime}) async {
if (!(await directory.exists())) return null;
var name = title ?? directory.name;
if (LocalManager().findByName(name) != null) {
@@ -212,7 +210,8 @@ class ImportComic {
}
fileList.sort();
coverPath = fileList.firstWhereOrNull((l) => l.startsWith('cover')) ?? fileList.first;
coverPath = fileList.firstWhereOrNull((l) => l.startsWith('cover')) ??
fileList.first;
chapters.sort();
if (hasChapters && coverPath == '') {
@@ -243,7 +242,9 @@ class ImportComic {
);
}
static Future<Map<String, String>> _copyDirectories(Map<String, dynamic> data) async {
static Future<Map<String, String>> _copyDirectories(
Map<String, dynamic> data) async {
return overrideIO(() async {
var toBeCopied = data['toBeCopied'] as List<String>;
var destination = data['destination'] as String;
Map<String, String> result = {};
@@ -255,7 +256,7 @@ class ImportComic {
// Rename the old directory to avoid conflicts.
Log.info("Import Comic",
"Directory already exists: ${source.name}\nRenaming the old directory.");
await dest.rename(
dest.renameSync(
findValidDirectoryName(dest.parent.path, "${dest.path}_old"));
}
dest.createSync();
@@ -263,6 +264,7 @@ class ImportComic {
result[source.path] = dest.path;
}
return result;
});
}
Future<Map<String?, List<LocalComic>>> _copyComicsToLocalDir(
@@ -284,13 +286,13 @@ class ImportComic {
// copy the comics to the local directory
var pathMap = await compute<Map<String, dynamic>, Map<String, String>>(
_copyDirectories, {
'toBeCopied': comics[favoriteFolder]!.map((e) => e.directory).toList(),
'toBeCopied':
comics[favoriteFolder]!.map((e) => e.directory).toList(),
'destination': destPath,
});
//Construct a new object since LocalComic.directory is a final String
for (var c in comics[favoriteFolder]!) {
result[favoriteFolder]!.add(
LocalComic(
result[favoriteFolder]!.add(LocalComic(
id: c.id,
title: c.title,
subtitle: c.subtitle,
@@ -300,20 +302,20 @@ class ImportComic {
cover: c.cover,
comicType: c.comicType,
downloadedChapters: c.downloadedChapters,
createdAt: c.createdAt
)
);
createdAt: c.createdAt,
));
}
} catch (e) {
} catch (e, s) {
App.rootContext.showMessage(message: "Failed to copy comics".tl);
Log.error("Import Comic", e.toString());
Log.error("Import Comic", e.toString(), s);
return result;
}
}
return result;
}
Future<bool> registerComics(Map<String?, List<LocalComic>> importedComics, bool copy) async {
Future<bool> registerComics(
Map<String?, List<LocalComic>> importedComics, bool copy) async {
try {
if (copy) {
importedComics = await _copyComicsToLocalDir(importedComics);
@@ -334,9 +336,7 @@ class ImportComic {
author: comic.subtitle,
type: comic.comicType,
tags: comic.tags,
favoriteTime: comic.createdAt
)
);
favoriteTime: comic.createdAt));
}
}
}
@@ -344,9 +344,9 @@ class ImportComic {
message: "Imported @a comics".tlParams({
'a': importedCount,
}));
} catch(e) {
} catch (e, s) {
App.rootContext.showMessage(message: "Failed to register comics".tl);
Log.error("Import Comic", e.toString());
Log.error("Import Comic", e.toString(), s);
return false;
}
return true;

View File

@@ -73,6 +73,15 @@ extension FileSystemEntityExt on FileSystemEntity {
extension FileExtension on File {
String get extension => path.split('.').last;
/// Copy the file to the specified path using memory.
///
/// This method prevents errors caused by files from different file systems.
Future<void> copyMem(String newPath) async {
var newFile = File(newPath);
// Stream is not usable since [AndroidFile] does not support [openRead].
await newFile.writeAsBytes(await readAsBytes());
}
}
extension DirectoryExtension on Directory {