Skip to content

Commit e0d9ee3

Browse files
committed
fix memory crashes :D
1 parent 38ea053 commit e0d9ee3

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

lib/date_extractors/exif_extractor.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ import 'dart:io';
22
import 'dart:math';
33

44
import 'package:exif/exif.dart';
5+
import 'package:gpth/utils.dart';
6+
import 'package:mime/mime.dart';
57

68
/// DateTime from exif data *potentially* hidden within a [file]
79
///
810
/// You can try this with *any* file, it either works or not 🤷
911
Future<DateTime?> exifExtractor(File file) async {
12+
// if file is not image or >32MiB - DO NOT crash :D
13+
if (!(lookupMimeType(file.path)?.startsWith('image/') ?? false) ||
14+
await file.length() > maxFileSize) {
15+
return null;
16+
}
1017
final bytes = await file.readAsBytes();
1118
// this returns empty {} if file doesn't have exif so don't worry
1219
final tags = await readExifFromBytes(bytes);

lib/media.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:io';
22

33
import 'package:crypto/crypto.dart';
4+
import 'package:gpth/utils.dart';
45

56
/// Abstract of a *media* - a photo or video
67
/// Main thing is the [file] - this should not change
@@ -34,7 +35,10 @@ class Media {
3435
Digest? _hash;
3536

3637
/// will be used for finding duplicates/albums
37-
Digest get hash => _hash ??= sha256.convert(file.readAsBytesSync());
38+
/// WARNING: Returns same value for files > [maxFileSize]
39+
Digest get hash => _hash ??= file.lengthSync() > maxFileSize
40+
? Digest([0])
41+
: sha256.convert(file.readAsBytesSync());
3842

3943
Media(
4044
this.file, {

lib/utils.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import 'package:proper_filesize/proper_filesize.dart';
99
// remember to bump this
1010
const version = '3.3.2';
1111

12+
/// max file size to read for exif/hash/anything
13+
const maxFileSize = 64 * 1024 * 1024;
14+
1215
/// convenient print for errors
1316
void error(Object? object) => stderr.write('$object\n');
1417

0 commit comments

Comments
 (0)