11import 'dart:convert' ;
2+ import 'dart:io' ;
23
34import 'package:dio/dio.dart' ;
45import 'package:flutter/material.dart' ;
56import 'package:flutter/services.dart' ;
7+ import 'package:flutter_image_compress/flutter_image_compress.dart' ;
68import 'package:flutter_local_notifications/flutter_local_notifications.dart' ;
79import 'package:image_picker/image_picker.dart' ;
10+ import 'package:path_provider/path_provider.dart' ;
811import 'package:piwigo_ng/api/API.dart' ;
912import 'package:piwigo_ng/api/ImageAPI.dart' ;
1013import 'package:piwigo_ng/constants/SettingsConstants.dart' ;
@@ -39,7 +42,7 @@ class Uploader {
3942 );
4043 }
4144
42- Future <void > uploadPhotos (BuildContext context, List <XFile > photos, String category, Map <String , dynamic > info) async {
45+ Future <void > uploadPhotos (BuildContext context, List <XFile > photos, String category, Map <String , dynamic > info, ) async {
4346 Map <String , dynamic > result = {
4447 'isSuccess' : true ,
4548 'filePath' : null ,
@@ -63,9 +66,16 @@ class Uploader {
6366 for (var element in photos) {
6467 uploadStatusProvider.status = true ;
6568
66- Response response = await uploadChunk (context, element, category, info,
69+ XFile photo;
70+ if (API .prefs.getBool ("remove_metadata" )) {
71+ photo = await testCompressAndGetFile (element);
72+ } else {
73+ photo = element;
74+ }
75+
76+ Response response = await uploadChunk (context, photo, category, info,
6777 (progress) {
68- print ( progress);
78+ debugPrint ( "$ progress " );
6979 uploadStatusProvider.progress = progress;
7080 },
7181 );
@@ -80,8 +90,8 @@ class Uploader {
8090 uploadStatusProvider.current++ ;
8191 }
8292 uploadStatusProvider.reset ();
83- } on DioError catch (e) {
84- print (e.message);
93+ } catch (e) {
94+ debugPrint (e.message);
8595 uploadStatusProvider.reset ();
8696 ScaffoldMessenger .of (context).hideCurrentSnackBar ();
8797 ScaffoldMessenger .of (context).showSnackBar (errorSnackBar (context, appStrings (context).uploadError_title));
@@ -90,13 +100,32 @@ class Uploader {
90100 try {
91101 await uploadCompleted (uploadedImages, int .parse (category));
92102 await communityUploadCompleted (uploadedImages, int .parse (category));
103+
104+ // cleanTempDirectory();
93105 } on DioError catch (e) {
94- print (e.message);
106+ debugPrint (e.message);
95107 }
96108
97109 await _showUploadNotification (result);
98110 }
99111
112+ Future <XFile > testCompressAndGetFile (XFile file) async {
113+
114+ Directory cacheDir = await getTemporaryDirectory ();
115+
116+ var result = await FlutterImageCompress .compressAndGetFile (
117+ await FlutterAbsolutePath .getAbsolutePath (file.path),
118+ cacheDir.absolute.path + file.name,
119+ );
120+
121+ return XFile (result.path, name: file.name);
122+ }
123+
124+ void cleanTempDirectory () async {
125+ Directory cacheDir = await getTemporaryDirectory ();
126+ cacheDir.deleteSync (recursive: true );
127+ }
128+
100129 Future <Response > uploadChunk (BuildContext context, XFile photo,
101130 String category, Map <String , dynamic > info,
102131 Function (double ) onProgress,
@@ -122,24 +151,17 @@ class Uploader {
122151 ),
123152 ));
124153
125- try {
126- return await chunkedUploader.upload (
127- context: context,
128- path: "/ws.php" ,
129- filePath: await FlutterAbsolutePath .getAbsolutePath (photo.path),
130- maxChunkSize: API .prefs.getInt ("upload_form_chunk_size" )* 1000 ,
131- params: queries,
132- method: 'POST' ,
133- data: fields,
134- contentType: Headers .formUrlEncodedContentType,
135- onUploadProgress: (value) => onProgress (value),
136- );
137- } on DioError catch (e) {
138- print ('Dio upload chunk error $e ' );
139- ScaffoldMessenger .of (context).hideCurrentSnackBar ();
140- ScaffoldMessenger .of (context).showSnackBar (errorSnackBar (context, appStrings (context).uploadError_title));
141- return Future .value (null );
142- }
154+ return await chunkedUploader.upload (
155+ context: context,
156+ path: "/ws.php" ,
157+ filePath: await FlutterAbsolutePath .getAbsolutePath (photo.path),
158+ maxChunkSize: API .prefs.getInt ("upload_form_chunk_size" )* 1000 ,
159+ params: queries,
160+ method: 'POST' ,
161+ data: fields,
162+ contentType: Headers .formUrlEncodedContentType,
163+ onUploadProgress: (value) => onProgress (value),
164+ );
143165 }
144166}
145167
0 commit comments