Fix exception when loadind metadata for png/webp

This commit is contained in:
Ming Ming 2021-05-07 04:11:31 +08:00
parent 9d2e1798f7
commit 5ad3254634
4 changed files with 26 additions and 8 deletions

View file

@ -10,6 +10,9 @@ 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 isMetadataSupportedFormat(File file) =>
_metadataSupportedFormatMimes.contains(file.contentType);
const _supportedFormatMimes = [ const _supportedFormatMimes = [
"image/jpeg", "image/jpeg",
"image/png", "image/png",
@ -18,3 +21,8 @@ const _supportedFormatMimes = [
// video player currently doesn't work on web // video player currently doesn't work on web
if (!platform_k.isWeb) "video/mp4", if (!platform_k.isWeb) "video/mp4",
]; ];
const _metadataSupportedFormatMimes = [
"image/jpeg",
"image/heic",
];

View file

@ -23,7 +23,7 @@ class MetadataLoader implements itf.MetadataLoader {
Future.delayed(Duration(seconds: 10)), Future.delayed(Duration(seconds: 10)),
]); ]);
if (_getFileTask.isGood && result is FileInfo) { if (_getFileTask.isGood && result is FileInfo) {
return _onGetFile(result); return _onGetFile(file, result);
} else { } else {
// timeout // timeout
_getFileTask.cancel(); _getFileTask.cancel();
@ -42,6 +42,7 @@ class MetadataLoader implements itf.MetadataLoader {
message: "Failed communicating with server: ${response.statusCode}"); message: "Failed communicating with server: ${response.statusCode}");
} }
return itf.MetadataLoader.loadMetadata( return itf.MetadataLoader.loadMetadata(
file: file,
exifdartReaderBuilder: () => MemoryBlobReader(response.body), exifdartReaderBuilder: () => MemoryBlobReader(response.body),
imageSizeGetterInputBuilder: () => AsyncMemoryInput(response.body), imageSizeGetterInputBuilder: () => AsyncMemoryInput(response.body),
); );
@ -55,7 +56,7 @@ class MetadataLoader implements itf.MetadataLoader {
// no cache // no cache
return loadNewFile(account, file); return loadNewFile(account, file);
} else { } else {
return _onGetFile(info); return _onGetFile(file, info);
} }
} }
@ -64,10 +65,11 @@ class MetadataLoader implements itf.MetadataLoader {
_getFileTask.cancel(); _getFileTask.cancel();
} }
Future<Map<String, dynamic>> _onGetFile(FileInfo f) { Future<Map<String, dynamic>> _onGetFile(File file, FileInfo info) {
return itf.MetadataLoader.loadMetadata( return itf.MetadataLoader.loadMetadata(
exifdartReaderBuilder: () => FileReader(f.file), file: file,
imageSizeGetterInputBuilder: () => AsyncFileInput(f.file), exifdartReaderBuilder: () => FileReader(info.file),
imageSizeGetterInputBuilder: () => AsyncFileInput(info.file),
); );
} }

View file

@ -1,8 +1,9 @@
import 'package:exifdart/exifdart.dart'; import 'package:exifdart/exifdart.dart' as exifdart;
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:image_size_getter/image_size_getter.dart'; import 'package:image_size_getter/image_size_getter.dart';
import 'package:nc_photos/account.dart'; import 'package:nc_photos/account.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;
abstract class MetadataLoader { abstract class MetadataLoader {
/// Load metadata for [file] from cache /// Load metadata for [file] from cache
@ -24,10 +25,16 @@ abstract class MetadataLoader {
@protected @protected
static Future<Map<String, dynamic>> loadMetadata({ static Future<Map<String, dynamic>> loadMetadata({
AbstractBlobReader Function() exifdartReaderBuilder, @required File file,
exifdart.AbstractBlobReader Function() exifdartReaderBuilder,
AsyncImageInput Function() imageSizeGetterInputBuilder, AsyncImageInput Function() imageSizeGetterInputBuilder,
}) async { }) async {
final metadata = await readMetadata(exifdartReaderBuilder()); exifdart.Metadata metadata;
if (file_util.isMetadataSupportedFormat(file)) {
metadata = await exifdart.readMetadata(exifdartReaderBuilder());
} else {
metadata = exifdart.Metadata();
}
int imageWidth, imageHeight; int imageWidth, imageHeight;
if (metadata.imageWidth == null || metadata.imageHeight == null) { if (metadata.imageWidth == null || metadata.imageHeight == null) {
final resolution = final resolution =

View file

@ -27,6 +27,7 @@ class MetadataLoader implements itf.MetadataLoader {
message: "Failed communicating with server: ${response.statusCode}"); message: "Failed communicating with server: ${response.statusCode}");
} }
return itf.MetadataLoader.loadMetadata( return itf.MetadataLoader.loadMetadata(
file: file,
exifdartReaderBuilder: () => MemoryBlobReader(response.body), exifdartReaderBuilder: () => MemoryBlobReader(response.body),
imageSizeGetterInputBuilder: () => AsyncMemoryInput(response.body), imageSizeGetterInputBuilder: () => AsyncMemoryInput(response.body),
); );