264 lines
7.7 KiB
Dart
264 lines
7.7 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:path/path.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:photos/models/collection.dart';
|
|
import 'package:sqflite/sqflite.dart';
|
|
import 'package:sqflite_migration/sqflite_migration.dart';
|
|
|
|
class CollectionsDB {
|
|
static const _databaseName = "ente.collections.db";
|
|
static const table = 'collections';
|
|
static const tempTable = 'temp_collections';
|
|
static const _sqlBoolTrue = 1;
|
|
static const _sqlBoolFalse = 0;
|
|
|
|
static const columnID = 'collection_id';
|
|
static const columnOwner = 'owner';
|
|
static const columnEncryptedKey = 'encrypted_key';
|
|
static const columnKeyDecryptionNonce = 'key_decryption_nonce';
|
|
static const columnName = 'name';
|
|
static const columnEncryptedName = 'encrypted_name';
|
|
static const columnNameDecryptionNonce = 'name_decryption_nonce';
|
|
static const columnType = 'type';
|
|
static const columnEncryptedPath = 'encrypted_path';
|
|
static const columnPathDecryptionNonce = 'path_decryption_nonce';
|
|
static const columnVersion = 'version';
|
|
static const columnSharees = 'sharees';
|
|
static const columnPublicURLs = 'public_urls';
|
|
// MMD -> Magic Metadata
|
|
static const columnMMdEncodedJson = 'mmd_encoded_json';
|
|
static const columnMMdVersion = 'mmd_ver';
|
|
static const columnUpdationTime = 'updation_time';
|
|
static const columnIsDeleted = 'is_deleted';
|
|
|
|
static final intitialScript = [...createTable(table)];
|
|
static final migrationScripts = [
|
|
...alterNameToAllowNULL(),
|
|
...addEncryptedName(),
|
|
...addVersion(),
|
|
...addIsDeleted(),
|
|
...addPublicURLs(),
|
|
...addPrivateMetadata(),
|
|
];
|
|
|
|
final dbConfig = MigrationConfig(
|
|
initializationScript: intitialScript,
|
|
migrationScripts: migrationScripts,
|
|
);
|
|
|
|
CollectionsDB._privateConstructor();
|
|
|
|
static final CollectionsDB instance = CollectionsDB._privateConstructor();
|
|
|
|
static Future<Database> _dbFuture;
|
|
|
|
Future<Database> get database async {
|
|
_dbFuture ??= _initDatabase();
|
|
return _dbFuture;
|
|
}
|
|
|
|
Future<Database> _initDatabase() async {
|
|
final Directory documentsDirectory = await getApplicationDocumentsDirectory();
|
|
final String path = join(documentsDirectory.path, _databaseName);
|
|
return await openDatabaseWithMigration(path, dbConfig);
|
|
}
|
|
|
|
Future<void> clearTable() async {
|
|
final db = await instance.database;
|
|
await db.delete(table);
|
|
}
|
|
|
|
static List<String> createTable(String tableName) {
|
|
return [
|
|
'''
|
|
CREATE TABLE $tableName (
|
|
$columnID INTEGER PRIMARY KEY NOT NULL,
|
|
$columnOwner TEXT NOT NULL,
|
|
$columnEncryptedKey TEXT NOT NULL,
|
|
$columnKeyDecryptionNonce TEXT,
|
|
$columnName TEXT,
|
|
$columnType TEXT NOT NULL,
|
|
$columnEncryptedPath TEXT,
|
|
$columnPathDecryptionNonce TEXT,
|
|
$columnSharees TEXT,
|
|
$columnUpdationTime TEXT NOT NULL
|
|
);
|
|
'''
|
|
];
|
|
}
|
|
|
|
static List<String> alterNameToAllowNULL() {
|
|
return [
|
|
...createTable(tempTable),
|
|
'''
|
|
INSERT INTO $tempTable
|
|
SELECT *
|
|
FROM $table;
|
|
|
|
DROP TABLE $table;
|
|
|
|
ALTER TABLE $tempTable
|
|
RENAME TO $table;
|
|
'''
|
|
];
|
|
}
|
|
|
|
static List<String> addEncryptedName() {
|
|
return [
|
|
'''
|
|
ALTER TABLE $table
|
|
ADD COLUMN $columnEncryptedName TEXT;
|
|
''',
|
|
'''ALTER TABLE $table
|
|
ADD COLUMN $columnNameDecryptionNonce TEXT;
|
|
'''
|
|
];
|
|
}
|
|
|
|
static List<String> addVersion() {
|
|
return [
|
|
'''
|
|
ALTER TABLE $table
|
|
ADD COLUMN $columnVersion INTEGER DEFAULT 0;
|
|
'''
|
|
];
|
|
}
|
|
|
|
static List<String> addIsDeleted() {
|
|
return [
|
|
'''
|
|
ALTER TABLE $table
|
|
ADD COLUMN $columnIsDeleted INTEGER DEFAULT $_sqlBoolFalse;
|
|
'''
|
|
];
|
|
}
|
|
|
|
static List<String> addPublicURLs() {
|
|
return [
|
|
'''
|
|
ALTER TABLE $table
|
|
ADD COLUMN $columnPublicURLs TEXT;
|
|
'''
|
|
];
|
|
}
|
|
|
|
static List<String> addPrivateMetadata() {
|
|
return [
|
|
'''
|
|
ALTER TABLE $table ADD COLUMN $columnMMdEncodedJson TEXT DEFAULT '{}';
|
|
''',
|
|
'''
|
|
ALTER TABLE $table ADD COLUMN $columnMMdVersion INTEGER DEFAULT 0;
|
|
'''
|
|
];
|
|
}
|
|
|
|
Future<List<dynamic>> insert(List<Collection> collections) async {
|
|
final db = await instance.database;
|
|
final batch = db.batch();
|
|
for (final collection in collections) {
|
|
batch.insert(
|
|
table,
|
|
_getRowForCollection(collection),
|
|
conflictAlgorithm: ConflictAlgorithm.replace,
|
|
);
|
|
}
|
|
return await batch.commit();
|
|
}
|
|
|
|
Future<List<Collection>> getAllCollections() async {
|
|
final db = await instance.database;
|
|
final rows = await db.query(table);
|
|
final collections = <Collection>[];
|
|
for (final row in rows) {
|
|
collections.add(_convertToCollection(row));
|
|
}
|
|
return collections;
|
|
}
|
|
|
|
Future<int> getLastCollectionUpdationTime() async {
|
|
final db = await instance.database;
|
|
final rows = await db.query(
|
|
table,
|
|
orderBy: '$columnUpdationTime DESC',
|
|
limit: 1,
|
|
);
|
|
if (rows.isNotEmpty) {
|
|
return int.parse(rows[0][columnUpdationTime]);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<int> deleteCollection(int collectionID) async {
|
|
final db = await instance.database;
|
|
return db.delete(
|
|
table,
|
|
where: '$columnID = ?',
|
|
whereArgs: [collectionID],
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> _getRowForCollection(Collection collection) {
|
|
final row = <String, dynamic>{};
|
|
row[columnID] = collection.id;
|
|
row[columnOwner] = collection.owner.toJson();
|
|
row[columnEncryptedKey] = collection.encryptedKey;
|
|
row[columnKeyDecryptionNonce] = collection.keyDecryptionNonce;
|
|
row[columnName] = collection.name;
|
|
row[columnEncryptedName] = collection.encryptedName;
|
|
row[columnNameDecryptionNonce] = collection.nameDecryptionNonce;
|
|
row[columnType] = Collection.typeToString(collection.type);
|
|
row[columnEncryptedPath] = collection.attributes.encryptedPath;
|
|
row[columnPathDecryptionNonce] = collection.attributes.pathDecryptionNonce;
|
|
row[columnVersion] = collection.attributes.version;
|
|
row[columnSharees] =
|
|
json.encode(collection.sharees?.map((x) => x?.toMap())?.toList());
|
|
row[columnPublicURLs] =
|
|
json.encode(collection.publicURLs?.map((x) => x?.toMap())?.toList());
|
|
row[columnUpdationTime] = collection.updationTime;
|
|
if (collection.isDeleted ?? false) {
|
|
row[columnIsDeleted] = _sqlBoolTrue;
|
|
} else {
|
|
row[columnIsDeleted] = _sqlBoolFalse;
|
|
}
|
|
row[columnMMdVersion] = collection.mMdVersion ?? 0;
|
|
row[columnMMdEncodedJson] = collection.mMdEncodedJson ?? '{}';
|
|
return row;
|
|
}
|
|
|
|
Collection _convertToCollection(Map<String, dynamic> row) {
|
|
final Collection result = Collection(
|
|
row[columnID],
|
|
User.fromJson(row[columnOwner]),
|
|
row[columnEncryptedKey],
|
|
row[columnKeyDecryptionNonce],
|
|
row[columnName],
|
|
row[columnEncryptedName],
|
|
row[columnNameDecryptionNonce],
|
|
Collection.typeFromString(row[columnType]),
|
|
CollectionAttributes(
|
|
encryptedPath: row[columnEncryptedPath],
|
|
pathDecryptionNonce: row[columnPathDecryptionNonce],
|
|
version: row[columnVersion],
|
|
),
|
|
List<User>.from(
|
|
(json.decode(row[columnSharees]) as List).map((x) => User.fromMap(x)),
|
|
),
|
|
row[columnPublicURLs] == null
|
|
? []
|
|
: List<PublicURL>.from(
|
|
(json.decode(row[columnPublicURLs]) as List)
|
|
.map((x) => PublicURL.fromMap(x)),
|
|
),
|
|
int.parse(row[columnUpdationTime]),
|
|
// default to False is columnIsDeleted is not set
|
|
isDeleted: (row[columnIsDeleted] ?? _sqlBoolFalse) == _sqlBoolTrue,
|
|
);
|
|
result.mMdVersion = row[columnMMdVersion] ?? 0;
|
|
result.mMdEncodedJson = row[columnMMdEncodedJson] ?? '{}';
|
|
return result;
|
|
}
|
|
}
|