1+ import 'dart:convert' ;
12import 'dart:io' ;
23
4+ import 'package:connectivity_plus/connectivity_plus.dart' ;
5+ import 'package:dio/dio.dart' ;
36import 'package:flutter/material.dart' ;
4- import 'package:path_provider/path_provider.dart' ;
7+ import 'package:flutter_secure_storage/flutter_secure_storage.dart' ;
8+ import 'package:piwigo_ng/api/api_error.dart' ;
9+ import 'package:piwigo_ng/api/authentication.dart' ;
10+ import 'package:piwigo_ng/api/upload.dart' ;
11+ import 'package:piwigo_ng/models/album_model.dart' ;
12+ import 'package:piwigo_ng/services/notification_service.dart' ;
513import 'package:piwigo_ng/services/preferences_service.dart' ;
14+ import 'package:piwigo_ng/utils/settings.dart' ;
615import 'package:shared_preferences/shared_preferences.dart' ;
716import 'package:workmanager/workmanager.dart' ;
817
@@ -16,44 +25,133 @@ class AutoUploadManager {
1625 }
1726
1827 Future <void > endAutoUpload () async {
19- appPreferences.setBool (
20- Preferences .autoUploadKey,
21- false ,
22- );
28+ SharedPreferences prefs = await SharedPreferences .getInstance ();
29+ prefs.setBool (AutoUploadPrefs .autoUploadKey, false );
2330 await _manager.cancelByUniqueName (taskKey);
2431 }
2532
2633 Future <void > startAutoUpload () async {
27- appPreferences.setBool (
28- Preferences .autoUploadKey,
29- true ,
30- );
34+ SharedPreferences prefs = await SharedPreferences .getInstance ();
35+ int hours = prefs.getInt (AutoUploadPrefs .autoUploadFrequencyKey) ?? Settings .defaultAutoUploadFrequency;
36+ prefs.setBool (AutoUploadPrefs .autoUploadKey, true );
3137 await _manager.registerPeriodicTask (
3238 taskKey,
3339 taskKey,
34- frequency: const Duration (minutes : 15 ),
40+ frequency: Duration (hours : hours ),
3541 );
3642 }
3743
44+ Future <bool > autoUpload () async {
45+ SharedPreferences prefs = await SharedPreferences .getInstance ();
46+ if (prefs.getBool (Preferences .wifiUploadKey) ?? false ) {
47+ print ('Check wifi only' );
48+ var connectivity = await Connectivity ().checkConnectivity ();
49+ if (connectivity != ConnectivityResult .wifi) {
50+ print ('No wifi' );
51+ return Future .value (false );
52+ }
53+ print ('Has wifi' );
54+ }
55+ final Directory ? appDocDir = await getUploadDirectory ();
56+ if (appDocDir == null ) return false ;
57+ print (appDocDir.listSync ());
58+ 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 );
70+ }
71+
3872 Future <Directory ?> getUploadDirectory () async {
39- return await getTemporaryDirectory ();
73+ SharedPreferences prefs = await SharedPreferences .getInstance ();
74+ String ? path = prefs.getString (AutoUploadPrefs .autoUploadSourceKey);
75+ if (path == null ) return null ;
76+ return Directory (path);
77+ }
78+
79+ Future <void > autoUploadPhotos (List <File > photos) async {
80+ if (photos.isEmpty) return ;
81+ List <int > result = [];
82+ SharedPreferences prefs = await SharedPreferences .getInstance ();
83+ FlutterSecureStorage storage = const FlutterSecureStorage ();
84+ String ? url = await storage.read (key: 'SERVER_URL' );
85+ if (url == null ) return ;
86+ String ? username = await storage.read (key: 'SERVER_USERNAME' );
87+ String ? password = await storage.read (key: 'SERVER_PASSWORD' );
88+ int nbError = 0 ;
89+ String ? albumJson = appPreferences.getString (AutoUploadPrefs .autoUploadDestinationKey);
90+ if (albumJson == null ) return null ;
91+ AlbumModel destination = AlbumModel .fromJson (json.decode (albumJson));
92+
93+ // todo: login
94+ // login
95+ ApiResult <bool > success = await loginUser (url, username: username ?? '' , password: password ?? '' );
96+ if (! (success.data ?? false )) {
97+ debugPrint ('login error' );
98+ return ;
99+ }
100+
101+ // upload
102+ await Future .wait (List <Future <void >>.generate (photos.length, (index) async {
103+ File file = photos[index];
104+ try {
105+ // Make Request
106+ Response ? response = await uploadChunk (
107+ photo: file,
108+ category: destination.id,
109+ url: url,
110+ username: username,
111+ password: password,
112+ onProgress: (progress) {
113+ debugPrint ("${file .path } | $progress " );
114+ },
115+ );
116+
117+ // Handle result
118+ if (response == null || json.decode (response.data)['stat' ] == 'fail' ) {
119+ nbError++ ;
120+ } else {
121+ var data = json.decode (response.data);
122+ result.add (data['result' ]['id' ]);
123+ if (prefs.getBool (Preferences .deleteAfterUploadKey) ?? false ) {
124+ // todo: delete file
125+ }
126+ }
127+ } on DioError catch (e) {
128+ debugPrint ("${e .type }" );
129+ } catch (e) {
130+ debugPrint ("$e " );
131+ nbError++ ;
132+ }
133+ }));
134+
135+ // notifications
136+ showAutoUploadNotification (nbError, result.length);
137+ if (result.isEmpty) return ;
138+ // empty lunge
139+ try {
140+ await uploadCompleted (result, destination.id);
141+ if (await methodExist ('community.images.uploadCompleted' )) {
142+ await communityUploadCompleted (result, destination.id);
143+ }
144+ } on DioError catch (e) {
145+ debugPrint (e.message);
146+ }
40147 }
41148}
42149
43150@pragma ('vm:entry-point' )
44151void callbackDispatcher () {
45152 Workmanager ().executeTask ((task, inputData) async {
46- final SharedPreferences prefs = await SharedPreferences .getInstance ();
47153 debugPrint ("Background $task " );
48- debugPrint (prefs.getString ('UPLOAD_AUTHOR_NAME' ) ?? '' );
49- final Directory ? appDocDir = await AutoUploadManager ().getUploadDirectory ();
50- if (appDocDir == null ) return false ;
51- debugPrint (appDocDir.listSync ().toString ());
52- // final List<File> files = appDocDir.listSync().whereType<File>().toList();
53- // List<XFile> uploadFiles = files.map<XFile>((file) => XFile(file.path)).toList();
54- // final result = await uploadPhotos(uploadFiles, 92);
55- // debugPrint(result.toString());
56- return Future .value (true );
154+ return await AutoUploadManager ().autoUpload ();
57155 });
58156}
59157
0 commit comments