123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
- import 'dart:io';
- import 'package:logging/logging.dart';
- import 'package:photos/models/file_type.dart';
- import 'package:photos/models/location.dart';
- import 'package:photos/models/file.dart';
- import 'package:path/path.dart';
- import 'package:sqflite/sqflite.dart';
- import 'package:path_provider/path_provider.dart';
- class FilesDB {
- static final _databaseName = "ente.files.db";
- static final _databaseVersion = 1;
- static final Logger _logger = Logger("FilesDB");
- static final table = 'files';
- static final columnGeneratedID = '_id';
- static final columnUploadedFileID = 'uploaded_file_id';
- static final columnOwnerID = 'owner_id';
- static final columnCollectionID = 'collection_id';
- static final columnLocalID = 'local_id';
- static final columnTitle = 'title';
- static final columnDeviceFolder = 'device_folder';
- static final columnLatitude = 'latitude';
- static final columnLongitude = 'longitude';
- static final columnFileType = 'file_type';
- static final columnIsEncrypted = 'is_encrypted';
- static final columnIsDeleted = 'is_deleted';
- static final columnCreationTime = 'creation_time';
- static final columnModificationTime = 'modification_time';
- static final columnUpdationTime = 'updation_time';
- static final columnEncryptedKey = 'encrypted_key';
- static final columnKeyDecryptionNonce = 'key_decryption_nonce';
- static final columnFileDecryptionHeader = 'file_decryption_header';
- static final columnThumbnailDecryptionHeader = 'thumbnail_decryption_header';
- static final columnMetadataDecryptionHeader = 'metadata_decryption_header';
- // make this a singleton class
- FilesDB._privateConstructor();
- static final FilesDB instance = FilesDB._privateConstructor();
- // only have a single app-wide reference to the database
- static Database _database;
- Future<Database> get database async {
- if (_database != null) return _database;
- // lazily instantiate the db the first time it is accessed
- _database = await _initDatabase();
- return _database;
- }
- // this opens the database (and creates it if it doesn't exist)
- _initDatabase() async {
- Directory documentsDirectory = await getApplicationDocumentsDirectory();
- String path = join(documentsDirectory.path, _databaseName);
- return await openDatabase(path,
- version: _databaseVersion, onCreate: _onCreate);
- }
- // SQL code to create the database table
- Future _onCreate(Database db, int version) async {
- await db.execute('''
- CREATE TABLE $table (
- $columnGeneratedID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
- $columnLocalID TEXT,
- $columnUploadedFileID INTEGER,
- $columnOwnerID INTEGER,
- $columnCollectionID INTEGER,
- $columnTitle TEXT NOT NULL,
- $columnDeviceFolder TEXT NOT NULL,
- $columnLatitude REAL,
- $columnLongitude REAL,
- $columnFileType INTEGER,
- $columnIsEncrypted INTEGER DEFAULT 1,
- $columnModificationTime TEXT NOT NULL,
- $columnEncryptedKey TEXT,
- $columnKeyDecryptionNonce TEXT,
- $columnFileDecryptionHeader TEXT,
- $columnThumbnailDecryptionHeader TEXT,
- $columnMetadataDecryptionHeader TEXT,
- $columnIsDeleted INTEGER DEFAULT 0,
- $columnCreationTime TEXT NOT NULL,
- $columnUpdationTime TEXT,
- UNIQUE($columnUploadedFileID, $columnCollectionID)
- );
- CREATE INDEX collection_id_index ON $table($columnCollectionID);
- CREATE INDEX device_folder_index ON $table($columnDeviceFolder);
- CREATE INDEX creation_time_index ON $table($columnCreationTime);
- CREATE INDEX updation_time_index ON $table($columnUpdationTime);
- ''');
- }
- Future<int> insert(File file) async {
- final db = await instance.database;
- return await db.insert(table, _getRowForFile(file));
- }
- Future<List<dynamic>> insertMultiple(List<File> files) async {
- final db = await instance.database;
- var batch = db.batch();
- int batchCounter = 0;
- for (File file in files) {
- if (batchCounter == 400) {
- await batch.commit();
- batch = db.batch();
- }
- batch.insert(
- table,
- _getRowForFile(file),
- conflictAlgorithm: ConflictAlgorithm.replace,
- );
- batchCounter++;
- }
- return await batch.commit();
- }
- Future<File> getFile(int generatedID) async {
- final db = await instance.database;
- final results = await db.query(table,
- where: '$columnGeneratedID = ?', whereArgs: [generatedID]);
- if (results.isEmpty) {
- return null;
- }
- return _convertToFiles(results)[0];
- }
- Future<List<File>> getDeduplicatedFiles() async {
- _logger.info("Getting files for collection");
- final db = await instance.database;
- final results = await db.query(table,
- where: '$columnIsDeleted = 0',
- orderBy: '$columnCreationTime DESC',
- groupBy:
- 'IFNULL($columnUploadedFileID, $columnGeneratedID), IFNULL($columnLocalID, $columnGeneratedID)');
- return _convertToFiles(results);
- }
- Future<List<File>> getFiles() async {
- final db = await instance.database;
- final results = await db.query(
- table,
- where: '$columnIsDeleted = 0',
- orderBy: '$columnCreationTime DESC',
- );
- return _convertToFiles(results);
- }
- Future<List<File>> getAllVideos() async {
- final db = await instance.database;
- final results = await db.query(
- table,
- where:
- '$columnLocalID IS NOT NULL AND $columnFileType = 1 AND $columnIsDeleted = 0',
- orderBy: '$columnCreationTime DESC',
- );
- return _convertToFiles(results);
- }
- Future<List<File>> getAllInCollectionBeforeCreationTime(
- int collectionID, int beforeCreationTime, int limit) async {
- final db = await instance.database;
- final results = await db.query(
- table,
- where:
- '$columnCollectionID = ? AND $columnIsDeleted = 0 AND $columnCreationTime < ?',
- whereArgs: [collectionID, beforeCreationTime],
- orderBy: '$columnCreationTime DESC',
- limit: limit,
- );
- return _convertToFiles(results);
- }
- Future<List<File>> getAllInPath(String path) async {
- final db = await instance.database;
- final results = await db.query(
- table,
- where: '$columnLocalID IS NOT NULL AND $columnDeviceFolder = ?',
- whereArgs: [path],
- orderBy: '$columnCreationTime DESC',
- groupBy: '$columnLocalID',
- );
- return _convertToFiles(results);
- }
- Future<List<File>> getAllInPathBeforeCreationTime(
- String path, int beforeCreationTime, int limit) async {
- final db = await instance.database;
- final results = await db.query(
- table,
- where:
- '$columnLocalID IS NOT NULL AND $columnDeviceFolder = ? AND $columnIsDeleted = 0 AND $columnCreationTime < ?',
- whereArgs: [path, beforeCreationTime],
- orderBy: '$columnCreationTime DESC',
- groupBy: '$columnLocalID',
- limit: limit,
- );
- return _convertToFiles(results);
- }
- Future<List<File>> getAllInCollection(int collectionID) async {
- final db = await instance.database;
- final results = await db.query(
- table,
- where: '$columnCollectionID = ?',
- whereArgs: [collectionID],
- orderBy: '$columnCreationTime DESC',
- );
- return _convertToFiles(results);
- }
- Future<List<File>> getFilesCreatedWithinDuration(
- int startCreationTime, int endCreationTime) async {
- final db = await instance.database;
- final results = await db.query(
- table,
- where:
- '$columnCreationTime > ? AND $columnCreationTime < ? AND $columnIsDeleted = 0',
- whereArgs: [startCreationTime, endCreationTime],
- orderBy: '$columnCreationTime ASC',
- );
- return _convertToFiles(results);
- }
- Future<List<int>> getDeletedFileIDs() async {
- final db = await instance.database;
- final rows = await db.query(
- table,
- columns: [columnUploadedFileID],
- distinct: true,
- where: '$columnIsDeleted = 1',
- orderBy: '$columnCreationTime DESC',
- );
- final result = List<int>();
- for (final row in rows) {
- result.add(row[columnUploadedFileID]);
- }
- return result;
- }
- Future<List<File>> getFilesToBeUploadedWithinFolders(
- Set<String> folders) async {
- final db = await instance.database;
- String inParam = "";
- for (final folder in folders) {
- inParam += "'" + folder + "',";
- }
- inParam = inParam.substring(0, inParam.length - 1);
- final results = await db.query(
- table,
- where:
- '$columnUploadedFileID IS NULL AND $columnDeviceFolder IN ($inParam)',
- orderBy: '$columnCreationTime DESC',
- );
- return _convertToFiles(results);
- }
- Future<Map<int, File>> getLastCreatedFilesInCollections(
- List<int> collectionIDs) async {
- final db = await instance.database;
- final rows = await db.rawQuery('''
- SELECT
- $columnGeneratedID,
- $columnLocalID,
- $columnUploadedFileID,
- $columnOwnerID,
- $columnCollectionID,
- $columnTitle,
- $columnDeviceFolder,
- $columnLatitude,
- $columnLongitude,
- $columnFileType,
- $columnIsEncrypted,
- $columnModificationTime,
- $columnEncryptedKey,
- $columnKeyDecryptionNonce,
- $columnFileDecryptionHeader,
- $columnThumbnailDecryptionHeader,
- $columnMetadataDecryptionHeader,
- $columnIsDeleted,
- $columnUpdationTime,
- MAX($columnCreationTime) as $columnCreationTime
- FROM $table
- WHERE $columnCollectionID IN (${collectionIDs.join(', ')}) AND $columnIsDeleted = 0
- GROUP BY $columnCollectionID
- ORDER BY $columnCreationTime DESC;
- ''');
- final result = Map<int, File>();
- final files = _convertToFiles(rows);
- for (final file in files) {
- result[file.collectionID] = file;
- }
- return result;
- }
- Future<Map<int, File>> getLastUpdatedFilesInCollections(
- List<int> collectionIDs) async {
- final db = await instance.database;
- final rows = await db.rawQuery('''
- SELECT
- $columnGeneratedID,
- $columnLocalID,
- $columnUploadedFileID,
- $columnOwnerID,
- $columnCollectionID,
- $columnTitle,
- $columnDeviceFolder,
- $columnLatitude,
- $columnLongitude,
- $columnFileType,
- $columnIsEncrypted,
- $columnModificationTime,
- $columnEncryptedKey,
- $columnKeyDecryptionNonce,
- $columnFileDecryptionHeader,
- $columnThumbnailDecryptionHeader,
- $columnMetadataDecryptionHeader,
- $columnIsDeleted,
- $columnCreationTime,
- MAX($columnUpdationTime) AS $columnUpdationTime
- FROM $table
- WHERE $columnCollectionID IN (${collectionIDs.join(', ')}) AND $columnIsDeleted = 0
- GROUP BY $columnCollectionID
- ORDER BY $columnUpdationTime DESC;
- ''');
- final result = Map<int, File>();
- final files = _convertToFiles(rows);
- for (final file in files) {
- result[file.collectionID] = file;
- }
- return result;
- }
- Future<List<File>> getMatchingFiles(
- String title, String deviceFolder, int creationTime, int modificationTime,
- {String alternateTitle}) async {
- final db = await instance.database;
- final rows = await db.query(
- table,
- where: '''($columnTitle=? OR $columnTitle=?) AND
- $columnDeviceFolder=? AND $columnCreationTime=? AND
- $columnModificationTime=?''',
- whereArgs: [
- title,
- alternateTitle,
- deviceFolder,
- creationTime,
- modificationTime,
- ],
- );
- if (rows.isNotEmpty) {
- return _convertToFiles(rows);
- } else {
- return null;
- }
- }
- Future<File> getMatchingRemoteFile(int uploadedFileID) async {
- final db = await instance.database;
- final rows = await db.query(
- table,
- where: '$columnUploadedFileID=?',
- whereArgs: [uploadedFileID],
- );
- if (rows.isNotEmpty) {
- return _getFileFromRow(rows[0]);
- } else {
- throw ("No matching file found");
- }
- }
- Future<int> update(File file) async {
- final db = await instance.database;
- return await db.update(
- table,
- _getRowForFile(file),
- where: '$columnGeneratedID = ?',
- whereArgs: [file.generatedID],
- );
- }
- Future<int> markForDeletion(int uploadedFileID) async {
- final db = await instance.database;
- final values = new Map<String, dynamic>();
- values[columnIsDeleted] = 1;
- return db.update(
- table,
- values,
- where: '$columnUploadedFileID =?',
- whereArgs: [uploadedFileID],
- );
- }
- Future<int> delete(int uploadedFileID) async {
- final db = await instance.database;
- return db.delete(
- table,
- where: '$columnUploadedFileID =?',
- whereArgs: [uploadedFileID],
- );
- }
- Future<int> deleteLocalFile(String localID) async {
- final db = await instance.database;
- return db.delete(
- table,
- where: '$columnLocalID =?',
- whereArgs: [localID],
- );
- }
- Future<int> deleteFromCollection(int uploadedFileID, int collectionID) async {
- final db = await instance.database;
- return db.delete(
- table,
- where: '$columnUploadedFileID = ? AND $columnCollectionID = ?',
- whereArgs: [uploadedFileID, collectionID],
- );
- }
- Future<int> deleteCollection(int collectionID) async {
- final db = await instance.database;
- return db.delete(
- table,
- where: '$columnCollectionID = ?',
- whereArgs: [collectionID],
- );
- }
- Future<int> removeFromCollection(int collectionID, List<int> fileIDs) async {
- final db = await instance.database;
- return db.delete(
- table,
- where:
- '$columnCollectionID =? AND $columnUploadedFileID IN (${fileIDs.join(', ')})',
- whereArgs: [collectionID],
- );
- }
- Future<List<String>> getLocalPaths() async {
- final db = await instance.database;
- final rows = await db.query(
- table,
- columns: [columnDeviceFolder],
- distinct: true,
- );
- List<String> result = List<String>();
- for (final row in rows) {
- result.add(row[columnDeviceFolder]);
- }
- return result;
- }
- Future<File> getLatestFileInCollection(int collectionID) async {
- final db = await instance.database;
- final rows = await db.query(
- table,
- where: '$columnCollectionID = ? AND $columnIsDeleted = 0',
- whereArgs: [collectionID],
- orderBy: '$columnCreationTime DESC',
- limit: 1,
- );
- if (rows.isNotEmpty) {
- return _getFileFromRow(rows[0]);
- } else {
- return null;
- }
- }
- Future<File> getLastModifiedFileInCollection(int collectionID) async {
- final db = await instance.database;
- final rows = await db.query(
- table,
- where: '$columnCollectionID = ? AND $columnIsDeleted = 0',
- whereArgs: [collectionID],
- orderBy: '$columnUpdationTime DESC',
- limit: 1,
- );
- if (rows.isNotEmpty) {
- return _getFileFromRow(rows[0]);
- } else {
- return null;
- }
- }
- Future<bool> doesFileExistInCollection(
- int uploadedFileID, int collectionID) async {
- final db = await instance.database;
- final rows = await db.query(
- table,
- where: '$columnUploadedFileID = ? AND $columnCollectionID = ?',
- whereArgs: [uploadedFileID, collectionID],
- limit: 1,
- );
- return rows.isNotEmpty;
- }
- List<File> _convertToFiles(List<Map<String, dynamic>> results) {
- final files = List<File>();
- for (final result in results) {
- files.add(_getFileFromRow(result));
- }
- return files;
- }
- Map<String, dynamic> _getRowForFile(File file) {
- final row = new Map<String, dynamic>();
- row[columnLocalID] = file.localID;
- row[columnUploadedFileID] = file.uploadedFileID;
- row[columnOwnerID] = file.ownerID;
- row[columnCollectionID] = file.collectionID;
- row[columnTitle] = file.title;
- row[columnDeviceFolder] = file.deviceFolder;
- if (file.location != null) {
- row[columnLatitude] = file.location.latitude;
- row[columnLongitude] = file.location.longitude;
- }
- switch (file.fileType) {
- case FileType.image:
- row[columnFileType] = 0;
- break;
- case FileType.video:
- row[columnFileType] = 1;
- break;
- default:
- row[columnFileType] = -1;
- }
- row[columnIsEncrypted] = file.isEncrypted ? 1 : 0;
- row[columnCreationTime] = file.creationTime;
- row[columnModificationTime] = file.modificationTime;
- row[columnUpdationTime] = file.updationTime;
- row[columnEncryptedKey] = file.encryptedKey;
- row[columnKeyDecryptionNonce] = file.keyDecryptionNonce;
- row[columnFileDecryptionHeader] = file.fileDecryptionHeader;
- row[columnThumbnailDecryptionHeader] = file.thumbnailDecryptionHeader;
- row[columnMetadataDecryptionHeader] = file.metadataDecryptionHeader;
- return row;
- }
- File _getFileFromRow(Map<String, dynamic> row) {
- final file = File();
- file.generatedID = row[columnGeneratedID];
- file.localID = row[columnLocalID];
- file.uploadedFileID = row[columnUploadedFileID];
- file.ownerID = row[columnOwnerID];
- file.collectionID = row[columnCollectionID];
- file.title = row[columnTitle];
- file.deviceFolder = row[columnDeviceFolder];
- if (row[columnLatitude] != null && row[columnLongitude] != null) {
- file.location = Location(row[columnLatitude], row[columnLongitude]);
- }
- file.fileType = getFileType(row[columnFileType]);
- file.isEncrypted = row[columnIsEncrypted] == 1;
- file.creationTime = int.parse(row[columnCreationTime]);
- file.modificationTime = int.parse(row[columnModificationTime]);
- file.updationTime = row[columnUpdationTime] == null
- ? -1
- : int.parse(row[columnUpdationTime]);
- file.encryptedKey = row[columnEncryptedKey];
- file.keyDecryptionNonce = row[columnKeyDecryptionNonce];
- file.fileDecryptionHeader = row[columnFileDecryptionHeader];
- file.thumbnailDecryptionHeader = row[columnThumbnailDecryptionHeader];
- file.metadataDecryptionHeader = row[columnMetadataDecryptionHeader];
- return file;
- }
- }
|