ente/lib/db/files_db.dart

392 lines
12 KiB
Dart
Raw Normal View History

2020-03-24 19:59:36 +00:00
import 'dart:io';
import 'package:logging/logging.dart';
2020-06-19 23:03:26 +00:00
import 'package:photos/models/file_type.dart';
2020-06-03 16:06:49 +00:00
import 'package:photos/models/location.dart';
2020-06-19 23:03:26 +00:00
import 'package:photos/models/file.dart';
2020-03-24 19:59:36 +00:00
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
2020-07-20 11:03:09 +00:00
class FilesDB {
2020-06-19 23:03:26 +00:00
static final _databaseName = "ente.files.db";
2020-03-24 19:59:36 +00:00
static final _databaseVersion = 1;
2020-07-20 11:03:09 +00:00
static final Logger _logger = Logger("FilesDB");
2020-06-19 23:03:26 +00:00
static final table = 'files';
2020-03-26 14:39:31 +00:00
2020-08-09 22:34:59 +00:00
static final columnGeneratedID = '_id';
static final columnUploadedFileID = 'uploaded_file_id';
static final columnOwnerID = 'owner_id';
static final columnLocalID = 'local_id';
2020-04-24 12:40:24 +00:00
static final columnTitle = 'title';
2020-05-17 12:39:38 +00:00
static final columnDeviceFolder = 'device_folder';
2020-06-03 14:11:34 +00:00
static final columnLatitude = 'latitude';
static final columnLongitude = 'longitude';
2020-06-19 23:03:26 +00:00
static final columnFileType = 'file_type';
2020-08-09 22:34:59 +00:00
static final columnRemoteFolderID = 'remote_folder_id';
2020-08-12 20:42:00 +00:00
static final columnIsEncrypted = 'is_encrypted';
2020-04-12 12:38:49 +00:00
static final columnIsDeleted = 'is_deleted';
static final columnCreationTime = 'creation_time';
static final columnModificationTime = 'modification_time';
static final columnUpdationTime = 'updation_time';
2020-03-24 19:59:36 +00:00
// make this a singleton class
2020-07-20 11:03:09 +00:00
FilesDB._privateConstructor();
static final FilesDB instance = FilesDB._privateConstructor();
2020-03-24 19:59:36 +00:00
// 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;
}
2020-03-26 14:39:31 +00:00
2020-03-24 19:59:36 +00:00
// 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,
2020-03-26 14:39:31 +00:00
version: _databaseVersion, onCreate: _onCreate);
2020-03-24 19:59:36 +00:00
}
// SQL code to create the database table
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE $table (
2020-08-09 22:34:59 +00:00
$columnGeneratedID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
$columnLocalID TEXT,
$columnUploadedFileID INTEGER,
$columnOwnerID INTEGER,
2020-04-24 12:40:24 +00:00
$columnTitle TEXT NOT NULL,
2020-05-17 12:39:38 +00:00
$columnDeviceFolder TEXT NOT NULL,
2020-06-03 14:11:34 +00:00
$columnLatitude REAL,
$columnLongitude REAL,
2020-06-19 23:03:26 +00:00
$columnFileType INTEGER,
2020-08-09 22:34:59 +00:00
$columnRemoteFolderID INTEGER,
2020-08-12 20:42:00 +00:00
$columnIsEncrypted INTEGER DEFAULT 0,
2020-04-12 12:38:49 +00:00
$columnIsDeleted INTEGER DEFAULT 0,
$columnCreationTime TEXT NOT NULL,
$columnModificationTime TEXT NOT NULL,
$columnUpdationTime TEXT
2020-03-24 19:59:36 +00:00
)
''');
}
2020-03-26 14:39:31 +00:00
2020-06-19 23:03:26 +00:00
Future<int> insert(File file) async {
2020-05-06 18:13:24 +00:00
final db = await instance.database;
2020-06-19 23:03:26 +00:00
return await db.insert(table, _getRowForFile(file));
2020-03-30 14:28:46 +00:00
}
2020-06-19 23:03:26 +00:00
Future<List<dynamic>> insertMultiple(List<File> files) async {
2020-05-06 18:13:24 +00:00
final db = await instance.database;
2020-03-30 14:28:46 +00:00
var batch = db.batch();
int batchCounter = 0;
2020-06-19 23:03:26 +00:00
for (File file in files) {
2020-03-30 14:28:46 +00:00
if (batchCounter == 400) {
await batch.commit();
batch = db.batch();
}
2020-06-19 23:03:26 +00:00
batch.insert(table, _getRowForFile(file));
2020-03-30 14:28:46 +00:00
batchCounter++;
}
return await batch.commit();
2020-03-26 14:39:31 +00:00
}
2020-08-09 22:34:59 +00:00
Future<List<File>> getOwnedFiles(int ownerID) async {
2020-05-06 18:13:24 +00:00
final db = await instance.database;
2020-08-09 22:34:59 +00:00
final whereArgs = List<dynamic>();
if (ownerID != null) {
whereArgs.add(ownerID);
}
2020-05-06 18:13:24 +00:00
final results = await db.query(
table,
2020-08-09 22:34:59 +00:00
// where: '$columnIsDeleted = 0' +
// (ownerID == null ? '' : ' AND $columnOwnerID = ?'),
// whereArgs: whereArgs,
orderBy: '$columnCreationTime DESC',
2020-05-06 18:13:24 +00:00
);
2020-06-19 23:03:26 +00:00
return _convertToFiles(results);
2020-04-12 12:38:49 +00:00
}
2020-06-21 00:05:15 +00:00
Future<List<File>> getAllVideos() async {
final db = await instance.database;
final results = await db.query(
table,
where:
2020-08-09 22:34:59 +00:00
'$columnLocalID IS NOT NULL AND $columnFileType = 1 AND $columnIsDeleted = 0',
orderBy: '$columnCreationTime DESC',
2020-06-21 00:05:15 +00:00
);
return _convertToFiles(results);
}
2020-07-19 21:26:26 +00:00
Future<List<File>> getAllInFolder(
2020-08-09 22:34:59 +00:00
int folderID, int beforeCreationTime, int limit) async {
2020-05-25 14:54:54 +00:00
final db = await instance.database;
final results = await db.query(
table,
2020-07-14 12:15:55 +00:00
where:
2020-08-09 22:34:59 +00:00
'$columnRemoteFolderID = ? AND $columnIsDeleted = 0 AND $columnCreationTime < ?',
whereArgs: [folderID, beforeCreationTime],
orderBy: '$columnCreationTime DESC',
limit: limit,
2020-05-25 14:54:54 +00:00
);
2020-06-19 23:03:26 +00:00
return _convertToFiles(results);
2020-05-25 14:54:54 +00:00
}
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],
2020-07-21 08:10:44 +00:00
orderBy: '$columnCreationTime ASC',
);
return _convertToFiles(results);
}
2020-06-19 23:03:26 +00:00
Future<List<File>> getAllDeleted() async {
2020-05-06 18:13:24 +00:00
final db = await instance.database;
final results = await db.query(
table,
where: '$columnIsDeleted = 1',
orderBy: '$columnCreationTime DESC',
2020-05-06 18:13:24 +00:00
);
2020-06-19 23:03:26 +00:00
return _convertToFiles(results);
2020-03-27 16:07:55 +00:00
}
2020-06-19 23:03:26 +00:00
Future<List<File>> getFilesToBeUploaded() async {
2020-05-06 18:13:24 +00:00
final db = await instance.database;
final results = await db.query(
table,
2020-08-09 22:34:59 +00:00
where: '$columnUploadedFileID IS NULL',
orderBy: '$columnCreationTime DESC',
2020-05-06 18:13:24 +00:00
);
2020-06-19 23:03:26 +00:00
return _convertToFiles(results);
2020-03-24 19:59:36 +00:00
}
2020-08-09 22:34:59 +00:00
Future<File> getMatchingFile(String localID, String title,
String deviceFolder, int creationTime, int modificationTime,
{String alternateTitle}) async {
final db = await instance.database;
final rows = await db.query(
table,
2020-08-09 22:34:59 +00:00
where: '''$columnLocalID=? AND ($columnTitle=? OR $columnTitle=?) AND
$columnDeviceFolder=? AND $columnCreationTime=? AND $columnModificationTime=?''',
whereArgs: [
2020-08-09 22:34:59 +00:00
localID,
title,
alternateTitle,
deviceFolder,
creationTime,
modificationTime
],
);
if (rows.isNotEmpty) {
2020-06-19 23:03:26 +00:00
return _getFileFromRow(rows[0]);
} else {
2020-06-19 23:03:26 +00:00
throw ("No matching file found");
}
}
2020-08-09 22:34:59 +00:00
Future<File> getMatchingRemoteFile(int uploadedFileID) async {
final db = await instance.database;
final rows = await db.query(
table,
2020-08-09 22:34:59 +00:00
where: '$columnUploadedFileID=?',
whereArgs: [uploadedFileID],
);
if (rows.isNotEmpty) {
2020-06-19 23:03:26 +00:00
return _getFileFromRow(rows[0]);
} else {
2020-06-19 23:03:26 +00:00
throw ("No matching file found");
}
}
2020-08-09 22:34:59 +00:00
Future<int> update(int generatedID, int uploadedID, int updationTime) async {
2020-05-06 18:13:24 +00:00
final db = await instance.database;
final values = new Map<String, dynamic>();
2020-08-09 22:34:59 +00:00
values[columnUploadedFileID] = uploadedID;
values[columnUpdationTime] = updationTime;
2020-05-06 18:13:24 +00:00
return await db.update(
table,
values,
2020-08-09 22:34:59 +00:00
where: '$columnGeneratedID = ?',
whereArgs: [generatedID],
2020-05-06 18:13:24 +00:00
);
2020-03-24 19:59:36 +00:00
}
2020-06-19 23:03:26 +00:00
// TODO: Remove deleted files on remote
Future<int> markForDeletion(File file) async {
2020-05-06 18:13:24 +00:00
final db = await instance.database;
2020-07-20 11:49:00 +00:00
final values = new Map<String, dynamic>();
2020-04-12 12:38:49 +00:00
values[columnIsDeleted] = 1;
2020-05-06 18:13:24 +00:00
return db.update(
table,
values,
2020-08-09 22:34:59 +00:00
where: '$columnGeneratedID =?',
whereArgs: [file.generatedID],
2020-05-06 18:13:24 +00:00
);
2020-04-12 12:38:49 +00:00
}
2020-06-19 23:03:26 +00:00
Future<int> delete(File file) async {
2020-05-06 18:13:24 +00:00
final db = await instance.database;
return db.delete(
table,
2020-08-09 22:34:59 +00:00
where: '$columnGeneratedID =?',
whereArgs: [file.generatedID],
2020-05-06 18:13:24 +00:00
);
}
2020-08-09 22:34:59 +00:00
Future<int> deleteFilesInRemoteFolder(int folderID) async {
2020-05-22 18:22:55 +00:00
final db = await instance.database;
return db.delete(
table,
2020-08-09 22:34:59 +00:00
where: '$columnRemoteFolderID =?',
whereArgs: [folderID],
2020-05-22 18:22:55 +00:00
);
}
Future<List<String>> getLocalPaths() async {
2020-05-06 18:13:24 +00:00
final db = await instance.database;
final rows = await db.query(
table,
2020-05-17 12:39:38 +00:00
columns: [columnDeviceFolder],
2020-05-06 18:13:24 +00:00
distinct: true,
2020-08-09 22:34:59 +00:00
where: '$columnRemoteFolderID IS NULL',
2020-05-06 18:13:24 +00:00
);
List<String> result = List<String>();
for (final row in rows) {
2020-05-17 12:39:38 +00:00
result.add(row[columnDeviceFolder]);
2020-05-06 18:13:24 +00:00
}
return result;
}
2020-06-19 23:03:26 +00:00
Future<File> getLatestFileInPath(String path) async {
2020-05-06 18:13:24 +00:00
final db = await instance.database;
2020-07-20 11:49:00 +00:00
final rows = await db.query(
2020-05-06 18:13:24 +00:00
table,
2020-05-17 12:39:38 +00:00
where: '$columnDeviceFolder =?',
2020-05-06 18:13:24 +00:00
whereArgs: [path],
orderBy: '$columnCreationTime DESC',
2020-05-06 18:13:24 +00:00
limit: 1,
);
if (rows.isNotEmpty) {
2020-06-19 23:03:26 +00:00
return _getFileFromRow(rows[0]);
2020-05-06 18:13:24 +00:00
} else {
2020-06-19 23:03:26 +00:00
throw ("No file found in path");
2020-05-06 18:13:24 +00:00
}
}
2020-08-09 22:34:59 +00:00
Future<File> getLatestFileInRemoteFolder(int folderID) async {
2020-05-22 18:22:55 +00:00
final db = await instance.database;
2020-07-20 11:49:00 +00:00
final rows = await db.query(
2020-05-22 18:22:55 +00:00
table,
2020-08-09 22:34:59 +00:00
where: '$columnRemoteFolderID =?',
whereArgs: [folderID],
orderBy: '$columnCreationTime DESC',
2020-05-22 18:22:55 +00:00
limit: 1,
);
if (rows.isNotEmpty) {
2020-06-19 23:03:26 +00:00
return _getFileFromRow(rows[0]);
2020-05-22 18:22:55 +00:00
} else {
2020-08-09 22:34:59 +00:00
throw ("No file found in remote folder " + folderID.toString());
2020-05-22 18:22:55 +00:00
}
}
2020-08-09 22:34:59 +00:00
Future<File> getLastSyncedFileInRemoteFolder(int folderID) async {
final db = await instance.database;
2020-07-20 11:49:00 +00:00
final rows = await db.query(
table,
2020-08-09 22:34:59 +00:00
where: '$columnRemoteFolderID =?',
whereArgs: [folderID],
orderBy: '$columnUpdationTime DESC',
limit: 1,
);
if (rows.isNotEmpty) {
2020-06-19 23:03:26 +00:00
return _getFileFromRow(rows[0]);
} else {
2020-08-09 22:34:59 +00:00
throw ("No file found in remote folder " + folderID.toString());
}
}
2020-08-09 22:34:59 +00:00
Future<File> getLatestFileAmongGeneratedIDs(List<String> generatedIDs) async {
2020-05-06 18:13:24 +00:00
final db = await instance.database;
2020-07-20 11:49:00 +00:00
final rows = await db.query(
2020-05-06 18:13:24 +00:00
table,
2020-08-09 22:34:59 +00:00
where: '$columnGeneratedID IN (${generatedIDs.join(",")})',
orderBy: '$columnCreationTime DESC',
2020-05-06 18:13:24 +00:00
limit: 1,
);
if (rows.isNotEmpty) {
2020-06-19 23:03:26 +00:00
return _getFileFromRow(rows[0]);
2020-05-06 18:13:24 +00:00
} else {
2020-08-09 22:34:59 +00:00
throw ("No file found with ids " + generatedIDs.join(", ").toString());
2020-05-06 18:13:24 +00:00
}
2020-04-12 12:38:49 +00:00
}
2020-06-19 23:03:26 +00:00
List<File> _convertToFiles(List<Map<String, dynamic>> results) {
2020-07-20 11:49:00 +00:00
final files = List<File>();
for (final result in results) {
2020-06-19 23:03:26 +00:00
files.add(_getFileFromRow(result));
2020-03-28 13:56:06 +00:00
}
2020-06-19 23:03:26 +00:00
return files;
2020-03-28 13:56:06 +00:00
}
2020-03-30 14:28:46 +00:00
2020-06-19 23:03:26 +00:00
Map<String, dynamic> _getRowForFile(File file) {
2020-07-20 11:49:00 +00:00
final row = new Map<String, dynamic>();
2020-08-09 22:34:59 +00:00
row[columnLocalID] = file.localID;
row[columnUploadedFileID] = file.uploadedFileID;
row[columnOwnerID] = file.ownerID;
2020-06-19 23:03:26 +00:00
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;
}
2020-08-12 20:42:00 +00:00
row[columnIsEncrypted] = file.isEncrypted ? 1 : 0;
2020-08-09 22:34:59 +00:00
row[columnRemoteFolderID] = file.remoteFolderID;
row[columnCreationTime] = file.creationTime;
row[columnModificationTime] = file.modificationTime;
row[columnUpdationTime] = file.updationTime;
2020-03-30 14:28:46 +00:00
return row;
}
2020-04-12 12:38:49 +00:00
2020-06-19 23:03:26 +00:00
File _getFileFromRow(Map<String, dynamic> row) {
2020-07-20 11:49:00 +00:00
final file = File();
2020-08-09 22:34:59 +00:00
file.generatedID = row[columnGeneratedID];
file.localID = row[columnLocalID];
file.uploadedFileID = row[columnUploadedFileID];
file.ownerID = row[columnUploadedFileID];
2020-06-19 23:03:26 +00:00
file.title = row[columnTitle];
file.deviceFolder = row[columnDeviceFolder];
2020-06-06 21:12:49 +00:00
if (row[columnLatitude] != null && row[columnLongitude] != null) {
2020-06-19 23:03:26 +00:00
file.location = Location(row[columnLatitude], row[columnLongitude]);
2020-06-06 21:12:49 +00:00
}
2020-06-19 23:03:26 +00:00
file.fileType = getFileType(row[columnFileType]);
2020-08-09 22:34:59 +00:00
file.remoteFolderID = row[columnRemoteFolderID];
2020-08-12 20:42:00 +00:00
file.isEncrypted = int.parse(row[columnIsEncrypted]) == 1;
file.creationTime = int.parse(row[columnCreationTime]);
file.modificationTime = int.parse(row[columnModificationTime]);
file.updationTime = row[columnUpdationTime] == null
2020-04-12 12:38:49 +00:00
? -1
: int.parse(row[columnUpdationTime]);
2020-06-19 23:03:26 +00:00
return file;
2020-04-12 12:38:49 +00:00
}
2020-03-26 14:39:31 +00:00
}