todo-app/lib/images/attachment_ingest.dart
Krzysztof kuhy Rudnicki 9f2ffdb8a9
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
file-length / file length <= 250 lines (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
Attach any file to a note, of any size, not only images
📎 now picks any file (Android: Camera / Gallery / File; desktop: an
unfiltered dialog) and the desktop page accepts any dropped file. Photo and
screenshot formats up to 64 MB are still re-encoded and shown inline;
everything else is stored byte for byte on dufs beside the images and linked
as [name](url), shown as a row with its size that opens in the platform's
app (Android) or a new browser tab (desktop). BACKLOG.md rewrites file links
to ~/data/cloud/todo-images/ like image links.

There is no size cap, so no layer holds a file in memory: uploads and
downloads stream through DufsImageClient, writes land in a .tmp renamed when
whole (an interrupted download no longer leaves a truncated file that looks
done), the desktop page POSTs the browser's own Blob with fetch, and Android's
"File" uses file_picker, because file_selector's Android side reads the whole
file into memory on the UI thread. Verified with a 1 GB file end to end on
desktop (peak RSS +36 MB) and on an API 36 emulator.

The wrapper serves attachments from the origin that holds the GitHub token
and the notes, so only script-free types open inline (nosniff), the rest
download; writes carrying another site's Origin are refused (this also
closes cross-site overwrites of the backup files); and attachment routes run
concurrently so a long upload or a playing video cannot stall the page.

Also: cupertino_icons 2.0.0 (unused, but the freshness gate blocks the
pubspec otherwise), open_file 4.0.0 and file_picker 13.1.0 pinned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 12:17:06 +02:00

113 lines
3.6 KiB
Dart

/// Turning an incoming file into a stored attachment: re-encoded as an image
/// when it is one, kept byte-for-byte otherwise.
///
/// The one place that decides, shared by the Android store and the desktop
/// wrapper. The file lands on disk first and is classified from there, so its
/// size never reaches memory: only something that sniffs as a photo format
/// (see `looksLikeImage`) and is at most [kMaxImageDecodeBytes] is read whole,
/// to be decoded. An image that fails to decode is kept as a file rather than
/// refused.
///
/// Pure Dart + `dart:io`.
library;
import 'dart:io';
import 'dart:typed_data';
import 'package:todo/images/attachment_ref.dart';
import 'package:todo/images/image_cache_store.dart';
import 'package:todo/images/image_processing.dart';
/// Stores incoming files in [store], queued for upload.
class AttachmentIngest {
/// Creates an ingest over [store]; [prepare] is injectable for tests.
AttachmentIngest(
this.store, {
Future<Uint8List?> Function(Uint8List)? prepare,
}) : _prepare = prepare ?? prepareImageInBackground;
/// Where attachments are kept.
final ImageCacheStore store;
final Future<Uint8List?> Function(Uint8List) _prepare;
/// Stores [data], the contents of a file called [name], for [noteId].
Future<Attachment> fromStream(
String noteId,
String name,
Stream<List<int>> data,
) async {
final ref = AttachmentRef.forFile(noteId, name);
final tmp = store.tempFor(ref);
await tmp.parent.create(recursive: true);
return await _guarded(tmp, () async {
final sink = tmp.openWrite();
try {
await sink.addStream(data);
} finally {
await sink.close();
}
return await _settle(ref, name, tmp);
});
}
/// Stores the local file [source], called [name], for [noteId].
///
/// With [move] (a picker's private copy, nobody else's file) it is renamed
/// into place, so a multi-gigabyte pick is never on disk twice; otherwise,
/// or across filesystems, it is copied.
Future<Attachment> fromFile(
String noteId,
String name,
File source, {
required bool move,
}) async {
if (!move) return await fromStream(noteId, name, source.openRead());
final ref = AttachmentRef.forFile(noteId, name);
final tmp = store.tempFor(ref);
await tmp.parent.create(recursive: true);
try {
await source.rename(tmp.path);
} on FileSystemException {
final attachment = await fromStream(noteId, name, source.openRead());
await source.delete();
return attachment;
}
return await _guarded(tmp, () => _settle(ref, name, tmp));
}
/// Runs [body], removing [tmp] if it fails so a failed attach leaves
/// nothing behind.
Future<Attachment> _guarded(
File tmp,
Future<Attachment> Function() body,
) async {
try {
return await body();
} on Object {
if (tmp.existsSync()) await tmp.delete();
rethrow;
}
}
Future<Attachment> _settle(AttachmentRef ref, String name, File tmp) async {
if (await tmp.length() <= kMaxImageDecodeBytes && await _sniff(tmp)) {
final jpeg = await _prepare(await tmp.readAsBytes());
if (jpeg != null) {
final image = AttachmentRef.create(ref.noteId);
await store.put(image, jpeg);
await tmp.delete();
return Attachment(ref: image, name: name, isImage: true);
}
}
await store.adopt(ref, tmp);
return Attachment(ref: ref, name: name, isImage: false);
}
Future<bool> _sniff(File file) async {
final head = <int>[];
await file.openRead(0, kImageSniffBytes).forEach(head.addAll);
return looksLikeImage(head);
}
}