fix & improve importing comic

This commit is contained in:
2024-11-19 18:44:52 +08:00
parent 8402c1c9f3
commit 6aeaeadb10
10 changed files with 557 additions and 450 deletions

View File

@@ -86,6 +86,9 @@ abstract class CBZ {
var ext = e.path.split('.').last;
return !['jpg', 'jpeg', 'png', 'webp', 'gif', 'jpe'].contains(ext);
});
if(files.isEmpty) {
throw Exception('No images found in the archive');
}
files.sort((a, b) => a.path.compareTo(b.path));
var coverFile = files.firstWhereOrNull(
(element) =>

View File

@@ -10,6 +10,9 @@ class FileType {
if(ext.startsWith('.')) {
ext = ext.substring(1);
}
if(ext == 'cbz') {
return const FileType('.cbz', 'application/octet-stream');
}
var mime = lookupMimeType('no-file.$ext');
return FileType(".$ext", mime ?? 'application/octet-stream');
}

338
lib/utils/import_comic.dart Normal file
View File

@@ -0,0 +1,338 @@
import 'dart:math';
import 'package:flutter/foundation.dart';
import 'package:venera/components/components.dart';
import 'package:venera/foundation/app.dart';
import 'package:venera/foundation/comic_type.dart';
import 'package:venera/foundation/favorites.dart';
import 'package:venera/foundation/local.dart';
import 'package:venera/foundation/log.dart';
import 'package:sqlite3/sqlite3.dart' as sql;
import 'package:venera/utils/translations.dart';
import 'cbz.dart';
import 'io.dart';
class ImportComic {
final String? selectedFolder;
const ImportComic({this.selectedFolder});
Future<bool> cbz() async {
var xFile = await selectFile(ext: ['cbz']);
if(xFile == null) {
return false;
}
var controller = showLoadingDialog(App.rootContext, allowCancel: false);
var isSuccessful = false;
try {
var cache = FilePath.join(App.cachePath, xFile.name);
await xFile.saveTo(cache);
var comic = await CBZ.import(File(cache));
if (selectedFolder != null) {
LocalFavoritesManager().addComic(
selectedFolder!,
FavoriteItem(
id: comic.id,
name: comic.title,
coverPath: comic.cover,
author: comic.subtitle,
type: comic.comicType,
tags: comic.tags,
),
);
}
await File(cache).deleteIgnoreError();
isSuccessful = true;
} catch (e, s) {
Log.error("Import Comic", e.toString(), s);
App.rootContext.showMessage(message: e.toString());
}
controller.close();
return isSuccessful;
}
Future<bool> ehViewer() async {
var dbFile = await selectFile(ext: ['db']);
final picker = DirectoryPicker();
final comicSrc = await picker.pickDirectory();
if (dbFile == null || comicSrc == null) {
return false;
}
bool cancelled = false;
var controller = showLoadingDialog(App.rootContext, onCancel: () {
cancelled = true;
});
bool isSuccessful = false;
try {
var cache = FilePath.join(App.cachePath, dbFile.name);
await dbFile.saveTo(cache);
var db = sql.sqlite3.open(cache);
Future<void> addTagComics(String destFolder, List<sql.Row> comics) async {
for (var comic in comics) {
if (cancelled) {
return;
}
var comicDir = Directory(
FilePath.join(comicSrc.path, comic['DIRNAME'] as String));
if (!(await comicDir.exists())) {
continue;
}
String titleJP =
comic['TITLE_JPN'] == null ? "" : comic['TITLE_JPN'] as String;
String title = titleJP == "" ? comic['TITLE'] as String : titleJP;
if (LocalManager().findByName(title) != null) {
Log.info("Import Comic", "Comic already exists: $title");
continue;
}
String coverURL = await comicDir.joinFile(".thumb").exists()
? comicDir.joinFile(".thumb").path
: (comic['THUMB'] as String)
.replaceAll('s.exhentai.org', 'ehgt.org');
int downloadedTimeStamp = comic['TIME'] as int;
DateTime downloadedTime = downloadedTimeStamp != 0
? DateTime.fromMillisecondsSinceEpoch(downloadedTimeStamp)
: DateTime.now();
var comicObj = LocalComic(
id: LocalManager().findValidId(ComicType.local),
title: title,
subtitle: '',
tags: [
//1 >> x
[
"MISC",
"DOUJINSHI",
"MANGA",
"ARTISTCG",
"GAMECG",
"IMAGE SET",
"COSPLAY",
"ASIAN PORN",
"NON-H",
"WESTERN",
][(log(comic['CATEGORY'] as int) / ln2).floor()]
],
directory: comicDir.path,
chapters: null,
cover: coverURL,
comicType: ComicType.local,
downloadedChapters: [],
createdAt: downloadedTime,
);
LocalManager().add(comicObj, comicObj.id);
LocalFavoritesManager().addComic(
destFolder,
FavoriteItem(
id: comicObj.id,
name: comicObj.title,
coverPath: comicObj.cover,
author: comicObj.subtitle,
type: comicObj.comicType,
tags: comicObj.tags,
favoriteTime: downloadedTime,
),
);
}
}
{
var defaultFolderName = '(EhViewer)Default';
if (!LocalFavoritesManager().existsFolder(defaultFolderName)) {
LocalFavoritesManager().createFolder(defaultFolderName);
}
var comicList = db.select("""
SELECT *
FROM DOWNLOAD_DIRNAME DN
LEFT JOIN DOWNLOADS DL
ON DL.GID = DN.GID
WHERE DL.LABEL IS NULL AND DL.STATE = 3
ORDER BY DL.TIME DESC
""").toList();
await addTagComics(defaultFolderName, comicList);
}
var folders = db.select("""
SELECT * FROM DOWNLOAD_LABELS;
""");
for (var folder in folders) {
if (cancelled) {
break;
}
var label = folder["LABEL"] as String;
var folderName = '(EhViewer)$label';
if (!LocalFavoritesManager().existsFolder(folderName)) {
LocalFavoritesManager().createFolder(folderName);
}
var comicList = db.select("""
SELECT *
FROM DOWNLOAD_DIRNAME DN
LEFT JOIN DOWNLOADS DL
ON DL.GID = DN.GID
WHERE DL.LABEL = ? AND DL.STATE = 3
ORDER BY DL.TIME DESC
""", [label]).toList();
await addTagComics(folderName, comicList);
}
db.dispose();
await File(cache).deleteIgnoreError();
isSuccessful = true;
} catch (e, s) {
Log.error("Import Comic", e.toString(), s);
App.rootContext.showMessage(message: e.toString());
}
controller.close();
return isSuccessful;
}
Future<bool> directory(bool single) async {
final picker = DirectoryPicker();
final path = await picker.pickDirectory();
if (path == null) {
return false;
}
Map<Directory, LocalComic> comics = {};
if (single) {
var result = await _checkSingleComic(path);
if (result != null) {
comics[path] = result;
} else {
App.rootContext.showMessage(message: "Invalid Comic".tl);
return false;
}
} else {
await for (var entry in path.list()) {
if (entry is Directory) {
var result = await _checkSingleComic(entry);
if (result != null) {
comics[entry] = result;
}
}
}
}
bool shouldCopy = true;
for (var comic in comics.keys) {
if (comic.parent.path == LocalManager().path) {
shouldCopy = false;
break;
}
}
if (shouldCopy && comics.isNotEmpty) {
try {
// copy the comics to the local directory
await compute<Map<String, dynamic>, void>(_copyDirectories, {
'toBeCopied': comics.keys.map((e) => e.path).toList(),
'destination': LocalManager().path,
});
} catch (e) {
App.rootContext.showMessage(message: "Failed to import comics".tl);
Log.error("Import Comic", e.toString());
return false;
}
}
for (var comic in comics.values) {
LocalManager().add(comic, LocalManager().findValidId(ComicType.local));
if (selectedFolder != null) {
LocalFavoritesManager().addComic(
selectedFolder!,
FavoriteItem(
id: comic.id,
name: comic.title,
coverPath: comic.cover,
author: comic.subtitle,
type: comic.comicType,
tags: comic.tags,
),
);
}
}
App.rootContext.showMessage(
message: "Imported @a comics".tlParams({
'a': comics.length,
}));
return true;
}
Future<LocalComic?> _checkSingleComic(Directory directory) async {
if (!(await directory.exists())) return null;
var name = directory.name;
if (LocalManager().findByName(name) != null) {
Log.info("Import Comic", "Comic already exists: $name");
return null;
}
bool hasChapters = false;
var chapters = <String>[];
var coverPath = ''; // relative path to the cover image
for (var entry in directory.listSync()) {
if (entry is Directory) {
hasChapters = true;
chapters.add(entry.name);
await for (var file in entry.list()) {
if (file is Directory) {
Log.info("Import Comic",
"Invalid Chapter: ${entry.name}\nA directory is found in the chapter directory.");
return null;
}
}
} else if (entry is File) {
if (entry.name.startsWith('cover')) {
coverPath = entry.name;
}
const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'gif', 'jpe'];
if (!coverPath.startsWith('cover') &&
imageExtensions.contains(entry.extension)) {
coverPath = entry.name;
}
}
}
chapters.sort();
if (hasChapters && coverPath == '') {
// use the first image in the first chapter as the cover
var firstChapter = Directory('${directory.path}/${chapters.first}');
await for (var entry in firstChapter.list()) {
if (entry is File) {
coverPath = entry.name;
break;
}
}
}
if (coverPath == '') {
Log.info("Import Comic", "Invalid Comic: $name\nNo cover image found.");
return null;
}
return LocalComic(
id: '0',
title: name,
subtitle: '',
tags: [],
directory: directory.name,
chapters: hasChapters ? Map.fromIterables(chapters, chapters) : null,
cover: coverPath,
comicType: ComicType.local,
downloadedChapters: chapters,
createdAt: DateTime.now(),
);
}
static _copyDirectories(Map<String, dynamic> data) {
var toBeCopied = data['toBeCopied'] as List<String>;
var destination = data['destination'] as String;
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.rename(
findValidDirectoryName(dest.parent.path, "${dest.path}_old"));
}
dest.createSync();
copyDirectory(source, dest);
}
}
}

