todo-app/test/desktop/wrapper_attachment_serving_test.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

79 lines
2.4 KiB
Dart

import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:todo/desktop/wrapper_attachment_serving.dart';
void main() {
test('script-free types are inline, everything else downloads', () {
const inline = {
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'png': 'image/png',
'gif': 'image/gif',
'webp': 'image/webp',
'bmp': 'image/bmp',
'pdf': 'application/pdf',
'txt': 'text/plain',
'md': 'text/plain',
'log': 'text/plain',
'csv': 'text/plain',
'mp4': 'video/mp4',
'm4v': 'video/mp4',
'webm': 'video/webm',
'ogv': 'video/ogg',
'mov': 'video/quicktime',
'mp3': 'audio/mpeg',
'm4a': 'audio/mp4',
'ogg': 'audio/ogg',
'oga': 'audio/ogg',
'opus': 'audio/ogg',
'wav': 'audio/wav',
'flac': 'audio/flac',
};
inline.forEach((extension, mime) {
final kind = attachmentContentType(extension);
expect(kind.type.mimeType, mime, reason: extension);
expect(kind.inline, isTrue, reason: extension);
});
expect(attachmentContentType('txt').type.charset, 'utf-8');
for (final risky in ['html', 'svg', 'xhtml', 'js', 'bin', 'exe']) {
final kind = attachmentContentType(risky);
expect(kind.type, ContentType.binary, reason: risky);
expect(kind.inline, isFalse, reason: risky);
}
});
test('the disposition carries any name, RFC 5987 encoded', () {
expect(
contentDisposition('Raport (v2) zażółć*.pdf', inline: true),
"inline; filename*=UTF-8''Raport%20%28v2%29%20za%C5%BC%C3%B3%C5%82%C4%87%2A.pdf",
);
expect(
contentDisposition("it's.html", inline: false),
"attachment; filename*=UTF-8''it%27s.html",
);
});
test('parseByteRange reads one range within the file', () {
expect(parseByteRange('bytes=0-9', 100), (0, 9));
expect(parseByteRange('bytes=5-', 10), (5, 9));
expect(parseByteRange('bytes=5-100', 10), (5, 9));
expect(parseByteRange('bytes=-3', 10), (7, 9));
expect(parseByteRange('bytes=-100', 10), (0, 9));
expect(parseByteRange(' bytes=1-1 ', 10), (1, 1));
});
test('parseByteRange refuses what it cannot serve', () {
for (final header in [
'bytes=10-',
'bytes=5-2',
'bytes=-0',
'bytes=-',
'bytes=0-1,4-5',
'items=0-1',
]) {
expect(parseByteRange(header, 10), isNull, reason: header);
}
});
}