This commit is contained in:
@@ -13,7 +13,6 @@ class PhotoRepository {
|
|||||||
|
|
||||||
PhotoRepository(this.basePath);
|
PhotoRepository(this.basePath);
|
||||||
|
|
||||||
|
|
||||||
/// 获取相册中的照片(分页)
|
/// 获取相册中的照片(分页)
|
||||||
Future<PageResult<Photo>> getPhotosByAlbumIdPaged(
|
Future<PageResult<Photo>> getPhotosByAlbumIdPaged(
|
||||||
int id, {
|
int id, {
|
||||||
@@ -93,10 +92,7 @@ class PhotoRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 创建排序比较器
|
/// 创建排序比较器
|
||||||
int Function(Photo, Photo) _createComparator(
|
int Function(Photo, Photo) _createComparator(String sortBy, String order) {
|
||||||
String sortBy,
|
|
||||||
String order,
|
|
||||||
) {
|
|
||||||
final ascending = order == 'asc';
|
final ascending = order == 'asc';
|
||||||
|
|
||||||
int compare(Photo a, Photo b) {
|
int compare(Photo a, Photo b) {
|
||||||
@@ -105,9 +101,7 @@ class PhotoRepository {
|
|||||||
if (valueA == null && valueB == null) return 0;
|
if (valueA == null && valueB == null) return 0;
|
||||||
if (valueA == null) return 1;
|
if (valueA == null) return 1;
|
||||||
if (valueB == null) return -1;
|
if (valueB == null) return -1;
|
||||||
return ascending
|
return ascending ? valueA.compareTo(valueB) : valueB.compareTo(valueA);
|
||||||
? valueA.compareTo(valueB)
|
|
||||||
: valueB.compareTo(valueA);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (sortBy) {
|
switch (sortBy) {
|
||||||
|
|||||||
@@ -274,16 +274,18 @@ void main() {
|
|||||||
'should return empty list when base directory does not exist',
|
'should return empty list when base directory does not exist',
|
||||||
() async {
|
() async {
|
||||||
final repo = PhotoRepository('/nonexistent/path');
|
final repo = PhotoRepository('/nonexistent/path');
|
||||||
final photos = (await repo.getPhotosByAlbumIdPaged(123)).items; expect(photos, isEmpty);
|
final photos = (await repo.getPhotosByAlbumIdPaged(123)).items;
|
||||||
|
expect(photos, isEmpty);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
test(
|
test(
|
||||||
'should return empty list when album directory does not exist',
|
'should return empty list when album directory does not exist',
|
||||||
() async {
|
() async {
|
||||||
final photos =(await repository.getPhotosByAlbumIdPaged(
|
final photos = (await repository.getPhotosByAlbumIdPaged(
|
||||||
p.join(tempDir.path, 'nonexistent').hashCode,
|
p.join(tempDir.path, 'nonexistent').hashCode,
|
||||||
)).items; expect(photos, isEmpty);
|
)).items;
|
||||||
|
expect(photos, isEmpty);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -434,9 +436,10 @@ void main() {
|
|||||||
).create();
|
).create();
|
||||||
_createTestPng(albumDir, 'unique.png', width: 50, height: 50);
|
_createTestPng(albumDir, 'unique.png', width: 50, height: 50);
|
||||||
|
|
||||||
final allPhotos =(await repository.getPhotosByAlbumIdPaged(
|
final allPhotos = (await repository.getPhotosByAlbumIdPaged(
|
||||||
p.join(tempDir.path, 'find_album').hashCode,
|
p.join(tempDir.path, 'find_album').hashCode,
|
||||||
)).items; final targetId = allPhotos.first.id;
|
)).items;
|
||||||
|
final targetId = allPhotos.first.id;
|
||||||
|
|
||||||
final photo = await repository.getPhotoById(targetId);
|
final photo = await repository.getPhotoById(targetId);
|
||||||
|
|
||||||
@@ -452,9 +455,10 @@ void main() {
|
|||||||
).create();
|
).create();
|
||||||
_createTestBmp(albumDir, 'dims.bmp', width: 1024, height: 768);
|
_createTestBmp(albumDir, 'dims.bmp', width: 1024, height: 768);
|
||||||
|
|
||||||
final allPhotos =(await repository.getPhotosByAlbumIdPaged(
|
final allPhotos = (await repository.getPhotosByAlbumIdPaged(
|
||||||
p.join(tempDir.path, 'dims_album').hashCode,
|
p.join(tempDir.path, 'dims_album').hashCode,
|
||||||
)).items; final targetId = allPhotos.first.id;
|
)).items;
|
||||||
|
final targetId = allPhotos.first.id;
|
||||||
|
|
||||||
final photo = await repository.getPhotoById(targetId);
|
final photo = await repository.getPhotoById(targetId);
|
||||||
|
|
||||||
@@ -472,9 +476,10 @@ void main() {
|
|||||||
File(p.join(albumDir.path, 'test.jpg')).writeAsBytesSync([0xFF, 0xD8]);
|
File(p.join(albumDir.path, 'test.jpg')).writeAsBytesSync([0xFF, 0xD8]);
|
||||||
File(p.join(albumDir.path, 'test2.jpeg')).writeAsBytesSync([0xFF, 0xD8]);
|
File(p.join(albumDir.path, 'test2.jpeg')).writeAsBytesSync([0xFF, 0xD8]);
|
||||||
|
|
||||||
final photos =(await repository.getPhotosByAlbumIdPaged(
|
final photos = (await repository.getPhotosByAlbumIdPaged(
|
||||||
p.join(tempDir.path, 'mime_jpg').hashCode,
|
p.join(tempDir.path, 'mime_jpg').hashCode,
|
||||||
)).items; expect(photos.length, 2);
|
)).items;
|
||||||
|
expect(photos.length, 2);
|
||||||
expect(photos.every((p) => p.mimeType == 'image/jpeg'), isTrue);
|
expect(photos.every((p) => p.mimeType == 'image/jpeg'), isTrue);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -486,9 +491,10 @@ void main() {
|
|||||||
p.join(albumDir.path, 'test.png'),
|
p.join(albumDir.path, 'test.png'),
|
||||||
).writeAsBytesSync(List.filled(100, 0));
|
).writeAsBytesSync(List.filled(100, 0));
|
||||||
|
|
||||||
final photos =(await repository.getPhotosByAlbumIdPaged(
|
final photos = (await repository.getPhotosByAlbumIdPaged(
|
||||||
p.join(tempDir.path, 'mime_png').hashCode,
|
p.join(tempDir.path, 'mime_png').hashCode,
|
||||||
)).items; expect(photos.first.mimeType, 'image/png');
|
)).items;
|
||||||
|
expect(photos.first.mimeType, 'image/png');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should detect webp as image/webp', () async {
|
test('should detect webp as image/webp', () async {
|
||||||
@@ -499,9 +505,10 @@ void main() {
|
|||||||
p.join(albumDir.path, 'test.webp'),
|
p.join(albumDir.path, 'test.webp'),
|
||||||
).writeAsBytesSync(List.filled(100, 0));
|
).writeAsBytesSync(List.filled(100, 0));
|
||||||
|
|
||||||
final photos =(await repository.getPhotosByAlbumIdPaged(
|
final photos = (await repository.getPhotosByAlbumIdPaged(
|
||||||
p.join(tempDir.path, 'mime_webp').hashCode,
|
p.join(tempDir.path, 'mime_webp').hashCode,
|
||||||
)).items; expect(photos.first.mimeType, 'image/webp');
|
)).items;
|
||||||
|
expect(photos.first.mimeType, 'image/webp');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should detect mp4 as video/mp4', () async {
|
test('should detect mp4 as video/mp4', () async {
|
||||||
@@ -512,9 +519,10 @@ void main() {
|
|||||||
p.join(albumDir.path, 'test.mp4'),
|
p.join(albumDir.path, 'test.mp4'),
|
||||||
).writeAsBytesSync(List.filled(100, 0));
|
).writeAsBytesSync(List.filled(100, 0));
|
||||||
|
|
||||||
final photos =(await repository.getPhotosByAlbumIdPaged(
|
final photos = (await repository.getPhotosByAlbumIdPaged(
|
||||||
p.join(tempDir.path, 'mime_mp4').hashCode,
|
p.join(tempDir.path, 'mime_mp4').hashCode,
|
||||||
)).items; expect(photos.first.mimeType, 'video/mp4');
|
)).items;
|
||||||
|
expect(photos.first.mimeType, 'video/mp4');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should detect avi as video/avi', () async {
|
test('should detect avi as video/avi', () async {
|
||||||
@@ -525,9 +533,10 @@ void main() {
|
|||||||
p.join(albumDir.path, 'test.avi'),
|
p.join(albumDir.path, 'test.avi'),
|
||||||
).writeAsBytesSync(List.filled(100, 0));
|
).writeAsBytesSync(List.filled(100, 0));
|
||||||
|
|
||||||
final photos =(await repository.getPhotosByAlbumIdPaged(
|
final photos = (await repository.getPhotosByAlbumIdPaged(
|
||||||
p.join(tempDir.path, 'mime_avi').hashCode,
|
p.join(tempDir.path, 'mime_avi').hashCode,
|
||||||
)).items; expect(photos.first.mimeType, 'video/avi');
|
)).items;
|
||||||
|
expect(photos.first.mimeType, 'video/avi');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should detect mov as video/quicktime', () async {
|
test('should detect mov as video/quicktime', () async {
|
||||||
@@ -538,9 +547,10 @@ void main() {
|
|||||||
p.join(albumDir.path, 'test.mov'),
|
p.join(albumDir.path, 'test.mov'),
|
||||||
).writeAsBytesSync(List.filled(100, 0));
|
).writeAsBytesSync(List.filled(100, 0));
|
||||||
|
|
||||||
final photos =(await repository.getPhotosByAlbumIdPaged(
|
final photos = (await repository.getPhotosByAlbumIdPaged(
|
||||||
p.join(tempDir.path, 'mime_mov').hashCode,
|
p.join(tempDir.path, 'mime_mov').hashCode,
|
||||||
)).items; expect(photos.first.mimeType, 'video/quicktime');
|
)).items;
|
||||||
|
expect(photos.first.mimeType, 'video/quicktime');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should detect mkv as video/x-matroska', () async {
|
test('should detect mkv as video/x-matroska', () async {
|
||||||
@@ -551,9 +561,10 @@ void main() {
|
|||||||
p.join(albumDir.path, 'test.mkv'),
|
p.join(albumDir.path, 'test.mkv'),
|
||||||
).writeAsBytesSync(List.filled(100, 0));
|
).writeAsBytesSync(List.filled(100, 0));
|
||||||
|
|
||||||
final photos =(await repository.getPhotosByAlbumIdPaged(
|
final photos = (await repository.getPhotosByAlbumIdPaged(
|
||||||
p.join(tempDir.path, 'mime_mkv').hashCode,
|
p.join(tempDir.path, 'mime_mkv').hashCode,
|
||||||
)).items; expect(photos.first.mimeType, 'video/x-matroska');
|
)).items;
|
||||||
|
expect(photos.first.mimeType, 'video/x-matroska');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -570,9 +581,10 @@ void main() {
|
|||||||
).writeAsBytesSync(List.filled(10, 0));
|
).writeAsBytesSync(List.filled(10, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
final photos =(await repository.getPhotosByAlbumIdPaged(
|
final photos = (await repository.getPhotosByAlbumIdPaged(
|
||||||
p.join(tempDir.path, 'ext_test').hashCode,
|
p.join(tempDir.path, 'ext_test').hashCode,
|
||||||
)).items; expect(photos.length, extensions.length);
|
)).items;
|
||||||
|
expect(photos.length, extensions.length);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should recognize all video extensions', () async {
|
test('should recognize all video extensions', () async {
|
||||||
@@ -587,9 +599,10 @@ void main() {
|
|||||||
).writeAsBytesSync(List.filled(10, 0));
|
).writeAsBytesSync(List.filled(10, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
final photos =(await repository.getPhotosByAlbumIdPaged(
|
final photos = (await repository.getPhotosByAlbumIdPaged(
|
||||||
p.join(tempDir.path, 'ext_video').hashCode,
|
p.join(tempDir.path, 'ext_video').hashCode,
|
||||||
)).items; expect(photos.length, extensions.length);
|
)).items;
|
||||||
|
expect(photos.length, extensions.length);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should ignore unsupported extensions', () async {
|
test('should ignore unsupported extensions', () async {
|
||||||
@@ -601,9 +614,10 @@ void main() {
|
|||||||
File(p.join(albumDir.path, 'skip.pdf')).writeAsBytesSync([]);
|
File(p.join(albumDir.path, 'skip.pdf')).writeAsBytesSync([]);
|
||||||
File(p.join(albumDir.path, 'skip.exe')).writeAsBytesSync([]);
|
File(p.join(albumDir.path, 'skip.exe')).writeAsBytesSync([]);
|
||||||
|
|
||||||
final photos =(await repository.getPhotosByAlbumIdPaged(
|
final photos = (await repository.getPhotosByAlbumIdPaged(
|
||||||
p.join(tempDir.path, 'ext_ignore').hashCode,
|
p.join(tempDir.path, 'ext_ignore').hashCode,
|
||||||
)).items; expect(photos.length, 1);
|
)).items;
|
||||||
|
expect(photos.length, 1);
|
||||||
expect(photos.first.fileName, 'valid.png');
|
expect(photos.first.fileName, 'valid.png');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user