11import 'dart:convert' ;
2+ import 'dart:io' ;
23
34import 'package:dio/dio.dart' ;
45import 'package:file_picker/file_picker.dart' ;
@@ -7,10 +8,10 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart';
78import 'package:image_picker/image_picker.dart' ;
89import 'package:path/path.dart' as path;
910import 'package:path_provider/path_provider.dart' ;
10- import 'package:permission_handler/permission_handler.dart' ;
1111import 'package:piwigo_ng/api/api_error.dart' ;
1212import 'package:piwigo_ng/models/album_model.dart' ;
1313import 'package:piwigo_ng/models/image_model.dart' ;
14+ import 'package:piwigo_ng/services/chunked_uploader.dart' ;
1415import 'package:piwigo_ng/services/notification_service.dart' ;
1516import 'package:piwigo_ng/services/preferences_service.dart' ;
1617import 'package:piwigo_ng/utils/localizations.dart' ;
@@ -47,7 +48,8 @@ Future<ApiResult<ImageModel>> getImage(int imageId) async {
4748 return ApiResult (error: ApiErrors .error);
4849}
4950
50- Future <ApiResult <List <ImageModel >>> fetchImages (int albumID, [int page = 0 ]) async {
51+ Future <ApiResult <List <ImageModel >>> fetchImages (int albumID,
52+ [int page = 0 ]) async {
5153 Map <String , dynamic > queries = {
5254 'format' : 'json' ,
5355 'method' : 'pwg.categories.getImages' ,
@@ -154,21 +156,12 @@ Future<ApiResult<Map>> fetchFavorites([int page = 0]) async {
154156 return ApiResult (error: ApiErrors .error);
155157}
156158
157- Future <bool > _requestPermissions () async {
158- var permission = await Permission .storage.status;
159- if (permission != PermissionStatus .granted) {
160- await Permission .storage.request ();
161- permission = await Permission .storage.status;
162- }
163-
164- return permission == PermissionStatus .granted;
165- }
166-
167159Future <String ?> pickDirectoryPath () async {
168160 return await FilePicker .platform.getDirectoryPath ();
169161}
170162
171- Future <void > _showDownloadNotification ({bool success = true , String ? payload}) async {
163+ Future <void > _showDownloadNotification (
164+ {bool success = true , String ? payload}) async {
172165 if (! Preferences .getDownloadNotification) return ;
173166 final android = AndroidNotificationDetails (
174167 'piwigo-ng-download' ,
@@ -179,8 +172,12 @@ Future<void> _showDownloadNotification({bool success = true, String? payload}) a
179172 );
180173 await showLocalNotification (
181174 id: 0 ,
182- title: success ? appStrings.downloadImageSuccess_title : appStrings.downloadImageFail_title,
183- body: success ? appStrings.downloadImageSuccess_message : appStrings.deleteImageFail_message,
175+ title: success
176+ ? appStrings.downloadImageSuccess_title
177+ : appStrings.downloadImageFail_title,
178+ body: success
179+ ? appStrings.downloadImageSuccess_message
180+ : appStrings.deleteImageFail_message,
184181 details: android,
185182 payload: payload,
186183 );
@@ -317,7 +314,8 @@ Future<int> removeImages(List<ImageModel> images, int albumId) async {
317314}
318315
319316Future <bool > removeImage (ImageModel image, int albumId) async {
320- final List <int > albums = image.categories.map <int >((album) => album['id' ]).toList ();
317+ final List <int > albums =
318+ image.categories.map <int >((album) => album['id' ]).toList ();
321319 albums.removeWhere ((album) => album == albumId);
322320
323321 if (albums.isEmpty) {
@@ -335,7 +333,8 @@ Future<bool> removeImage(ImageModel image, int albumId) async {
335333 });
336334
337335 try {
338- Response response = await ApiClient .post (data: formData, queryParameters: queries);
336+ Response response =
337+ await ApiClient .post (data: formData, queryParameters: queries);
339338
340339 if (response.statusCode == 200 ) {
341340 return true ;
@@ -348,7 +347,8 @@ Future<bool> removeImage(ImageModel image, int albumId) async {
348347 return false ;
349348}
350349
351- Future <int > moveImages (List <ImageModel > images, int oldAlbumId, int newAlbumId) async {
350+ Future <int > moveImages (
351+ List <ImageModel > images, int oldAlbumId, int newAlbumId) async {
352352 int nbMoved = 0 ;
353353 for (var image in images) {
354354 bool response = await moveImage (image, oldAlbumId, newAlbumId);
@@ -360,7 +360,8 @@ Future<int> moveImages(List<ImageModel> images, int oldAlbumId, int newAlbumId)
360360}
361361
362362Future <bool > moveImage (ImageModel image, int oldAlbumId, int newAlbumId) async {
363- final List <int > albums = image.categories.map <int >((album) => album['id' ]).toList ();
363+ final List <int > albums =
364+ image.categories.map <int >((album) => album['id' ]).toList ();
364365 albums.removeWhere ((id) => id == oldAlbumId);
365366 albums.add (newAlbumId);
366367 Map <String , String > queries = {
@@ -375,7 +376,8 @@ Future<bool> moveImage(ImageModel image, int oldAlbumId, int newAlbumId) async {
375376 });
376377
377378 try {
378- Response response = await ApiClient .post (data: formData, queryParameters: queries);
379+ Response response =
380+ await ApiClient .post (data: formData, queryParameters: queries);
379381
380382 if (response.statusCode == 200 ) {
381383 return true ;
@@ -391,7 +393,8 @@ Future<bool> moveImage(ImageModel image, int oldAlbumId, int newAlbumId) async {
391393Future <int > assignImages (List <ImageModel > images, int albumId) async {
392394 int nbAssigned = 0 ;
393395 for (ImageModel image in images) {
394- final List <int > categories = image.categories.map <int >((album) => album['id' ]).toList ();
396+ final List <int > categories =
397+ image.categories.map <int >((album) => album['id' ]).toList ();
395398 categories.add (albumId);
396399 bool response = await assignImage (image.id, categories);
397400 if (response == true ) {
@@ -414,7 +417,8 @@ Future<bool> assignImage(int imageId, List<int> categories) async {
414417 });
415418
416419 try {
417- Response response = await ApiClient .post (data: formData, queryParameters: queries);
420+ Response response =
421+ await ApiClient .post (data: formData, queryParameters: queries);
418422
419423 if (response.statusCode == 200 ) {
420424 return true ;
@@ -427,7 +431,8 @@ Future<bool> assignImage(int imageId, List<int> categories) async {
427431 return false ;
428432}
429433
430- Future <int > editImages (List <ImageModel > images, [Map <String , dynamic > info = const {}]) async {
434+ Future <int > editImages (List <ImageModel > images,
435+ [Map <String , dynamic > info = const {}]) async {
431436 int nbEdited = 0 ;
432437 for (ImageModel image in images) {
433438 bool response = await editImage (image, info);
@@ -438,7 +443,8 @@ Future<int> editImages(List<ImageModel> images, [Map<String, dynamic> info = con
438443 return nbEdited;
439444}
440445
441- Future <bool > editImage (ImageModel image, [Map <String , dynamic > info = const {}]) async {
446+ Future <bool > editImage (ImageModel image,
447+ [Map <String , dynamic > info = const {}]) async {
442448 final Map <String , String > queries = {
443449 'format' : 'json' ,
444450 'method' : 'pwg.images.setInfo' ,
@@ -472,3 +478,39 @@ Future<bool> editImage(ImageModel image, [Map<String, dynamic> info = const {}])
472478 }
473479 return false ;
474480}
481+
482+ Future <int ?> checkImageExist (File file) async {}
483+
484+ /// Return a list of files that are not in the server
485+ Future <List <File >> checkImagesNotExist (List <File > files) async {
486+ Map <String , File > md5sumList = {};
487+
488+ for (File file in files) {
489+ String md5sum = await ChunkedUploader .generateMd5 (file.openRead ());
490+ md5sumList[md5sum] = file;
491+ }
492+
493+ final Map <String , String > queries = {
494+ 'format' : 'json' ,
495+ 'method' : 'pwg.images.exist' ,
496+ 'md5sum_list' : md5sumList.keys.join (',' ),
497+ };
498+
499+ try {
500+ Response response = await ApiClient .get (
501+ queryParameters: queries,
502+ );
503+
504+ Map <String , dynamic > data = json.decode (response.data);
505+ if (data['stat' ] == 'fail' ) return [];
506+ print (data['result' ]);
507+ Map <String , dynamic > existResult = data['result' ];
508+ existResult.removeWhere ((key, value) => value != null );
509+ return existResult.keys.map ((md5sum) => md5sumList[md5sum]! ).toList ();
510+ } on DioError catch (e) {
511+ debugPrint ('Edit images: ${e .message }' );
512+ } on Error catch (e) {
513+ debugPrint ('Edit images: ${e .stackTrace }' );
514+ }
515+ return [];
516+ }
0 commit comments