Files backend: release-name parser, video-only scanner, folder store, mpv thumbnails

lib/src/files/ for the 1.0 Files source (UI comes with the library
round; ARCHITECTURE gets its section then):
- parseReleaseName: series/season/episode/group/year/extras from release
  names (fansub, scene, batch, NCOP/OVA markers); seriesKey for grouping.
- scanFolder (isolate): videos only, samples/trailers and other files
  counted as hidden, sidecar subtitles paired by name (+ language/flag
  suffixes), 20 000-file cap; groupBySeries -> series + loose videos.
- FolderStore: watched folders and recent files in Hive, as signals.
- thumbnailFor: a 480 px JPEG at 25% via a hidden mpv player
  (screenshot-to-file, vo=null), cached by path+size+mtime, 2 at a time,
  cancellable, never throws. No ffmpeg needed (Windows ships only libmpv).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
foxyzandClaude Opus 5.5 committed 2026-10-08 23:46:01 +03:00
1 parent 6f136bfc4d
commit c76fee70dc
8 files changed
+1181

No files matched your search

+150
View File
@@ -0,0 +1,150 @@
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:watch_together/src/files/name_parser.dart';
import 'package:watch_together/src/files/scanner.dart';
late Directory _root;
String _p(String rel) => '${_root.path}/$rel';
void _file(String rel, {int size = 10}) {
final f = File(_p(rel))..createSync(recursive: true);
if (size > 0) {
final raf = f.openSync(mode: FileMode.write);
raf.truncateSync(size);
raf.closeSync();
}
}
VideoFile _v(String name) =>
VideoFile('/x/$name', 1, parseReleaseName(name), DateTime(2020));
void main() {
setUp(() => _root = Directory.systemTemp.createTempSync('files_scan_'));
tearDown(() => _root.deleteSync(recursive: true));
test('classifies, skips hidden and samples, pairs sidecar subtitles',
() async {
_file('Show/[Grp] Show - 01 [1080p].mkv');
_file('Show/[Grp] Show - 01 [1080p].ass');
_file('Show/[Grp] Show - 02 [1080p].mkv');
_file('Show/[Grp] Show - 02 [1080p].en.srt');
_file('Show/[Grp] Show - 02 [1080p].en.forced.srt');
_file('Show/unrelated.srt');
_file('Show/sample-show.mkv');
_file('Show/info.nfo');
_file('Show/cover.jpg');
_file('Movie.2020.1080p.BluRay.mp4');
_file('Movie.2020.1080p.BluRay.nfo');
_file('.hidden.mkv');
_file('.cache/inside.mkv');
_file('deep/a/b/c/clip.webm');
_file('Trailer big.mkv', size: 301 << 20); // sparse
Link(_p('link-to-show')).createSync(_p('Show'));
Link(_p('link.mkv')).createSync(_p('Movie.2020.1080p.BluRay.mp4'));
final scan = await scanFolder(_root.path);
final names = scan.videos.map((v) => v.name).toList()..sort();
expect(names, [
'Movie.2020.1080p.BluRay.mp4',
'Trailer big.mkv',
'[Grp] Show - 01 [1080p].mkv',
'[Grp] Show - 02 [1080p].mkv',
'clip.webm',
]);
expect(scan.hiddenCount, 4); // sample, nfo, jpg, nfo
expect(scan.hiddenExamples, hasLength(4));
expect(scan.hiddenExamples, contains('sample-show.mkv'));
expect(scan.truncated, isFalse);
expect(scan.subtitles[_p('Show/[Grp] Show - 01 [1080p].mkv')],
[_p('Show/[Grp] Show - 01 [1080p].ass')]);
expect(scan.subtitles[_p('Show/[Grp] Show - 02 [1080p].mkv')], [
_p('Show/[Grp] Show - 02 [1080p].en.forced.srt'),
_p('Show/[Grp] Show - 02 [1080p].en.srt'),
]);
expect(scan.subtitles.containsKey(_p('Movie.2020.1080p.BluRay.mp4')),
isFalse);
final ep = scan.videos.firstWhere((v) => v.name.contains('- 02'));
expect(ep.parsed.episode, 2);
expect(ep.size, 10);
expect(ep.modified.year, DateTime.now().year);
});
test('only language/flag suffixes pair a subtitle with a shorter name',
() async {
_file('Show.mkv');
_file('Show.pt-BR.srt');
_file('Show.S01E01.srt');
final scan = await scanFolder(_root.path);
expect(scan.subtitles[_p('Show.mkv')], [_p('Show.pt-BR.srt')]);
});
test('stops after the file limit and says so', () async {
for (var i = 0; i < 30; i++) {
_file('f$i.mkv', size: 0);
}
final scan = await scanFolder(_root.path, maxFiles: 12);
expect(scan.truncated, isTrue);
expect(scan.videos, hasLength(12));
});
test('hidden examples stop at 6', () async {
for (var i = 0; i < 9; i++) {
_file('note$i.txt', size: 0);
}
final scan = await scanFolder(_root.path);
expect(scan.hiddenCount, 9);
expect(scan.hiddenExamples, hasLength(6));
});
test('a missing folder is an empty scan', () async {
final scan = await scanFolder(_p('nope'));
expect(scan.videos, isEmpty);
expect(scan.truncated, isFalse);
});
test('kindOf', () {
expect(kindOf('/a/b.MKV'), FileKind.video);
expect(kindOf('b.m2ts'), FileKind.video);
expect(kindOf('b.SUP'), FileKind.subtitle);
expect(kindOf('b.nfo'), FileKind.other);
expect(kindOf('mkv'), FileKind.other);
});
group('groupBySeries', () {
test('groups across spellings, sorts by season then episode', () {
final g = groupBySeries([
_v('[A] Re:Zero S2 - 02 [1080p].mkv'),
_v('[A] Re:Zero - 10 [1080p].mkv'),
_v('[B] Re Zero - 02 [720p].mkv'),
_v('[A] Re:Zero S2 - 01 [1080p].mkv'),
_v('[A] Re:Zero - NCOP1 [1080p].mkv'),
_v('Other Show - 03.mkv'),
_v('Other Show - 01.mkv'),
_v('Movie.2020.1080p.mkv'),
_v('vid_2024_final.mp4'),
_v('[A] Lonely - OVA.mkv'),
]);
expect(g.series.map((s) => s.title), ['Other Show', 'Re:Zero']);
final re = g.series[1];
expect(re.episodes.map((v) => v.parsed.label),
['E02', 'E10', 'S02E01', 'S02E02']);
expect(re.seasons, [1, 2]);
expect(re.extras.single.parsed.extra, 'NCOP1');
expect(g.series[0].episodes.map((v) => v.parsed.episode), [1, 3]);
expect(g.loose.map((v) => v.name), [
'[A] Lonely - OVA.mkv',
'Movie.2020.1080p.mkv',
'vid_2024_final.mp4',
]);
});
test('empty input', () {
final g = groupBySeries(const []);
expect(g.series, isEmpty);
expect(g.loose, isEmpty);
});
});
}
+48
View File
@@ -0,0 +1,48 @@
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:hive_ce/hive.dart';
import 'package:watch_together/src/files/folder_store.dart';
void main() {
test('folders and recent files persist across a reload', () async {
final dir = Directory.systemTemp.createTempSync('files_store_');
addTearDown(() => dir.deleteSync(recursive: true));
Hive.init(dir.path);
final a = FolderStore();
await a.ensureLoaded();
expect(a.folders.value, isEmpty);
expect(a.recent.value, isEmpty);
expect(a.addFolder('/media/anime'), isTrue);
expect(a.addFolder('/media/movies'), isTrue);
expect(a.addFolder('/media/anime'), isFalse);
a.removeFolder('/media/movies');
a.addFolder('/media/films');
for (var i = 0; i < 35; i++) {
a.addRecent('/v/$i.mkv');
}
a.addRecent('/v/10.mkv'); // back to the top, not duplicated
a.removeRecent('/v/33.mkv');
expect(a.recent.value.first, '/v/10.mkv');
expect(a.recent.value, hasLength(maxRecentFiles - 1));
expect(a.recent.value.where((p) => p == '/v/10.mkv'), hasLength(1));
await Future.delayed(const Duration(milliseconds: 100));
await Hive.close();
final b = FolderStore();
await b.ensureLoaded();
expect(b.folders.value, ['/media/anime', '/media/films']);
expect(b.recent.value, a.recent.value);
expect(b.recent.value.take(3), ['/v/10.mkv', '/v/34.mkv', '/v/32.mkv']);
b.clearRecent();
await Future.delayed(const Duration(milliseconds: 100));
await Hive.close();
final c = FolderStore();
await c.ensureLoaded();
expect(c.recent.value, isEmpty);
expect(c.folders.value, hasLength(2));
await Hive.close();
});
}
@@ -0,0 +1,96 @@
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:image/image.dart' as img;
import 'package:media_kit/media_kit.dart';
import 'package:watch_together/src/files/thumbnails.dart';
// Real libmpv: frames grabbed with vo=null + screenshot-to-file. The test
// media comes from ffmpeg (this machine only; the app never needs it).
late Directory _tmp;
/// 8 s, 640x360: red until 1.5 s, green until 4 s, then blue; a keyframe
/// every 0.5 s, so the 25% frame (2 s) is green.
Future<String> _media(String name) async {
final path = '${_tmp.path}/$name';
final r = await Process.run('ffmpeg', [
'-y', '-v', 'error',
'-f', 'lavfi', '-i', 'color=c=red:size=640x360:rate=24:duration=1.5',
'-f', 'lavfi', '-i', 'color=c=green:size=640x360:rate=24:duration=2.5',
'-f', 'lavfi', '-i', 'color=c=blue:size=640x360:rate=24:duration=4',
'-filter_complex', '[0][1][2]concat=n=3:v=1:a=0',
'-c:v', 'libx264', '-preset', 'ultrafast', '-g', '12', '-pix_fmt', 'yuv420p',
path,
]);
if (r.exitCode != 0) fail('ffmpeg failed: ${r.stderr}');
return path;
}
void main() {
setUpAll(() {
MediaKit.ensureInitialized();
_tmp = Directory.systemTemp.createTempSync('files_thumbs_');
thumbnailCacheRoot = () async => Directory('${_tmp.path}/cache');
});
tearDownAll(() => _tmp.deleteSync(recursive: true));
test('a JPEG of the 25% frame plus the duration, then from the cache',
timeout: const Timeout(Duration(minutes: 2)), () async {
final path = await _media('a.mkv');
final sw = Stopwatch()..start();
final t = await thumbnailFor(path);
final first = sw.elapsedMilliseconds;
expect(t, isNotNull);
expect(t!.duration.inMilliseconds, closeTo(8000, 100));
final bytes = File(t.imagePath).readAsBytesSync();
expect(bytes.sublist(0, 2), [0xFF, 0xD8]); // JPEG
final image = img.decodeJpg(bytes)!;
expect(image.width, thumbnailWidth);
expect(image.height, 270);
final px = image.getPixel(image.width ~/ 2, image.height ~/ 2);
expect(px.g, greaterThan(100), reason: 'green frame, got $px');
expect(px.r, lessThan(60));
expect(px.b, lessThan(60));
sw.reset();
final again = await thumbnailFor(path);
final cachedMs = sw.elapsedMilliseconds;
expect(again?.imagePath, t.imagePath);
expect(again?.duration, t.duration);
// ignore: avoid_print
print('thumbnail: first $first ms, cached $cachedMs ms');
expect(cachedMs, lessThan(first));
});
test('several at once (2 at a time), same file deduplicated',
timeout: const Timeout(Duration(minutes: 2)), () async {
final paths = [for (var i = 0; i < 4; i++) await _media('m$i.mp4')];
final sw = Stopwatch()..start();
final results = await Future.wait([
for (final p in paths) thumbnailFor(p),
thumbnailFor(paths.first),
]);
// ignore: avoid_print
print('4 thumbnails in ${sw.elapsedMilliseconds} ms');
expect(results.every((t) => t != null), isTrue);
expect(results.first!.imagePath, results.last!.imagePath);
});
test('not a video, missing, cancelled: null, never a throw',
timeout: const Timeout(Duration(minutes: 1)), () async {
final text = File('${_tmp.path}/notes.mkv')..writeAsStringSync('hello');
expect(await thumbnailFor(text.path), isNull);
// The failure is remembered: the second answer comes from the cache.
final sw = Stopwatch()..start();
expect(await thumbnailFor(text.path), isNull);
expect(sw.elapsedMilliseconds, lessThan(500));
expect(await thumbnailFor('${_tmp.path}/missing.mkv'), isNull);
final path = await _media('c.mkv');
expect(await thumbnailFor(path, cancelled: () => true), isNull);
// A cancellation is not a remembered failure.
expect(await thumbnailFor(path), isNotNull);
});
}
+169
View File
@@ -0,0 +1,169 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:watch_together/src/files/name_parser.dart';
void _expect(
String name,
String series, {
int? season,
int? episode,
String? group,
int? year,
String? extra,
String? label,
}) {
final p = parseReleaseName(name);
final why = '$name -> $p';
expect(p.series, series, reason: why);
expect(p.season, season, reason: why);
expect(p.episode, episode, reason: why);
expect(p.group, group, reason: why);
expect(p.year, year, reason: why);
expect(p.extra, extra, reason: why);
if (label != null) expect(p.label, label, reason: why);
}
void main() {
group('anime', () {
test('SubsPlease dash number', () {
_expect('[SubsPlease] Sousou no Frieren - 25 (1080p) [A1B2C3D4].mkv',
'Sousou no Frieren',
episode: 25, group: 'SubsPlease', label: 'E25');
});
test('Erai-raws Nth Season', () {
_expect(
'[Erai-raws] Kusuriya no Hitorigoto 2nd Season - 04 [1080p][Multiple Subtitle].mkv',
'Kusuriya no Hitorigoto',
season: 2, episode: 4, group: 'Erai-raws', label: 'S02E04');
});
test('four-digit episode', () {
_expect('One Piece - 1120 [1080p].mkv', 'One Piece',
episode: 1120, label: 'E1120');
});
test('SxxEyy behind a group', () {
_expect('[Judas] Vinland Saga - S01E28.mkv', 'Vinland Saga',
season: 1, episode: 28, group: 'Judas', label: 'S01E28');
});
test('S2 in the title and a v2 release', () {
_expect('Mushoku Tensei S2 - 03v2 [1080p].mkv', 'Mushoku Tensei',
season: 2, episode: 3, label: 'S02E03');
});
test('Season N in the title', () {
_expect('[Group] Spy x Family Season 2 - 07 [720p].mkv', 'Spy x Family',
season: 2, episode: 7, group: 'Group');
});
test('Episode word', () {
_expect('Cowboy Bebop - Episode 12.mkv', 'Cowboy Bebop', episode: 12);
_expect('Cowboy Bebop Ep.05.mkv', 'Cowboy Bebop', episode: 5);
_expect('Cowboy Bebop EP07.mkv', 'Cowboy Bebop', episode: 7);
});
test('numbers in the title are not episodes', () {
_expect('[SubsPlease] 86 - 05 (1080p).mkv', '86',
episode: 5, group: 'SubsPlease');
_expect('Mob Psycho 100 - 03 [1080p].mkv', 'Mob Psycho 100', episode: 3);
_expect('Mob Psycho 100 III - 03.mkv', 'Mob Psycho 100 III', episode: 3);
});
test('batch keeps the first episode', () {
_expect('Show.S01E01-E02.1080p.WEB.mkv', 'Show', season: 1, episode: 1);
_expect('Show S01E05E06.mkv', 'Show', season: 1, episode: 5);
_expect('[Grp] Show - 01-02 [1080p].mkv', 'Show', episode: 1, group: 'Grp');
_expect('[Grp] Show - 01~12 [1080p].mkv', 'Show', episode: 1, group: 'Grp');
_expect('Show E01-E02.mkv', 'Show', episode: 1);
});
test('resolution, codec, CRC and audio never become episodes', () {
_expect('[Grp] Show [1080p][HEVC][ABCD1234].mkv', 'Show', group: 'Grp');
_expect('Show (1920x1080 x264 AAC 5.1).mkv', 'Show');
_expect('Show 1080p x265 10bit.mkv', 'Show');
_expect('Show.720p.HDTV.x264-GRP.mkv', 'Show', group: 'GRP');
});
});
group('extras', () {
test('NCOP / NCED', () {
_expect('[Grp] Show - NCOP1 [1080p].mkv', 'Show',
group: 'Grp', extra: 'NCOP1', label: 'NCOP1');
_expect('[Grp] Show - NCED [1080p].mkv', 'Show',
group: 'Grp', extra: 'NCED');
_expect('[Grp] Show S2 NCOP 2 [BD 1080p].mkv', 'Show',
season: 2, group: 'Grp', extra: 'NCOP2');
});
test('OVA, OAD, Special, OP/ED after the dash', () {
_expect('[Grp] Show - OVA [720p].mkv', 'Show', group: 'Grp', extra: 'OVA');
_expect('[Grp] Show OVA - 02 [720p].mkv', 'Show',
group: 'Grp', extra: 'OVA 2');
_expect('Show - OAD 3.mkv', 'Show', extra: 'OAD 3');
_expect('Show - Special 1.mkv', 'Show', extra: 'Special 1');
_expect('Show - SP2.mkv', 'Show', extra: 'SP2');
_expect('Show - OP1 [1080p].mkv', 'Show', extra: 'OP1');
_expect('Show - ED.mkv', 'Show', extra: 'ED');
});
test('"Special" inside a title is a title', () {
_expect('Special Operations Unit - 04.mkv', 'Special Operations Unit',
episode: 4);
});
});
group('scene / western', () {
test('dotted SxxEyy with a trailing group', () {
_expect('The.Expanse.S01E07.1080p.WEB.H264-GRP.mkv', 'The Expanse',
season: 1, episode: 7, group: 'GRP', label: 'S01E07');
});
test('year before the episode', () {
_expect('Doctor.Who.2005.S02E03.720p.mkv', 'Doctor Who',
season: 2, episode: 3, year: 2005);
_expect('Fargo (2014) - S03E01.mkv', 'Fargo',
season: 3, episode: 1, year: 2014);
});
test('1x03 style', () {
_expect('Friends 1x03 The One.avi', 'Friends', season: 1, episode: 3);
});
test('season pack name', () {
_expect('Show.S02.1080p.BluRay.x264-GRP.mkv', 'Show',
season: 2, group: 'GRP', label: 'S02');
});
test('lowercase', () {
_expect('show.name.s01e02.mkv', 'show name', season: 1, episode: 2);
});
});
group('movies', () {
test('dotted movie: year taken out of the title', () {
_expect('Movie.Title.2023.1080p.BluRay.x264.mkv', 'Movie Title',
year: 2023, label: '');
});
test('year in parentheses', () {
_expect('Movie Title (2019) [1080p].mkv', 'Movie Title', year: 2019);
});
test('title that is a number, year too far ahead', () {
_expect('Blade.Runner.2049.2017.1080p.BluRay.mkv', 'Blade Runner 2049',
year: 2017);
_expect('Blade Runner 2049.mkv', 'Blade Runner 2049');
_expect('1917.2019.1080p.WEB-DL.mkv', '1917', year: 2019);
});
test('WEB-DL at the end is not a group', () {
_expect('Some.Movie.2021.1080p.WEB-DL.mkv', 'Some Movie', year: 2021);
});
test('hyphen in the title', () {
_expect('Spider-Man.2002.1080p.BluRay.mkv', 'Spider-Man', year: 2002);
});
});
group('unstructured', () {
test('cleaned name, no episode', () {
_expect('vid_2024_final.mp4', 'vid 2024 final');
_expect('holiday video.mov', 'holiday video');
_expect('Dr. Stone - 05.mkv', 'Dr. Stone', episode: 5);
});
test('never empty', () {
expect(parseReleaseName('[Grp].mkv').series, isNotEmpty);
expect(parseReleaseName('1080p.mkv').series, '1080p');
expect(parseReleaseName('').series, '');
});
});
test('seriesKey ignores case and punctuation', () {
expect(parseReleaseName('Re:Zero - 01.mkv').seriesKey,
parseReleaseName('re zero - 02.mkv').seriesKey);
expect(parseReleaseName("Frieren: Beyond Journey's End - 01.mkv").seriesKey,
'frieren beyond journeys end');
});
}