Skip to content

Commit

Permalink
fix cbz import
Browse files Browse the repository at this point in the history
  • Loading branch information
wgh136 committed Dec 2, 2024
1 parent 1a50b8b commit b8bdda1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 73 deletions.
17 changes: 8 additions & 9 deletions lib/utils/cbz.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
}
}
Expand All @@ -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;
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/file_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
126 changes: 63 additions & 63 deletions lib/utils/import_comic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ImportComic {
Future<bool> cbz() async {
var file = await selectFile(ext: ['cbz', 'zip']);
Map<String?, List<LocalComic>> imported = {};
if(file == null) {
if (file == null) {
return false;
}
var controller = showLoadingDialog(App.rootContext, allowCancel: false);
Expand All @@ -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 {
Expand Down Expand Up @@ -63,7 +63,7 @@ class ImportComic {
var comicDir = Directory(
FilePath.join(comicSrc.path, comic['DIRNAME'] as String));
String titleJP =
comic['TITLE_JPN'] == null ? "" : comic['TITLE_JPN'] as String;
comic['TITLE_JPN'] == null ? "" : comic['TITLE_JPN'] as String;
String title = titleJP == "" ? comic['TITLE'] as String : titleJP;
int timeStamp = comic['TIME'] as int;
DateTime downloadTime = timeStamp != 0
Expand Down Expand Up @@ -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
Expand All @@ -133,7 +132,7 @@ class ImportComic {
App.rootContext.showMessage(message: e.toString());
}
controller.close();
if(cancelled) return false;
if (cancelled) return false;
return registerComics(imported, copyToLocal);
}

Expand Down Expand Up @@ -173,11 +172,10 @@ class ImportComic {
//Automatically search for cover image and chapters
Future<LocalComic?> _checkSingleComic(Directory directory,
{String? id,
String? title,
String? subtitle,
List<String>? tags,
DateTime? createTime})
async {
String? title,
String? subtitle,
List<String>? tags,
DateTime? createTime}) async {
if (!(await directory.exists())) return null;
var name = title ?? directory.name;
if (LocalManager().findByName(name) != null) {
Expand Down Expand Up @@ -207,12 +205,13 @@ class ImportComic {
}
}

if(fileList.isEmpty) {
if (fileList.isEmpty) {
return null;
}

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 == '') {
Expand Down Expand Up @@ -243,26 +242,29 @@ class ImportComic {
);
}

static Future<Map<String, String>> _copyDirectories(Map<String, dynamic> data) async {
var toBeCopied = data['toBeCopied'] as List<String>;
var destination = data['destination'] as String;
Map<String, String> result = {};
for (var dir in toBeCopied) {
var source = Directory(dir);
var dest = Directory("$destination/${source.name}");
if (dest.existsSync()) {
// The destination directory already exists, and it is not managed by the app.
// Rename the old directory to avoid conflicts.
Log.info("Import Comic",
"Directory already exists: ${source.name}\nRenaming the old directory.");
await dest.rename(
findValidDirectoryName(dest.parent.path, "${dest.path}_old"));
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 = {};
for (var dir in toBeCopied) {
var source = Directory(dir);
var dest = Directory("$destination/${source.name}");
if (dest.existsSync()) {
// The destination directory already exists, and it is not managed by the app.
// Rename the old directory to avoid conflicts.
Log.info("Import Comic",
"Directory already exists: ${source.name}\nRenaming the old directory.");
dest.renameSync(
findValidDirectoryName(dest.parent.path, "${dest.path}_old"));
}
dest.createSync();
await copyDirectory(source, dest);
result[source.path] = dest.path;
}
dest.createSync();
await copyDirectory(source, dest);
result[source.path] = dest.path;
}
return result;
return result;
});
}

Future<Map<String?, List<LocalComic>>> _copyComicsToLocalDir(
Expand All @@ -284,36 +286,36 @@ 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(
id: c.id,
title: c.title,
subtitle: c.subtitle,
tags: c.tags,
directory: pathMap[c.directory]!,
chapters: c.chapters,
cover: c.cover,
comicType: c.comicType,
downloadedChapters: c.downloadedChapters,
createdAt: c.createdAt
)
);
result[favoriteFolder]!.add(LocalComic(
id: c.id,
title: c.title,
subtitle: c.subtitle,
tags: c.tags,
directory: pathMap[c.directory]!,
chapters: c.chapters,
cover: c.cover,
comicType: c.comicType,
downloadedChapters: c.downloadedChapters,
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);
Expand All @@ -328,25 +330,23 @@ class ImportComic {
LocalFavoritesManager().addComic(
folder,
FavoriteItem(
id: id,
name: comic.title,
coverPath: comic.cover,
author: comic.subtitle,
type: comic.comicType,
tags: comic.tags,
favoriteTime: comic.createdAt
)
);
id: id,
name: comic.title,
coverPath: comic.cover,
author: comic.subtitle,
type: comic.comicType,
tags: comic.tags,
favoriteTime: comic.createdAt));
}
}
}
App.rootContext.showMessage(
message: "Imported @a comics".tlParams({
'a': importedCount,
}));
} catch(e) {
'a': importedCount,
}));
} 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;
Expand Down
9 changes: 9 additions & 0 deletions lib/utils/io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b8bdda1

Please sign in to comment.