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,12 @@ class Uploader {
6366 for (var element in photos) {
6467 uploadStatusProvider.status = true ;
6568
66- Response response = await uploadChunk (context, element, category, info,
69+
70+ XFile photo = await testCompressAndGetFile (element);
71+
72+ Response response = await uploadChunk (context, photo, category, info,
6773 (progress) {
68- print ( progress);
74+ debugPrint ( "$ progress " );
6975 uploadStatusProvider.progress = progress;
7076 },
7177 );
@@ -81,7 +87,7 @@ class Uploader {
8187 }
8288 uploadStatusProvider.reset ();
8389 } on DioError catch (e) {
84- print (e.message);
90+ debugPrint (e.message);
8591 uploadStatusProvider.reset ();
8692 ScaffoldMessenger .of (context).hideCurrentSnackBar ();
8793 ScaffoldMessenger .of (context).showSnackBar (errorSnackBar (context, appStrings (context).uploadError_title));
@@ -90,13 +96,32 @@ class Uploader {
9096 try {
9197 await uploadCompleted (uploadedImages, int .parse (category));
9298 await communityUploadCompleted (uploadedImages, int .parse (category));
99+
100+ // cleanTempDirectory();
93101 } on DioError catch (e) {
94- print (e.message);
102+ debugPrint (e.message);
95103 }
96104
97105 await _showUploadNotification (result);
98106 }
99107
108+ Future <XFile > testCompressAndGetFile (XFile file) async {
109+
110+ Directory cacheDir = await getTemporaryDirectory ();
111+
112+ var result = await FlutterImageCompress .compressAndGetFile (
113+ await FlutterAbsolutePath .getAbsolutePath (file.path),
114+ cacheDir.absolute.path + file.name,
115+ );
116+
117+ return XFile (result.path, name: file.name);
118+ }
119+
120+ void cleanTempDirectory () async {
121+ Directory cacheDir = await getTemporaryDirectory ();
122+ cacheDir.deleteSync (recursive: true );
123+ }
124+
100125 Future <Response > uploadChunk (BuildContext context, XFile photo,
101126 String category, Map <String , dynamic > info,
102127 Function (double ) onProgress,
@@ -135,7 +160,7 @@ class Uploader {
135160 onUploadProgress: (value) => onProgress (value),
136161 );
137162 } on DioError catch (e) {
138- print ('Dio upload chunk error $e ' );
163+ debugPrint ('Dio upload chunk error $e ' );
139164 ScaffoldMessenger .of (context).hideCurrentSnackBar ();
140165 ScaffoldMessenger .of (context).showSnackBar (errorSnackBar (context, appStrings (context).uploadError_title));
141166 return Future .value (null );
0 commit comments