diff --git a/lib/date_extractors/exif_extractor.dart b/lib/date_extractors/exif_extractor.dart index 2816dc7c..4eae4dec 100644 --- a/lib/date_extractors/exif_extractor.dart +++ b/lib/date_extractors/exif_extractor.dart @@ -2,11 +2,18 @@ import 'dart:io'; import 'dart:math'; import 'package:exif/exif.dart'; +import 'package:gpth/utils.dart'; +import 'package:mime/mime.dart'; /// DateTime from exif data *potentially* hidden within a [file] /// /// You can try this with *any* file, it either works or not 🤷 Future exifExtractor(File file) async { + // if file is not image or >32MiB - DO NOT crash :D + if (!(lookupMimeType(file.path)?.startsWith('image/') ?? false) || + await file.length() > maxFileSize) { + return null; + } final bytes = await file.readAsBytes(); // this returns empty {} if file doesn't have exif so don't worry final tags = await readExifFromBytes(bytes); diff --git a/lib/media.dart b/lib/media.dart index 56bacf03..42c205f2 100644 --- a/lib/media.dart +++ b/lib/media.dart @@ -1,6 +1,7 @@ import 'dart:io'; import 'package:crypto/crypto.dart'; +import 'package:gpth/utils.dart'; /// Abstract of a *media* - a photo or video /// Main thing is the [file] - this should not change @@ -34,7 +35,10 @@ class Media { Digest? _hash; /// will be used for finding duplicates/albums - Digest get hash => _hash ??= sha256.convert(file.readAsBytesSync()); + /// WARNING: Returns same value for files > [maxFileSize] + Digest get hash => _hash ??= file.lengthSync() > maxFileSize + ? Digest([0]) + : sha256.convert(file.readAsBytesSync()); Media( this.file, { diff --git a/lib/utils.dart b/lib/utils.dart index 16687e44..d4e6669c 100644 --- a/lib/utils.dart +++ b/lib/utils.dart @@ -9,6 +9,9 @@ import 'package:proper_filesize/proper_filesize.dart'; // remember to bump this const version = '3.3.2'; +/// max file size to read for exif/hash/anything +const maxFileSize = 64 * 1024 * 1024; + /// convenient print for errors void error(Object? object) => stderr.write('$object\n');