@@ -5,6 +5,8 @@ import 'package:connectivity_plus/connectivity_plus.dart';
55import 'package:dio/dio.dart' ;
66import 'package:flutter/material.dart' ;
77import 'package:flutter_secure_storage/flutter_secure_storage.dart' ;
8+ import 'package:image_picker/image_picker.dart' ;
9+ import 'package:piwigo_ng/api/api_client.dart' ;
810import 'package:piwigo_ng/api/api_error.dart' ;
911import 'package:piwigo_ng/api/authentication.dart' ;
1012import 'package:piwigo_ng/api/upload.dart' ;
@@ -54,19 +56,11 @@ class AutoUploadManager {
5456 }
5557 final Directory ? appDocDir = await getUploadDirectory ();
5658 if (appDocDir == null ) return false ;
57- print (appDocDir.listSync ());
5859 List <FileSystemEntity > dirFiles = appDocDir.listSync ();
59- print (dirFiles);
60- List <File > files = dirFiles
61- .where ((file) {
62- print (file.runtimeType);
63- return file is File ;
64- })
65- .map <File >((e) => e as File )
66- .toList ();
67- debugPrint (files.toString ());
68- autoUploadPhotos (files);
69- return Future .value (true );
60+ List <File > files = dirFiles.where ((file) => file is File ).map <File >((e) => e as File ).toList ();
61+ debugPrint ("List files: ${files .toString ()}" );
62+ await autoUploadPhotos (files);
63+ return true ;
7064 }
7165
7266 Future <Directory ?> getUploadDirectory () async {
@@ -86,21 +80,31 @@ class AutoUploadManager {
8680 String ? username = await storage.read (key: 'SERVER_USERNAME' );
8781 String ? password = await storage.read (key: 'SERVER_PASSWORD' );
8882 int nbError = 0 ;
89- String ? albumJson = appPreferences .getString (AutoUploadPrefs .autoUploadDestinationKey);
83+ String ? albumJson = prefs .getString (AutoUploadPrefs .autoUploadDestinationKey);
9084 if (albumJson == null ) return null ;
9185 AlbumModel destination = AlbumModel .fromJson (json.decode (albumJson));
9286
87+ print ('Try login' );
9388 // todo: login
9489 // login
95- ApiResult <bool > success = await loginUser (url, username: username ?? '' , password: password ?? '' );
90+ ApiResult <bool > success = await loginUser (
91+ url,
92+ username: username ?? '' ,
93+ password: password ?? '' ,
94+ );
95+
9696 if (! (success.data ?? false )) {
97- debugPrint ('login error' );
97+ print ('login error' );
9898 return ;
9999 }
100+ print ('Login success' );
100101
101102 // upload
102- await Future .wait (List <Future <void >>.generate (photos.length, (index) async {
103- File file = photos[index];
103+ for (File file in photos) {
104+ print ("Try upload ${file .path }" );
105+ if (prefs.getBool (Preferences .removeMetadataKey) ?? Settings .defaultRemoveMetadata) {
106+ file = await compressFile (XFile (file.path));
107+ }
104108 try {
105109 // Make Request
106110 Response ? response = await uploadChunk (
@@ -109,15 +113,13 @@ class AutoUploadManager {
109113 url: url,
110114 username: username,
111115 password: password,
112- onProgress: (progress) {
113- debugPrint ("${file .path } | $progress " );
114- },
115116 );
116117
117118 // Handle result
118119 if (response == null || json.decode (response.data)['stat' ] == 'fail' ) {
119120 nbError++ ;
120121 } else {
122+ print ("Upload success ${response .data }" );
121123 var data = json.decode (response.data);
122124 result.add (data['result' ]['id' ]);
123125 if (prefs.getBool (Preferences .deleteAfterUploadKey) ?? false ) {
@@ -126,25 +128,99 @@ class AutoUploadManager {
126128 }
127129 } on DioError catch (e) {
128130 debugPrint ("${e .type }" );
131+ nbError++ ;
132+ } on Error catch (e) {
133+ debugPrint ("$e " );
134+ debugPrint ("${e .stackTrace }" );
135+ nbError++ ;
129136 } catch (e) {
130137 debugPrint ("$e " );
131138 nbError++ ;
132139 }
133- }));
140+ }
134141
135142 // notifications
136143 showAutoUploadNotification (nbError, result.length);
137144 if (result.isEmpty) return ;
145+ print ('Empty lunge' );
138146 // empty lunge
139147 try {
140- await uploadCompleted (result, destination.id);
148+ bool uploadCompletedSuccess = await uploadCompleted (result, destination.id);
149+ print (uploadCompletedSuccess);
141150 if (await methodExist ('community.images.uploadCompleted' )) {
142151 await communityUploadCompleted (result, destination.id);
143152 }
144153 } on DioError catch (e) {
145154 debugPrint (e.message);
146155 }
147156 }
157+
158+ Future <ApiResult <bool >> loginAutoUpload (
159+ String url, {
160+ String username = '' ,
161+ String password = '' ,
162+ }) async {
163+ if (url.isEmpty) {
164+ return ApiResult <bool >(
165+ data: false ,
166+ error: ApiErrors .wrongServerUrl,
167+ );
168+ }
169+
170+ ApiClient .cookieJar.deleteAll ();
171+ FlutterSecureStorage secureStorage = const FlutterSecureStorage ();
172+ await secureStorage.write (key: 'SERVER_URL' , value: url);
173+
174+ if (username.isEmpty && password.isEmpty) {
175+ return ApiResult <bool >(
176+ data: false ,
177+ error: ApiErrors .wrongServerUrl,
178+ );
179+ }
180+
181+ Map <String , String > queries = {
182+ 'format' : 'json' ,
183+ 'method' : 'pwg.session.login' ,
184+ };
185+ Map <String , String > fields = {
186+ 'username' : username,
187+ 'password' : password,
188+ };
189+
190+ try {
191+ Response response = await ApiClient .post (
192+ data: FormData .fromMap (fields),
193+ options: Options (contentType: Headers .formUrlEncodedContentType),
194+ queryParameters: queries,
195+ );
196+
197+ if (response.statusCode == 200 ) {
198+ var data = json.decode (response.data);
199+ if (data['stat' ] == 'fail' ) {
200+ return ApiResult <bool >(
201+ data: false ,
202+ error: ApiErrors .wrongLoginId,
203+ );
204+ }
205+ await sessionStatus ();
206+ return ApiResult <bool >(
207+ data: true ,
208+ );
209+ }
210+ return ApiResult <bool >(
211+ data: false ,
212+ error: ApiErrors .wrongLoginId,
213+ );
214+ } on DioError catch (e) {
215+ debugPrint (e.message);
216+ } catch (e) {
217+ debugPrint ('Error $e ' );
218+ }
219+ return ApiResult <bool >(
220+ data: false ,
221+ error: ApiErrors .wrongServerUrl,
222+ );
223+ }
148224}
149225
150226@pragma ('vm:entry-point' )
0 commit comments