View File

@@ -14,6 +14,21 @@ import 'package:venera/utils/file_type.dart';
export 'dart:io';
export 'dart:typed_data';
class IO {
/// A global flag used to indicate whether the app is selecting files.
///
/// Select file and other similar file operations will launch external programs,
/// causing the app to lose focus. AppLifecycleState will be set to paused.
static bool get isSelectingFiles => _isSelectingFiles;
static bool _isSelectingFiles = false;
}
/// A finalizer that can be used to dispose resources related to file operations.
final _finalizer = Finalizer<Function>((e) {
e();
});
class FilePath {
const FilePath._();
@@ -147,24 +162,35 @@ String findValidDirectoryName(String path, String directory) {
}
class DirectoryPicker {
DirectoryPicker() {
_finalizer.attach(this, dispose);
}
String? _directory;
final _methodChannel = const MethodChannel("venera/method_channel");
Future<Directory?> pickDirectory() async {
if (App.isWindows || App.isLinux) {
var d = await file_selector.getDirectoryPath();
_directory = d;
return d == null ? null : Directory(d);
} else if (App.isAndroid) {
var d = await _methodChannel.invokeMethod<String?>("getDirectoryPath");
_directory = d;
return d == null ? null : Directory(d);
} else {
// ios, macos
var d = await _methodChannel.invokeMethod<String?>("getDirectoryPath");
_directory = d;
return d == null ? null : Directory(d);
IO._isSelectingFiles = true;
try {
if (App.isWindows || App.isLinux) {
var d = await file_selector.getDirectoryPath();
_directory = d;
return d == null ? null : Directory(d);
} else if (App.isAndroid) {
var d = await _methodChannel.invokeMethod<String?>("getDirectoryPath");
_directory = d;
return d == null ? null : Directory(d);
} else {
// ios, macos
var d = await _methodChannel.invokeMethod<String?>("getDirectoryPath");
_directory = d;
return d == null ? null : Directory(d);
}
} finally {
Future.delayed(const Duration(milliseconds: 100), () {
IO._isSelectingFiles = false;
});
}
}
@@ -172,11 +198,15 @@ class DirectoryPicker {
if (_directory == null) {
return;
}
if (App.isAndroid && _directory != null) {
return Directory(_directory!).deleteIgnoreError(recursive: true);
if (App.isAndroid &&
_directory != null &&
_directory!.startsWith(App.cachePath)) {
await Directory(_directory!).deleteIgnoreError(recursive: true);
_directory = null;
}
if (App.isIOS || App.isMacOS) {
await _methodChannel.invokeMethod("stopAccessingSecurityScopedResource");
_directory = null;
}
}
}
@@ -186,53 +216,73 @@ class IOSDirectoryPicker {
// 调用 iOS 目录选择方法
static Future<String?> selectDirectory() async {
IO._isSelectingFiles = true;
try {
final String? path = await _channel.invokeMethod('selectDirectory');
return path;
} catch (e) {
// 返回报错信息
return e.toString();
} finally {
Future.delayed(const Duration(milliseconds: 100), () {
IO._isSelectingFiles = false;
});
}
}
}
Future<file_selector.XFile?> selectFile({required List<String> ext}) async {
var extensions = App.isMacOS || App.isIOS ? null : ext;
if (App.isAndroid) {
for (var e in ext) {
var fileType = FileType.fromExtension(e);
if (fileType.mime == "application/octet-stream") {
extensions = null;
break;
}
}
}
file_selector.XTypeGroup typeGroup = file_selector.XTypeGroup(
label: 'files',
extensions: extensions,
);
file_selector.XFile? file;
if (extensions == null && App.isAndroid) {
const selectFileChannel = MethodChannel("venera/select_file");
var filePath = await selectFileChannel.invokeMethod("selectFile");
if (filePath == null) return null;
file = file_selector.XFile(filePath);
} else {
file = await file_selector.openFile(
acceptedTypeGroups: <file_selector.XTypeGroup>[typeGroup],
IO._isSelectingFiles = true;
try {
var extensions = App.isMacOS || App.isIOS ? null : ext;
file_selector.XTypeGroup typeGroup = file_selector.XTypeGroup(
label: 'files',
extensions: extensions,
);
if (file == null) return null;
file_selector.XFile? file;
if (App.isAndroid) {
const selectFileChannel = MethodChannel("venera/select_file");
String mimeType = "*/*";
if(ext.length == 1) {
mimeType = FileType.fromExtension(ext[0]).mime;
if(mimeType == "application/octet-stream") {
mimeType = "*/*";
}
}
var filePath = await selectFileChannel.invokeMethod(
"selectFile",
mimeType,
);
if (filePath == null) return null;
file = _AndroidFileSelectResult(filePath);
} else {
file = await file_selector.openFile(
acceptedTypeGroups: <file_selector.XTypeGroup>[typeGroup],
);
if (file == null) return null;
}
if (!ext.contains(file.path.split(".").last)) {
App.rootContext.showMessage(message: "Invalid file type");
return null;
}
return file;
} finally {
Future.delayed(const Duration(milliseconds: 100), () {
IO._isSelectingFiles = false;
});
}
if (!ext.contains(file.path.split(".").last)) {
App.rootContext.showMessage(message: "Invalid file type");
return null;
}
return file;
}
Future<String?> selectDirectory() async {
var path = await file_selector.getDirectoryPath();
return path;
IO._isSelectingFiles = true;
try {
var path = await file_selector.getDirectoryPath();
return path;
} finally {
Future.delayed(const Duration(milliseconds: 100), () {
IO._isSelectingFiles = false;
});
}
}
// selectDirectoryIOS
@@ -245,25 +295,32 @@ Future<void> saveFile(
if (data == null && file == null) {
throw Exception("data and file cannot be null at the same time");
}
if (data != null) {
var cache = FilePath.join(App.cachePath, filename);
if (File(cache).existsSync()) {
File(cache).deleteSync();
IO._isSelectingFiles = true;
try {
if (data != null) {
var cache = FilePath.join(App.cachePath, filename);
if (File(cache).existsSync()) {
File(cache).deleteSync();
}
await File(cache).writeAsBytes(data);
file = File(cache);
}
await File(cache).writeAsBytes(data);
file = File(cache);
}
if (App.isMobile) {
final params = SaveFileDialogParams(sourceFilePath: file!.path);
await FlutterFileDialog.saveFile(params: params);
} else {
final result = await file_selector.getSaveLocation(
suggestedName: filename,
);
if (result != null) {
var xFile = file_selector.XFile(file!.path);
await xFile.saveTo(result.path);
if (App.isMobile) {
final params = SaveFileDialogParams(sourceFilePath: file!.path);
await FlutterFileDialog.saveFile(params: params);
} else {
final result = await file_selector.getSaveLocation(
suggestedName: filename,
);
if (result != null) {
var xFile = file_selector.XFile(file!.path);
await xFile.saveTo(result.path);
}
}
} finally {
Future.delayed(const Duration(milliseconds: 100), () {
IO._isSelectingFiles = false;
});
}
}
@@ -302,3 +359,16 @@ String bytesToReadableString(int bytes) {
return "${(bytes / 1024 / 1024 / 1024).toStringAsFixed(2)} GB";
}
}
class _AndroidFileSelectResult extends s.XFile {
_AndroidFileSelectResult(super.path) {
_finalizer.attach(this, dispose);
}
void dispose() {
print("dispose $path");
if (path.startsWith(App.cachePath)) {
File(path).deleteIgnoreError();
}
}
}