Use server API to compute file size

This commit is contained in:
Vishnu 2021-06-28 12:10:19 +05:30
parent 3a369eb76d
commit 8a07a438f2
5 changed files with 49 additions and 22 deletions

View file

@ -1,6 +1,7 @@
import 'dart:io';
import 'package:logging/logging.dart';
import 'package:photos/models/backup_status.dart';
import 'package:photos/models/file_type.dart';
import 'package:photos/models/location.dart';
import 'package:photos/models/file.dart';
@ -213,19 +214,21 @@ class FilesDB {
return ids;
}
Future<Set<String>> getUploadedLocalIDs() async {
Future<BackedUpFileIDs> getBackedUpIDs() async {
final db = await instance.database;
final results = await db.query(
table,
columns: [columnLocalID],
columns: [columnLocalID, columnUploadedFileID],
where:
'$columnLocalID IS NOT NULL AND ($columnUploadedFileID IS NOT NULL AND $columnUploadedFileID IS NOT -1)',
);
final ids = Set<String>();
final localIDs = Set<String>();
final uploadedIDs = Set<int>();
for (final result in results) {
ids.add(result[columnLocalID]);
localIDs.add(result[columnLocalID]);
uploadedIDs.add(result[columnUploadedFileID]);
}
return ids;
return BackedUpFileIDs(localIDs.toList(), uploadedIDs.toList());
}
Future<List<File>> getAllFiles(int startTime, int endTime,

View file

@ -1,6 +0,0 @@
class BackedUpFiles {
final Set<String> localIDs;
final int space;
BackedUpFiles(this.localIDs, this.space);
}

View file

@ -0,0 +1,13 @@
class BackupStatus {
final List<String> localIDs;
final int size;
BackupStatus(this.localIDs, this.size);
}
class BackedUpFileIDs {
final List<String> localIDs;
final List<int> uploadedIDs;
BackedUpFileIDs(this.localIDs, this.uploadedIDs);
}

View file

@ -7,7 +7,6 @@ import 'package:photos/core/event_bus.dart';
import 'package:photos/db/files_db.dart';
import 'package:photos/events/local_photos_updated_event.dart';
import 'package:photos/events/sync_status_update_event.dart';
import 'package:photos/models/backed_up_files.dart';
import 'package:photos/models/file.dart';
import 'package:photos/utils/file_sync_util.dart';
import 'package:shared_preferences/shared_preferences.dart';
@ -174,16 +173,6 @@ class LocalSyncService {
return _prefs.getBool(kHasCompletedFirstImportKey) ?? false;
}
Future<BackedUpFiles> getBackupStatus() async {
final localIDs = await FilesDB.instance.getUploadedLocalIDs();
int space = 0;
for (final id in localIDs) {
final asset = await AssetEntity.fromId(id);
space += (await asset.originFile).lengthSync();
}
return BackedUpFiles(localIDs, space);
}
Future<void> _loadAndStorePhotos(
int fromTime,
int toTime,

View file

@ -10,10 +10,12 @@ import 'package:photos/core/constants.dart';
import 'package:photos/core/errors.dart';
import 'package:photos/core/event_bus.dart';
import 'package:photos/core/network.dart';
import 'package:photos/db/files_db.dart';
import 'package:photos/events/permission_granted_event.dart';
import 'package:photos/events/subscription_purchased_event.dart';
import 'package:photos/events/sync_status_update_event.dart';
import 'package:photos/events/trigger_logout_event.dart';
import 'package:photos/models/backup_status.dart';
import 'package:photos/services/local_sync_service.dart';
import 'package:photos/services/notification_service.dart';
import 'package:photos/services/remote_sync_service.dart';
@ -176,6 +178,32 @@ class SyncService {
);
}
Future<BackupStatus> getBackupStatus() async {
final ids = await FilesDB.instance.getBackedUpIDs();
final size = await _getFileSize(ids.uploadedIDs);
return BackupStatus(ids.localIDs, size);
}
Future<int> _getFileSize(List<int> fileIDs) async {
try {
final response = await _dio.post(
Configuration.instance.getHttpEndpoint() + "/files/size",
options: Options(
headers: {
"X-Auth-Token": Configuration.instance.getToken(),
},
),
data: {
"fileIDs": fileIDs,
},
);
return response.data["size"];
} catch (e) {
_logger.severe(e);
throw e;
}
}
Future<void> _doSync() async {
await _localSyncService.sync();
if (_localSyncService.hasCompletedFirstImport()) {