mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-02-08 18:28:53 +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) =>
|
bool isSupportedVideoFormat(File file) =>
|
||||||
isSupportedFormat(file) && file.contentType?.startsWith("video/") == true;
|
isSupportedFormat(file) && file.contentType?.startsWith("video/") == true;
|
||||||
|
|
||||||
|
bool isMetadataSupportedMime(String mime) =>
|
||||||
|
_metadataSupportedFormatMimes.contains(mime);
|
||||||
|
|
||||||
bool isMetadataSupportedFormat(File file) =>
|
bool isMetadataSupportedFormat(File file) =>
|
||||||
_metadataSupportedFormatMimes.contains(file.contentType);
|
isMetadataSupportedMime(file.contentType ?? "");
|
||||||
|
|
||||||
bool isTrash(Account account, File file) =>
|
bool isTrash(Account account, File file) =>
|
||||||
file.path.startsWith(api_util.getTrashbinPath(account));
|
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:logging/logging.dart';
|
||||||
import 'package:nc_photos/account.dart';
|
import 'package:nc_photos/account.dart';
|
||||||
import 'package:nc_photos/debug_util.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.dart';
|
||||||
import 'package:nc_photos/entity/file_util.dart' as file_util;
|
import 'package:nc_photos/entity/file_util.dart' as file_util;
|
||||||
import 'package:nc_photos/image_size_getter_util.dart';
|
import 'package:nc_photos/image_size_getter_util.dart';
|
||||||
|
|
||||||
class LoadMetadata {
|
class LoadMetadata {
|
||||||
/// Load metadata of [binary], which is the content of [file]
|
/// Load metadata of [binary], which is the content of [file]
|
||||||
Future<Map<String, dynamic>> call(
|
Future<Metadata> loadRemote(Account account, File file, Uint8List binary) {
|
||||||
Account account, File file, Uint8List binary) {
|
|
||||||
return _loadMetadata(
|
return _loadMetadata(
|
||||||
file: file,
|
mime: file.contentType ?? "",
|
||||||
exifdartReaderBuilder: () => MemoryBlobReader(binary),
|
exifdartReaderBuilder: () => MemoryBlobReader(binary),
|
||||||
imageSizeGetterInputBuilder: () => AsyncMemoryInput(binary),
|
imageSizeGetterInputBuilder: () => AsyncMemoryInput(binary),
|
||||||
|
filename: file.path,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Map<String, dynamic>> _loadMetadata({
|
Future<Metadata> _loadMetadata({
|
||||||
required File file,
|
required String mime,
|
||||||
required exifdart.AbstractBlobReader Function() exifdartReaderBuilder,
|
required exifdart.AbstractBlobReader Function() exifdartReaderBuilder,
|
||||||
required AsyncImageInput Function() imageSizeGetterInputBuilder,
|
required AsyncImageInput Function() imageSizeGetterInputBuilder,
|
||||||
|
String? filename,
|
||||||
}) async {
|
}) async {
|
||||||
var metadata = exifdart.Metadata();
|
var metadata = exifdart.Metadata();
|
||||||
if (file_util.isMetadataSupportedFormat(file)) {
|
if (file_util.isMetadataSupportedMime(mime)) {
|
||||||
try {
|
try {
|
||||||
metadata = await exifdart.readMetadata(exifdartReaderBuilder());
|
metadata = await exifdart.readMetadata(exifdartReaderBuilder());
|
||||||
} catch (e, stacktrace) {
|
} catch (e, stacktrace) {
|
||||||
_log.shout(
|
_log.shout(
|
||||||
"[_loadMetadata] Failed while readMetadata for ${file.contentType} file: ${logFilename(file.path)}",
|
"[_loadMetadata] Failed while readMetadata for $mime file: ${logFilename(filename)}",
|
||||||
e,
|
e,
|
||||||
stacktrace);
|
stacktrace);
|
||||||
// ignore exif
|
// ignore exif
|
||||||
|
@ -58,7 +60,7 @@ class LoadMetadata {
|
||||||
} catch (e, stacktrace) {
|
} catch (e, stacktrace) {
|
||||||
// is this even an image file?
|
// is this even an image file?
|
||||||
_log.shout(
|
_log.shout(
|
||||||
"[_loadMetadata] Failed while getSize for ${file.contentType} file: ${logFilename(file.path)}",
|
"[_loadMetadata] Failed while getSize for $mime file: ${logFilename(filename)}",
|
||||||
e,
|
e,
|
||||||
stacktrace);
|
stacktrace);
|
||||||
}
|
}
|
||||||
|
@ -73,7 +75,7 @@ class LoadMetadata {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
final map = {
|
||||||
if (metadata.exif != null) "exif": metadata.exif,
|
if (metadata.exif != null) "exif": metadata.exif,
|
||||||
if (imageWidth > 0 && imageHeight > 0)
|
if (imageWidth > 0 && imageHeight > 0)
|
||||||
"resolution": {
|
"resolution": {
|
||||||
|
@ -81,6 +83,24 @@ class LoadMetadata {
|
||||||
"height": imageHeight,
|
"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");
|
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:logging/logging.dart';
|
||||||
import 'package:nc_photos/account.dart';
|
import 'package:nc_photos/account.dart';
|
||||||
import 'package:nc_photos/connectivity_util.dart' as connectivity_util;
|
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/entity/file.dart';
|
||||||
import 'package:nc_photos/event/event.dart';
|
import 'package:nc_photos/event/event.dart';
|
||||||
import 'package:nc_photos/exception.dart';
|
import 'package:nc_photos/exception.dart';
|
||||||
|
@ -62,27 +61,15 @@ class UpdateMissingMetadata {
|
||||||
}
|
}
|
||||||
_log.fine("[call] Updating metadata for ${file.path}");
|
_log.fine("[call] Updating metadata for ${file.path}");
|
||||||
final binary = await GetFileBinary(fileRepo)(account, file);
|
final binary = await GetFileBinary(fileRepo)(account, file);
|
||||||
final metadata = await LoadMetadata()(account, file, binary);
|
final metadata =
|
||||||
int? imageWidth, imageHeight;
|
(await LoadMetadata().loadRemote(account, file, binary)).copyWith(
|
||||||
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(
|
|
||||||
fileEtag: file.etag,
|
fileEtag: file.etag,
|
||||||
imageWidth: imageWidth,
|
|
||||||
imageHeight: imageHeight,
|
|
||||||
exif: exif,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
await UpdateProperty(fileRepo)(
|
await UpdateProperty(fileRepo)(
|
||||||
account,
|
account,
|
||||||
file,
|
file,
|
||||||
metadata: OrNull(metadataObj),
|
metadata: OrNull(metadata),
|
||||||
);
|
);
|
||||||
yield file;
|
yield file;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue