files_db.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. import 'dart:io';
  2. import 'package:logging/logging.dart';
  3. import 'package:photos/models/file_type.dart';
  4. import 'package:photos/models/location.dart';
  5. import 'package:photos/models/file.dart';
  6. import 'package:path/path.dart';
  7. import 'package:sqflite/sqflite.dart';
  8. import 'package:path_provider/path_provider.dart';
  9. class FilesDB {
  10. static final _databaseName = "ente.files.db";
  11. static final _databaseVersion = 1;
  12. static final Logger _logger = Logger("FilesDB");
  13. static final table = 'files';
  14. static final columnGeneratedID = '_id';
  15. static final columnUploadedFileID = 'uploaded_file_id';
  16. static final columnOwnerID = 'owner_id';
  17. static final columnCollectionID = 'collection_id';
  18. static final columnLocalID = 'local_id';
  19. static final columnTitle = 'title';
  20. static final columnDeviceFolder = 'device_folder';
  21. static final columnLatitude = 'latitude';
  22. static final columnLongitude = 'longitude';
  23. static final columnFileType = 'file_type';
  24. static final columnRemoteFolderID = 'remote_folder_id';
  25. static final columnIsEncrypted = 'is_encrypted';
  26. static final columnIsDeleted = 'is_deleted';
  27. static final columnCreationTime = 'creation_time';
  28. static final columnModificationTime = 'modification_time';
  29. static final columnUpdationTime = 'updation_time';
  30. static final columnEncryptedKey = 'encrypted_key';
  31. static final columnKeyDecryptionNonce = 'key_decryption_nonce';
  32. static final columnFileDecryptionHeader = 'file_decryption_header';
  33. static final columnThumbnailDecryptionHeader = 'thumbnail_decryption_header';
  34. static final columnMetadataDecryptionHeader = 'metadata_decryption_header';
  35. // make this a singleton class
  36. FilesDB._privateConstructor();
  37. static final FilesDB instance = FilesDB._privateConstructor();
  38. // only have a single app-wide reference to the database
  39. static Database _database;
  40. Future<Database> get database async {
  41. if (_database != null) return _database;
  42. // lazily instantiate the db the first time it is accessed
  43. _database = await _initDatabase();
  44. return _database;
  45. }
  46. // this opens the database (and creates it if it doesn't exist)
  47. _initDatabase() async {
  48. Directory documentsDirectory = await getApplicationDocumentsDirectory();
  49. String path = join(documentsDirectory.path, _databaseName);
  50. return await openDatabase(path,
  51. version: _databaseVersion, onCreate: _onCreate);
  52. }
  53. // SQL code to create the database table
  54. Future _onCreate(Database db, int version) async {
  55. await db.execute('''
  56. CREATE TABLE $table (
  57. $columnGeneratedID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  58. $columnLocalID TEXT,
  59. $columnUploadedFileID INTEGER,
  60. $columnOwnerID INTEGER,
  61. $columnCollectionID INTEGER,
  62. $columnTitle TEXT NOT NULL,
  63. $columnDeviceFolder TEXT NOT NULL,
  64. $columnLatitude REAL,
  65. $columnLongitude REAL,
  66. $columnFileType INTEGER,
  67. $columnRemoteFolderID INTEGER,
  68. $columnIsEncrypted INTEGER DEFAULT 1,
  69. $columnModificationTime TEXT NOT NULL,
  70. $columnEncryptedKey TEXT,
  71. $columnKeyDecryptionNonce TEXT,
  72. $columnFileDecryptionHeader TEXT,
  73. $columnThumbnailDecryptionHeader TEXT,
  74. $columnMetadataDecryptionHeader TEXT,
  75. $columnIsDeleted INTEGER DEFAULT 0,
  76. $columnCreationTime TEXT NOT NULL,
  77. $columnUpdationTime TEXT,
  78. UNIQUE($columnUploadedFileID, $columnCollectionID)
  79. )
  80. ''');
  81. }
  82. Future<int> insert(File file) async {
  83. final db = await instance.database;
  84. return await db.insert(table, _getRowForFile(file));
  85. }
  86. Future<List<dynamic>> insertMultiple(List<File> files) async {
  87. final db = await instance.database;
  88. var batch = db.batch();
  89. int batchCounter = 0;
  90. for (File file in files) {
  91. if (batchCounter == 400) {
  92. await batch.commit();
  93. batch = db.batch();
  94. }
  95. batch.insert(
  96. table,
  97. _getRowForFile(file),
  98. conflictAlgorithm: ConflictAlgorithm.replace,
  99. );
  100. batchCounter++;
  101. }
  102. return await batch.commit();
  103. }
  104. Future<File> getFile(int generatedID) async {
  105. final db = await instance.database;
  106. final results = await db.query(table,
  107. where: '$columnGeneratedID = ?', whereArgs: [generatedID]);
  108. return _convertToFiles(results)[0];
  109. }
  110. Future<List<File>> getFiles() async {
  111. final db = await instance.database;
  112. final results = await db.query(
  113. table,
  114. where: '$columnIsDeleted = 0',
  115. orderBy: '$columnCreationTime DESC',
  116. );
  117. return _convertToFiles(results);
  118. }
  119. Future<List<File>> getAllVideos() async {
  120. final db = await instance.database;
  121. final results = await db.query(
  122. table,
  123. where:
  124. '$columnLocalID IS NOT NULL AND $columnFileType = 1 AND $columnIsDeleted = 0',
  125. orderBy: '$columnCreationTime DESC',
  126. );
  127. return _convertToFiles(results);
  128. }
  129. Future<List<File>> getAllInCollectionBeforeCreationTime(
  130. int collectionID, int beforeCreationTime, int limit) async {
  131. final db = await instance.database;
  132. final results = await db.query(
  133. table,
  134. where:
  135. '$columnCollectionID = ? AND $columnIsDeleted = 0 AND $columnCreationTime < ?',
  136. whereArgs: [collectionID, beforeCreationTime],
  137. orderBy: '$columnCreationTime DESC',
  138. limit: limit,
  139. );
  140. return _convertToFiles(results);
  141. }
  142. Future<List<File>> getAllInCollection(int collectionID) async {
  143. final db = await instance.database;
  144. final results = await db.query(
  145. table,
  146. where: '$columnCollectionID = ?',
  147. whereArgs: [collectionID],
  148. orderBy: '$columnCreationTime DESC',
  149. );
  150. return _convertToFiles(results);
  151. }
  152. Future<List<File>> getFilesCreatedWithinDuration(
  153. int startCreationTime, int endCreationTime) async {
  154. final db = await instance.database;
  155. final results = await db.query(
  156. table,
  157. where:
  158. '$columnCreationTime > ? AND $columnCreationTime < ? AND $columnIsDeleted = 0',
  159. whereArgs: [startCreationTime, endCreationTime],
  160. orderBy: '$columnCreationTime ASC',
  161. );
  162. return _convertToFiles(results);
  163. }
  164. Future<List<File>> getAllDeleted() async {
  165. final db = await instance.database;
  166. final results = await db.query(
  167. table,
  168. where: '$columnIsDeleted = 1',
  169. orderBy: '$columnCreationTime DESC',
  170. );
  171. return _convertToFiles(results);
  172. }
  173. Future<List<File>> getFilesToBeUploadedWithinFolders(
  174. Set<String> folders) async {
  175. final db = await instance.database;
  176. String inParam = "";
  177. for (final folder in folders) {
  178. inParam += "'" + folder + "',";
  179. }
  180. inParam = inParam.substring(0, inParam.length - 1);
  181. final results = await db.query(
  182. table,
  183. where:
  184. '$columnUploadedFileID IS NULL AND $columnDeviceFolder IN ($inParam)',
  185. orderBy: '$columnCreationTime DESC',
  186. );
  187. return _convertToFiles(results);
  188. }
  189. Future<List<File>> getMatchingFiles(
  190. String title, String deviceFolder, int creationTime, int modificationTime,
  191. {String alternateTitle}) async {
  192. final db = await instance.database;
  193. final rows = await db.query(
  194. table,
  195. where: '''($columnTitle=? OR $columnTitle=?) AND
  196. $columnDeviceFolder=? AND $columnCreationTime=? AND
  197. $columnModificationTime=?''',
  198. whereArgs: [
  199. title,
  200. alternateTitle,
  201. deviceFolder,
  202. creationTime,
  203. modificationTime,
  204. ],
  205. );
  206. if (rows.isNotEmpty) {
  207. return _convertToFiles(rows);
  208. } else {
  209. return null;
  210. }
  211. }
  212. Future<File> getMatchingRemoteFile(int uploadedFileID) async {
  213. final db = await instance.database;
  214. final rows = await db.query(
  215. table,
  216. where: '$columnUploadedFileID=?',
  217. whereArgs: [uploadedFileID],
  218. );
  219. if (rows.isNotEmpty) {
  220. return _getFileFromRow(rows[0]);
  221. } else {
  222. throw ("No matching file found");
  223. }
  224. }
  225. Future<int> update(File file) async {
  226. final db = await instance.database;
  227. return await db.update(
  228. table,
  229. _getRowForFile(file),
  230. where: '$columnGeneratedID = ?',
  231. whereArgs: [file.generatedID],
  232. );
  233. }
  234. Future<int> markForDeletion(File file) async {
  235. final db = await instance.database;
  236. final values = new Map<String, dynamic>();
  237. values[columnIsDeleted] = 1;
  238. return db.update(
  239. table,
  240. values,
  241. where: '$columnGeneratedID =?',
  242. whereArgs: [file.generatedID],
  243. );
  244. }
  245. Future<int> delete(File file) async {
  246. final db = await instance.database;
  247. return db.delete(
  248. table,
  249. where: '$columnGeneratedID =?',
  250. whereArgs: [file.generatedID],
  251. );
  252. }
  253. Future<int> deleteFromCollection(int uploadedFileID, int collectionID) async {
  254. final db = await instance.database;
  255. return db.delete(
  256. table,
  257. where: '$columnUploadedFileID = ? AND $columnCollectionID = ?',
  258. whereArgs: [uploadedFileID, collectionID],
  259. );
  260. }
  261. Future<int> removeFromCollection(int collectionID, List<int> fileIDs) async {
  262. final db = await instance.database;
  263. return db.delete(
  264. table,
  265. where:
  266. '$columnCollectionID =? AND $columnUploadedFileID IN (${fileIDs.join(', ')})',
  267. whereArgs: [collectionID],
  268. );
  269. }
  270. Future<List<String>> getLocalPaths() async {
  271. final db = await instance.database;
  272. final rows = await db.query(
  273. table,
  274. columns: [columnDeviceFolder],
  275. distinct: true,
  276. where: '$columnRemoteFolderID IS NULL',
  277. );
  278. List<String> result = List<String>();
  279. for (final row in rows) {
  280. result.add(row[columnDeviceFolder]);
  281. }
  282. return result;
  283. }
  284. Future<File> getLatestFileInCollection(int collectionID) async {
  285. final db = await instance.database;
  286. final rows = await db.query(
  287. table,
  288. where: '$columnCollectionID =?',
  289. whereArgs: [collectionID],
  290. orderBy: '$columnCreationTime DESC',
  291. limit: 1,
  292. );
  293. if (rows.isNotEmpty) {
  294. return _getFileFromRow(rows[0]);
  295. } else {
  296. return null;
  297. }
  298. }
  299. Future<File> getLastModifiedFileInCollection(int collectionID) async {
  300. final db = await instance.database;
  301. final rows = await db.query(
  302. table,
  303. where: '$columnCollectionID =?',
  304. whereArgs: [collectionID],
  305. orderBy: '$columnUpdationTime DESC',
  306. limit: 1,
  307. );
  308. if (rows.isNotEmpty) {
  309. return _getFileFromRow(rows[0]);
  310. } else {
  311. return null;
  312. }
  313. }
  314. Future<bool> doesFileExistInCollection(
  315. int uploadedFileID, int collectionID) async {
  316. final db = await instance.database;
  317. final rows = await db.query(
  318. table,
  319. where: '$columnUploadedFileID = ? AND $columnCollectionID = ?',
  320. whereArgs: [uploadedFileID, collectionID],
  321. limit: 1,
  322. );
  323. return rows.isNotEmpty;
  324. }
  325. List<File> _convertToFiles(List<Map<String, dynamic>> results) {
  326. final files = List<File>();
  327. for (final result in results) {
  328. files.add(_getFileFromRow(result));
  329. }
  330. return files;
  331. }
  332. Map<String, dynamic> _getRowForFile(File file) {
  333. final row = new Map<String, dynamic>();
  334. row[columnLocalID] = file.localID;
  335. row[columnUploadedFileID] = file.uploadedFileID;
  336. row[columnOwnerID] = file.ownerID;
  337. row[columnCollectionID] = file.collectionID;
  338. row[columnTitle] = file.title;
  339. row[columnDeviceFolder] = file.deviceFolder;
  340. if (file.location != null) {
  341. row[columnLatitude] = file.location.latitude;
  342. row[columnLongitude] = file.location.longitude;
  343. }
  344. switch (file.fileType) {
  345. case FileType.image:
  346. row[columnFileType] = 0;
  347. break;
  348. case FileType.video:
  349. row[columnFileType] = 1;
  350. break;
  351. default:
  352. row[columnFileType] = -1;
  353. }
  354. row[columnIsEncrypted] = file.isEncrypted ? 1 : 0;
  355. row[columnRemoteFolderID] = file.remoteFolderID;
  356. row[columnCreationTime] = file.creationTime;
  357. row[columnModificationTime] = file.modificationTime;
  358. row[columnUpdationTime] = file.updationTime;
  359. row[columnEncryptedKey] = file.encryptedKey;
  360. row[columnKeyDecryptionNonce] = file.keyDecryptionNonce;
  361. row[columnFileDecryptionHeader] = file.fileDecryptionHeader;
  362. row[columnThumbnailDecryptionHeader] = file.thumbnailDecryptionHeader;
  363. row[columnMetadataDecryptionHeader] = file.metadataDecryptionHeader;
  364. return row;
  365. }
  366. File _getFileFromRow(Map<String, dynamic> row) {
  367. final file = File();
  368. file.generatedID = row[columnGeneratedID];
  369. file.localID = row[columnLocalID];
  370. file.uploadedFileID = row[columnUploadedFileID];
  371. file.ownerID = row[columnOwnerID];
  372. file.collectionID = row[columnCollectionID];
  373. file.title = row[columnTitle];
  374. file.deviceFolder = row[columnDeviceFolder];
  375. if (row[columnLatitude] != null && row[columnLongitude] != null) {
  376. file.location = Location(row[columnLatitude], row[columnLongitude]);
  377. }
  378. file.fileType = getFileType(row[columnFileType]);
  379. file.remoteFolderID = row[columnRemoteFolderID];
  380. file.isEncrypted = row[columnIsEncrypted] == 1;
  381. file.creationTime = int.parse(row[columnCreationTime]);
  382. file.modificationTime = int.parse(row[columnModificationTime]);
  383. file.updationTime = row[columnUpdationTime] == null
  384. ? -1
  385. : int.parse(row[columnUpdationTime]);
  386. file.encryptedKey = row[columnEncryptedKey];
  387. file.keyDecryptionNonce = row[columnKeyDecryptionNonce];
  388. file.fileDecryptionHeader = row[columnFileDecryptionHeader];
  389. file.thumbnailDecryptionHeader = row[columnThumbnailDecryptionHeader];
  390. file.metadataDecryptionHeader = row[columnMetadataDecryptionHeader];
  391. return file;
  392. }
  393. }