ente/lib/db/device_files_db.dart

161 lines
4.6 KiB
Dart
Raw Normal View History

import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
2022-07-24 17:22:12 +00:00
import 'package:photo_manager/photo_manager.dart';
import 'package:photos/db/files_db.dart';
import 'package:photos/models/device_folder.dart';
import 'package:photos/models/file.dart';
import 'package:sqflite/sqlite_api.dart';
extension DeviceFiles on FilesDB {
static final Logger _logger = Logger("DeviceFilesDB");
Future<void> insertDeviceFiles(List<File> files) async {
final startTime = DateTime.now();
final db = await database;
var batch = db.batch();
int batchCounter = 0;
for (File file in files) {
if (file.localID == null || file.devicePathID == null) {
debugPrint(
"attempting to insert file with missing local or "
"devicePathID ${file.tag()}",
);
continue;
}
if (batchCounter == 400) {
await batch.commit(noResult: true);
batch = db.batch();
batchCounter = 0;
}
batch.insert(
"device_files",
{
"id": file.localID,
"path_id": file.devicePathID,
},
conflictAlgorithm: ConflictAlgorithm.ignore,
);
batchCounter++;
}
await batch.commit(noResult: true);
final endTime = DateTime.now();
final duration = Duration(
microseconds:
endTime.microsecondsSinceEpoch - startTime.microsecondsSinceEpoch,
);
_logger.info(
"Batch insert of ${files.length} took ${duration.inMilliseconds} ms.",
);
}
Future<Map<String, int>> getDevicePathIDToImportedFileCount() async {
final db = await database;
final rows = await db.rawQuery(
'''
SELECT count(*) as count, path_id
2022-07-26 08:31:07 +00:00
FROM device_files
GROUP BY path_id
''',
2022-07-26 08:31:07 +00:00
);
final result = <String, int>{};
for (final row in rows) {
result[row['path_id']] = row["count"];
}
return result;
} catch (e) {
_logger.severe("failed to getDevicePathIDToImportedFileCount", e);
rethrow;
}
}
2022-07-24 17:22:12 +00:00
Future<Set<String>> getDevicePathIDs() async {
final rows = await (await database).rawQuery(
'''
2022-07-25 07:15:17 +00:00
SELECT id FROM device_path_collections
2022-07-24 17:22:12 +00:00
''',
);
final Set<String> result = <String>{};
for (final row in rows) {
2022-07-25 07:15:17 +00:00
result.add(row['id']);
2022-07-24 17:22:12 +00:00
}
return result;
}
Future<void> insertOrUpdatePathName(
List<AssetPathEntity> pathEntities,
) async {
2022-07-25 07:15:17 +00:00
try {
final Set<String> existingPathIds = await getDevicePathIDs();
final Database db = await database;
for (AssetPathEntity pathEntity in pathEntities) {
if (existingPathIds.contains(pathEntity.id)) {
await db.rawUpdate(
"UPDATE device_path_collections SET name = ? where id = "
"?",
[pathEntity.name, pathEntity.id],
);
} else {
await db.insert(
"device_path_collections",
{
"id": pathEntity.id,
"name": pathEntity.name,
},
);
}
2022-07-24 17:22:12 +00:00
}
2022-07-25 07:15:17 +00:00
} catch (e) {
_logger.severe("failed to save path names", e);
rethrow;
}
}
Future<int> updateDeviceCoverWithCount(
AssetPathEntity pathEntity,
String localID,
) async {
try {
final Database db = await database;
return db.rawUpdate(
"UPDATE device_path_collections SET name = ?, cover_id = ?, count"
" = ? where id = ?",
[pathEntity.name, localID, pathEntity.assetCount, pathEntity.id],
);
} catch (e) {
_logger.severe("failed to save path names", e);
rethrow;
2022-07-24 17:22:12 +00:00
}
}
Future<List<DevicePathCollection>> getDevicePathCollections() async {
debugPrint("Fetching DevicePathCollections From DB");
final db = await database;
final fileRows = await db.rawQuery(
'''SELECT files.* FROM FILES where local_id in (select cover_id from "
"device_path_collections) group by local_id;
''',
);
final files = convertToFiles(fileRows);
final devicePathRows = await db.rawQuery(
'''SELECT * from
device_path_collections''',
);
final List<DevicePathCollection> deviceCollections = [];
for (var row in devicePathRows) {
DevicePathCollection devicePathCollection = DevicePathCollection(
row["id"],
row['name'],
count: row['count'],
collectionID: row["collection_id"],
coverId: row["cover_id"],
);
devicePathCollection.thumbnail = files.firstWhere(
(element) => element.localID == devicePathCollection.coverId,
orElse: () => null,
);
deviceCollections.add(devicePathCollection);
}
return deviceCollections;
}
}