mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-02-02 06:46:22 +01:00
Load metadata now directly return metadata
This commit is contained in:
parent
f6ec7e10d5
commit
886bcf10ae
3 changed files with 36 additions and 26 deletions
|
@ -17,8 +17,11 @@ bool isSupportedImageFormat(File file) =>
|
|||
bool isSupportedVideoFormat(File file) =>
|
||||
isSupportedFormat(file) && file.contentType?.startsWith("video/") == true;
|
||||
|
||||
bool isMetadataSupportedMime(String mime) =>
|
||||
_metadataSupportedFormatMimes.contains(mime);
|
||||
|
||||
bool isMetadataSupportedFormat(File file) =>
|
||||
_metadataSupportedFormatMimes.contains(file.contentType);
|
||||
isMetadataSupportedMime(file.contentType ?? "");
|
||||
|
||||
bool isTrash(Account account, File file) =>
|
||||
file.path.startsWith(api_util.getTrashbinPath(account));
|
||||
|
|
|
@ -6,33 +6,35 @@ import 'package:image_size_getter/image_size_getter.dart';
|
|||
import 'package:logging/logging.dart';
|
||||
import 'package:nc_photos/account.dart';
|
||||
import 'package:nc_photos/debug_util.dart';
|
||||
import 'package:nc_photos/entity/exif.dart';
|
||||
import 'package:nc_photos/entity/file.dart';
|
||||
import 'package:nc_photos/entity/file_util.dart' as file_util;
|
||||
import 'package:nc_photos/image_size_getter_util.dart';
|
||||
|
||||
class LoadMetadata {
|
||||
/// Load metadata of [binary], which is the content of [file]
|
||||
Future<Map<String, dynamic>> call(
|
||||
Account account, File file, Uint8List binary) {
|
||||
Future<Metadata> loadRemote(Account account, File file, Uint8List binary) {
|
||||
return _loadMetadata(
|
||||
file: file,
|
||||
mime: file.contentType ?? "",
|
||||
exifdartReaderBuilder: () => MemoryBlobReader(binary),
|
||||
imageSizeGetterInputBuilder: () => AsyncMemoryInput(binary),
|
||||
filename: file.path,
|
||||
);
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> _loadMetadata({
|
||||
required File file,
|
||||
Future<Metadata> _loadMetadata({
|
||||
required String mime,
|
||||
required exifdart.AbstractBlobReader Function() exifdartReaderBuilder,
|
||||
required AsyncImageInput Function() imageSizeGetterInputBuilder,
|
||||
String? filename,
|
||||
}) async {
|
||||
var metadata = exifdart.Metadata();
|
||||
if (file_util.isMetadataSupportedFormat(file)) {
|
||||
if (file_util.isMetadataSupportedMime(mime)) {
|
||||
try {
|
||||
metadata = await exifdart.readMetadata(exifdartReaderBuilder());
|
||||
} catch (e, stacktrace) {
|
||||
_log.shout(
|
||||
"[_loadMetadata] Failed while readMetadata for ${file.contentType} file: ${logFilename(file.path)}",
|
||||
"[_loadMetadata] Failed while readMetadata for $mime file: ${logFilename(filename)}",
|
||||
e,
|
||||
stacktrace);
|
||||
// ignore exif
|
||||
|
@ -58,7 +60,7 @@ class LoadMetadata {
|
|||
} catch (e, stacktrace) {
|
||||
// is this even an image file?
|
||||
_log.shout(
|
||||
"[_loadMetadata] Failed while getSize for ${file.contentType} file: ${logFilename(file.path)}",
|
||||
"[_loadMetadata] Failed while getSize for $mime file: ${logFilename(filename)}",
|
||||
e,
|
||||
stacktrace);
|
||||
}
|
||||
|
@ -73,7 +75,7 @@ class LoadMetadata {
|
|||
}
|
||||
}
|
||||
|
||||
return {
|
||||
final map = {
|
||||
if (metadata.exif != null) "exif": metadata.exif,
|
||||
if (imageWidth > 0 && imageHeight > 0)
|
||||
"resolution": {
|
||||
|
@ -81,6 +83,24 @@ class LoadMetadata {
|
|||
"height": imageHeight,
|
||||
},
|
||||
};
|
||||
return _buildMetadata(map);
|
||||
}
|
||||
|
||||
Metadata _buildMetadata(Map<String, dynamic> map) {
|
||||
int? imageWidth, imageHeight;
|
||||
Exif? exif;
|
||||
if (map.containsKey("resolution")) {
|
||||
imageWidth = map["resolution"]["width"];
|
||||
imageHeight = map["resolution"]["height"];
|
||||
}
|
||||
if (map.containsKey("exif")) {
|
||||
exif = Exif(map["exif"]);
|
||||
}
|
||||
return Metadata(
|
||||
imageWidth: imageWidth,
|
||||
imageHeight: imageHeight,
|
||||
exif: exif,
|
||||
);
|
||||
}
|
||||
|
||||
static final _log = Logger("use_case.load_metadata.LoadMetadata");
|
||||
|
|
|
@ -4,7 +4,6 @@ import 'package:kiwi/kiwi.dart';
|
|||
import 'package:logging/logging.dart';
|
||||
import 'package:nc_photos/account.dart';
|
||||
import 'package:nc_photos/connectivity_util.dart' as connectivity_util;
|
||||
import 'package:nc_photos/entity/exif.dart';
|
||||
import 'package:nc_photos/entity/file.dart';
|
||||
import 'package:nc_photos/event/event.dart';
|
||||
import 'package:nc_photos/exception.dart';
|
||||
|
@ -62,27 +61,15 @@ class UpdateMissingMetadata {
|
|||
}
|
||||
_log.fine("[call] Updating metadata for ${file.path}");
|
||||
final binary = await GetFileBinary(fileRepo)(account, file);
|
||||
final metadata = await LoadMetadata()(account, file, binary);
|
||||
int? imageWidth, imageHeight;
|
||||
Exif? exif;
|
||||
if (metadata.containsKey("resolution")) {
|
||||
imageWidth = metadata["resolution"]["width"];
|
||||
imageHeight = metadata["resolution"]["height"];
|
||||
}
|
||||
if (metadata.containsKey("exif")) {
|
||||
exif = Exif(metadata["exif"]);
|
||||
}
|
||||
final metadataObj = Metadata(
|
||||
final metadata =
|
||||
(await LoadMetadata().loadRemote(account, file, binary)).copyWith(
|
||||
fileEtag: file.etag,
|
||||
imageWidth: imageWidth,
|
||||
imageHeight: imageHeight,
|
||||
exif: exif,
|
||||
);
|
||||
|
||||
await UpdateProperty(fileRepo)(
|
||||
account,
|
||||
file,
|
||||
metadata: OrNull(metadataObj),
|
||||
metadata: OrNull(metadata),
|
||||
);
|
||||
yield file;
|
||||
|
||||
|
|
Loading…
Reference in a new issue