todo-app/lib/images/attachment_links.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

66 lines
2.3 KiB
Dart

/// Finding attachment links in a note's text, and rewriting them for
/// `BACKLOG.md`.
///
/// One regex reads both shapes: `![](url)` is an image, `[name](url)` a file.
/// Matching them together (rather than a file regex beside an image regex)
/// is what keeps an image link, which contains `[](url)`, from also counting
/// as a file.
///
/// Pure Dart: the desktop wrapper imports it.
library;
import 'package:todo/images/attachment_ref.dart';
final String _base = RegExp.escape('$kDufsBaseUrl/$kImagesDir/');
const _label = r'\[[^\]\n]*\]\(';
const _link = r'(!?)\[([^\]\n]*)\]\(';
final _anyLink = RegExp('$_link$_base($kAttachmentPathPattern)\\)');
final _wholeLine = RegExp('^\\s*${_anyLink.pattern}\\s*\$');
final _urlLink = RegExp('($_label)$_base($kAttachmentPathPattern\\))');
final _backlogLink = RegExp(
'($_label)${RegExp.escape('$kBacklogImagesRoot/')}'
'($kAttachmentPathPattern\\))',
);
Attachment _attachment(RegExpMatch match) => Attachment(
ref: AttachmentRef.tryParse(match.group(3)!)!,
name: match.group(2)!,
isImage: match.group(1)!.isNotEmpty,
);
/// Every attachment [text] links to, in order, each ref once.
List<Attachment> attachmentsIn(String text) {
final seen = <AttachmentRef>{};
final found = <Attachment>[];
for (final match in _anyLink.allMatches(text)) {
final attachment = _attachment(match);
if (seen.add(attachment.ref)) found.add(attachment);
}
return found;
}
/// Every ref [text] links to, images and files alike.
Set<AttachmentRef> attachmentRefsIn(String text) => {
for (final attachment in attachmentsIn(text)) attachment.ref,
};
/// The attachment when [line] is nothing but one attachment link, else null.
Attachment? attachmentOfLine(String line) {
final match = _wholeLine.firstMatch(line);
return match == null ? null : _attachment(match);
}
/// Rewrites attachment links to their on-disk path under
/// [kBacklogImagesRoot].
String toBacklogAttachmentPaths(String text) => text.replaceAllMapped(
_urlLink,
(m) => '${m[1]}$kBacklogImagesRoot/${m[2]}',
);
/// Exact inverse of [toBacklogAttachmentPaths], so an exported backlog
/// imports back with its links intact.
String fromBacklogAttachmentPaths(String text) => text.replaceAllMapped(
_backlogLink,
(m) => '${m[1]}$kDufsBaseUrl/$kImagesDir/${m[2]}',
);