files_db.dart 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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 columnIsEncrypted = 'is_encrypted';
  25. static final columnIsDeleted = 'is_deleted';
  26. static final columnCreationTime = 'creation_time';
  27. static final columnModificationTime = 'modification_time';
  28. static final columnUpdationTime = 'updation_time';
  29. static final columnEncryptedKey = 'encrypted_key';
  30. static final columnKeyDecryptionNonce = 'key_decryption_nonce';
  31. static final columnFileDecryptionHeader = 'file_decryption_header';
  32. static final columnThumbnailDecryptionHeader = 'thumbnail_decryption_header';
  33. static final columnMetadataDecryptionHeader = 'metadata_decryption_header';
  34. // make this a singleton class
  35. FilesDB._privateConstructor();
  36. static final FilesDB instance = FilesDB._privateConstructor();
  37. // only have a single app-wide reference to the database
  38. static Database _database;
  39. Future<Database> get database async {
  40. if (_database != null) return _database;
  41. // lazily instantiate the db the first time it is accessed
  42. _database = await _initDatabase();
  43. return _database;
  44. }
  45. // this opens the database (and creates it if it doesn't exist)
  46. _initDatabase() async {
  47. Directory documentsDirectory = await getApplicationDocumentsDirectory();
  48. String path = join(documentsDirectory.path, _databaseName);
  49. return await openDatabase(path,
  50. version: _databaseVersion, onCreate: _onCreate);
  51. }
  52. // SQL code to create the database table
  53. Future _onCreate(Database db, int version) async {
  54. await db.execute('''
  55. CREATE TABLE $table (
  56. $columnGeneratedID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  57. $columnLocalID TEXT,
  58. $columnUploadedFileID INTEGER,
  59. $columnOwnerID INTEGER,
  60. $columnCollectionID INTEGER,
  61. $columnTitle TEXT NOT NULL,
  62. $columnDeviceFolder TEXT NOT NULL,
  63. $columnLatitude REAL,
  64. $columnLongitude REAL,
  65. $columnFileType INTEGER,
  66. $columnIsEncrypted INTEGER DEFAULT 1,
  67. $columnModificationTime TEXT NOT NULL,
  68. $columnEncryptedKey TEXT,
  69. $columnKeyDecryptionNonce TEXT,
  70. $columnFileDecryptionHeader TEXT,
  71. $columnThumbnailDecryptionHeader TEXT,
  72. $columnMetadataDecryptionHeader TEXT,
  73. $columnIsDeleted INTEGER DEFAULT 0,
  74. $columnCreationTime TEXT NOT NULL,
  75. $columnUpdationTime TEXT,
  76. UNIQUE($columnUploadedFileID, $columnCollectionID)
  77. );
  78. CREATE INDEX collection_id_index ON $table($columnCollectionID);
  79. CREATE INDEX device_folder_index ON $table($columnDeviceFolder);
  80. CREATE INDEX creation_time_index ON $table($columnCreationTime);
  81. CREATE INDEX updation_time_index ON $table($columnUpdationTime);
  82. ''');
  83. }
  84. Future<int> insert(File file) async {
  85. final db = await instance.database;
  86. return await db.insert(table, _getRowForFile(file));
  87. }
  88. Future<void> insertMultiple(List<File> files) async {
  89. final db = await instance.database;
  90. var batch = db.batch();
  91. int batchCounter = 0;
  92. for (File file in files) {
  93. if (batchCounter == 400) {
  94. await batch.commit();
  95. batch = db.batch();
  96. }
  97. batch.insert(
  98. table,
  99. _getRowForFile(file),
  100. conflictAlgorithm: ConflictAlgorithm.replace,
  101. );
  102. batchCounter++;
  103. }
  104. await batch.commit(noResult: true);
  105. }
  106. Future<File> getFile(int generatedID) async {
  107. final db = await instance.database;
  108. final results = await db.query(table,
  109. where: '$columnGeneratedID = ?', whereArgs: [generatedID]);
  110. if (results.isEmpty) {
  111. return null;
  112. }
  113. return _convertToFiles(results)[0];
  114. }
  115. Future<File> getUploadedFile(int uploadedID, int collectionID) async {
  116. final db = await instance.database;
  117. final results = await db.query(
  118. table,
  119. where: '$columnUploadedFileID = ? AND $columnCollectionID = ?',
  120. whereArgs: [
  121. uploadedID,
  122. collectionID,
  123. ],
  124. );
  125. if (results.isEmpty) {
  126. return null;
  127. }
  128. return _convertToFiles(results)[0];
  129. }
  130. Future<List<File>> getDeduplicatedFiles() async {
  131. _logger.info("Getting files for collection");
  132. final db = await instance.database;
  133. final results = await db.query(table,
  134. where: '$columnIsDeleted = 0',
  135. orderBy: '$columnCreationTime DESC',
  136. groupBy:
  137. 'IFNULL($columnUploadedFileID, $columnGeneratedID), IFNULL($columnLocalID, $columnGeneratedID)');
  138. return _convertToFiles(results);
  139. }
  140. Future<List<File>> getFiles() async {
  141. final db = await instance.database;
  142. final results = await db.query(
  143. table,
  144. where: '$columnIsDeleted = 0',
  145. orderBy: '$columnCreationTime DESC',
  146. );
  147. return _convertToFiles(results);
  148. }
  149. Future<List<File>> getAllVideos() async {
  150. final db = await instance.database;
  151. final results = await db.query(
  152. table,
  153. where:
  154. '$columnLocalID IS NOT NULL AND $columnFileType = 1 AND $columnIsDeleted = 0',
  155. orderBy: '$columnCreationTime DESC',
  156. );
  157. return _convertToFiles(results);
  158. }
  159. Future<List<File>> getAllInCollectionBeforeCreationTime(
  160. int collectionID, int beforeCreationTime, int limit) async {
  161. final db = await instance.database;
  162. final results = await db.query(
  163. table,
  164. where:
  165. '$columnCollectionID = ? AND $columnIsDeleted = 0 AND $columnCreationTime < ?',
  166. whereArgs: [collectionID, beforeCreationTime],
  167. orderBy: '$columnCreationTime DESC',
  168. limit: limit,
  169. );
  170. return _convertToFiles(results);
  171. }
  172. Future<List<File>> getAllInPath(String path) async {
  173. final db = await instance.database;
  174. final results = await db.query(
  175. table,
  176. where:
  177. '$columnLocalID IS NOT NULL AND $columnDeviceFolder = ? AND $columnIsDeleted = 0',
  178. whereArgs: [path],
  179. orderBy: '$columnCreationTime DESC',
  180. groupBy: '$columnLocalID',
  181. );
  182. return _convertToFiles(results);
  183. }
  184. Future<List<File>> getAllInPathBeforeCreationTime(
  185. String path, int beforeCreationTime, int limit) async {
  186. final db = await instance.database;
  187. final results = await db.query(
  188. table,
  189. where:
  190. '$columnLocalID IS NOT NULL AND $columnDeviceFolder = ? AND $columnIsDeleted = 0 AND $columnCreationTime < ?',
  191. whereArgs: [path, beforeCreationTime],
  192. orderBy: '$columnCreationTime DESC',
  193. groupBy: '$columnLocalID',
  194. limit: limit,
  195. );
  196. return _convertToFiles(results);
  197. }
  198. Future<List<File>> getAllInCollection(int collectionID) async {
  199. final db = await instance.database;
  200. final results = await db.query(
  201. table,
  202. where: '$columnCollectionID = ?',
  203. whereArgs: [collectionID],
  204. orderBy: '$columnCreationTime DESC',
  205. );
  206. return _convertToFiles(results);
  207. }
  208. Future<List<File>> getFilesCreatedWithinDuration(
  209. int startCreationTime, int endCreationTime) async {
  210. final db = await instance.database;
  211. final results = await db.query(
  212. table,
  213. where:
  214. '$columnCreationTime > ? AND $columnCreationTime < ? AND $columnIsDeleted = 0',
  215. whereArgs: [startCreationTime, endCreationTime],
  216. orderBy: '$columnCreationTime ASC',
  217. );
  218. return _convertToFiles(results);
  219. }
  220. Future<List<int>> getDeletedFileIDs() async {
  221. final db = await instance.database;
  222. final rows = await db.query(
  223. table,
  224. columns: [columnUploadedFileID],
  225. distinct: true,
  226. where: '$columnIsDeleted = 1',
  227. orderBy: '$columnCreationTime DESC',
  228. );
  229. final result = List<int>();
  230. for (final row in rows) {
  231. result.add(row[columnUploadedFileID]);
  232. }
  233. return result;
  234. }
  235. Future<List<File>> getFilesToBeUploadedWithinFolders(
  236. Set<String> folders) async {
  237. final db = await instance.database;
  238. String inParam = "";
  239. for (final folder in folders) {
  240. inParam += "'" + folder + "',";
  241. }
  242. inParam = inParam.substring(0, inParam.length - 1);
  243. final results = await db.query(
  244. table,
  245. where:
  246. '$columnUploadedFileID IS NULL AND $columnDeviceFolder IN ($inParam)',
  247. orderBy: '$columnCreationTime DESC',
  248. );
  249. return _convertToFiles(results);
  250. }
  251. Future<List<int>> getUploadedFileIDsToBeUpdated() async {
  252. final db = await instance.database;
  253. final rows = await db.query(
  254. table,
  255. columns: [columnUploadedFileID],
  256. where:
  257. '($columnUploadedFileID IS NOT NULL AND $columnUpdationTime IS NULL AND $columnIsDeleted = 0)',
  258. orderBy: '$columnCreationTime DESC',
  259. distinct: true,
  260. );
  261. final uploadedFileIDs = List<int>();
  262. for (final row in rows) {
  263. uploadedFileIDs.add(row[columnUploadedFileID]);
  264. }
  265. return uploadedFileIDs;
  266. }
  267. Future<File> getUploadedFileInAnyCollection(int uploadedFileID) async {
  268. final db = await instance.database;
  269. final results = await db.query(
  270. table,
  271. where: '$columnUploadedFileID = ?',
  272. whereArgs: [
  273. uploadedFileID,
  274. ],
  275. limit: 1,
  276. );
  277. if (results.isEmpty) {
  278. return null;
  279. }
  280. return _convertToFiles(results)[0];
  281. }
  282. Future<Set<String>> getUploadedLocalFileIDs() async {
  283. final db = await instance.database;
  284. final rows = await db.query(
  285. table,
  286. columns: [columnLocalID],
  287. distinct: true,
  288. where:
  289. '$columnLocalID IS NOT NULL AND $columnUploadedFileID IS NOT NULL AND $columnIsDeleted = 0',
  290. );
  291. final result = Set<String>();
  292. for (final row in rows) {
  293. result.add(row[columnLocalID]);
  294. }
  295. return result;
  296. }
  297. Future<int> updateUploadedFile(
  298. String localID,
  299. String title,
  300. Location location,
  301. int creationTime,
  302. int modificationTime,
  303. int updationTime,
  304. ) async {
  305. final db = await instance.database;
  306. return await db.update(
  307. table,
  308. {
  309. columnTitle: title,
  310. columnLatitude: location.latitude,
  311. columnLongitude: location.longitude,
  312. columnCreationTime: creationTime,
  313. columnModificationTime: modificationTime,
  314. columnUpdationTime: updationTime,
  315. },
  316. where: '$columnLocalID = ?',
  317. whereArgs: [localID],
  318. );
  319. }
  320. Future<Map<int, File>> getLastCreatedFilesInCollections(
  321. List<int> collectionIDs) async {
  322. final db = await instance.database;
  323. final rows = await db.rawQuery('''
  324. SELECT
  325. $columnGeneratedID,
  326. $columnLocalID,
  327. $columnUploadedFileID,
  328. $columnOwnerID,
  329. $columnCollectionID,
  330. $columnTitle,
  331. $columnDeviceFolder,
  332. $columnLatitude,
  333. $columnLongitude,
  334. $columnFileType,
  335. $columnIsEncrypted,
  336. $columnModificationTime,
  337. $columnEncryptedKey,
  338. $columnKeyDecryptionNonce,
  339. $columnFileDecryptionHeader,
  340. $columnThumbnailDecryptionHeader,
  341. $columnMetadataDecryptionHeader,
  342. $columnIsDeleted,
  343. $columnUpdationTime,
  344. MAX($columnCreationTime) as $columnCreationTime
  345. FROM $table
  346. WHERE $columnCollectionID IN (${collectionIDs.join(', ')}) AND $columnIsDeleted = 0
  347. GROUP BY $columnCollectionID
  348. ORDER BY $columnCreationTime DESC;
  349. ''');
  350. final result = Map<int, File>();
  351. final files = _convertToFiles(rows);
  352. for (final file in files) {
  353. result[file.collectionID] = file;
  354. }
  355. return result;
  356. }
  357. Future<Map<int, File>> getLastUpdatedFilesInCollections(
  358. List<int> collectionIDs) async {
  359. final db = await instance.database;
  360. final rows = await db.rawQuery('''
  361. SELECT
  362. $columnGeneratedID,
  363. $columnLocalID,
  364. $columnUploadedFileID,
  365. $columnOwnerID,
  366. $columnCollectionID,
  367. $columnTitle,
  368. $columnDeviceFolder,
  369. $columnLatitude,
  370. $columnLongitude,
  371. $columnFileType,
  372. $columnIsEncrypted,
  373. $columnModificationTime,
  374. $columnEncryptedKey,
  375. $columnKeyDecryptionNonce,
  376. $columnFileDecryptionHeader,
  377. $columnThumbnailDecryptionHeader,
  378. $columnMetadataDecryptionHeader,
  379. $columnIsDeleted,
  380. $columnCreationTime,
  381. MAX($columnUpdationTime) AS $columnUpdationTime
  382. FROM $table
  383. WHERE $columnCollectionID IN (${collectionIDs.join(', ')}) AND $columnIsDeleted = 0
  384. GROUP BY $columnCollectionID
  385. ORDER BY $columnUpdationTime DESC;
  386. ''');
  387. final result = Map<int, File>();
  388. final files = _convertToFiles(rows);
  389. for (final file in files) {
  390. result[file.collectionID] = file;
  391. }
  392. return result;
  393. }
  394. Future<List<File>> getMatchingFiles(
  395. String title,
  396. String deviceFolder,
  397. int creationTime,
  398. int modificationTime,
  399. ) async {
  400. final db = await instance.database;
  401. final rows = await db.query(
  402. table,
  403. where: '''$columnTitle=? AND $columnDeviceFolder=? AND
  404. $columnCreationTime=? AND $columnModificationTime=?''',
  405. whereArgs: [
  406. title,
  407. deviceFolder,
  408. creationTime,
  409. modificationTime,
  410. ],
  411. );
  412. if (rows.isNotEmpty) {
  413. return _convertToFiles(rows);
  414. } else {
  415. return null;
  416. }
  417. }
  418. Future<File> getMatchingRemoteFile(int uploadedFileID) async {
  419. final db = await instance.database;
  420. final rows = await db.query(
  421. table,
  422. where: '$columnUploadedFileID=?',
  423. whereArgs: [uploadedFileID],
  424. );
  425. if (rows.isNotEmpty) {
  426. return _getFileFromRow(rows[0]);
  427. } else {
  428. throw ("No matching file found");
  429. }
  430. }
  431. Future<int> update(File file) async {
  432. final db = await instance.database;
  433. return await db.update(
  434. table,
  435. _getRowForFile(file),
  436. where: '$columnGeneratedID = ?',
  437. whereArgs: [file.generatedID],
  438. );
  439. }
  440. Future<int> updateUploadedFileAcrossCollections(File file) async {
  441. final db = await instance.database;
  442. return await db.update(
  443. table,
  444. _getRowForFileWithoutCollection(file),
  445. where: '$columnUploadedFileID = ?',
  446. whereArgs: [file.uploadedFileID],
  447. );
  448. }
  449. Future<int> markForDeletion(int uploadedFileID) async {
  450. final db = await instance.database;
  451. final values = new Map<String, dynamic>();
  452. values[columnIsDeleted] = 1;
  453. return db.update(
  454. table,
  455. values,
  456. where: '$columnUploadedFileID =?',
  457. whereArgs: [uploadedFileID],
  458. );
  459. }
  460. Future<int> delete(int uploadedFileID) async {
  461. final db = await instance.database;
  462. return db.delete(
  463. table,
  464. where: '$columnUploadedFileID =?',
  465. whereArgs: [uploadedFileID],
  466. );
  467. }
  468. Future<int> deleteLocalFile(String localID) async {
  469. final db = await instance.database;
  470. return db.delete(
  471. table,
  472. where: '$columnLocalID =?',
  473. whereArgs: [localID],
  474. );
  475. }
  476. Future<int> deleteFromCollection(int uploadedFileID, int collectionID) async {
  477. final db = await instance.database;
  478. return db.delete(
  479. table,
  480. where: '$columnUploadedFileID = ? AND $columnCollectionID = ?',
  481. whereArgs: [uploadedFileID, collectionID],
  482. );
  483. }
  484. Future<int> deleteCollection(int collectionID) async {
  485. final db = await instance.database;
  486. return db.delete(
  487. table,
  488. where: '$columnCollectionID = ?',
  489. whereArgs: [collectionID],
  490. );
  491. }
  492. Future<int> removeFromCollection(int collectionID, List<int> fileIDs) async {
  493. final db = await instance.database;
  494. return db.delete(
  495. table,
  496. where:
  497. '$columnCollectionID =? AND $columnUploadedFileID IN (${fileIDs.join(', ')})',
  498. whereArgs: [collectionID],
  499. );
  500. }
  501. Future<List<String>> getLocalPaths() async {
  502. final db = await instance.database;
  503. final rows = await db.query(
  504. table,
  505. columns: [columnDeviceFolder],
  506. where: '$columnLocalID IS NOT NULL',
  507. distinct: true,
  508. );
  509. List<String> result = List<String>();
  510. for (final row in rows) {
  511. result.add(row[columnDeviceFolder]);
  512. }
  513. return result;
  514. }
  515. Future<File> getLatestFileInCollection(int collectionID) async {
  516. final db = await instance.database;
  517. final rows = await db.query(
  518. table,
  519. where: '$columnCollectionID = ? AND $columnIsDeleted = 0',
  520. whereArgs: [collectionID],
  521. orderBy: '$columnCreationTime DESC',
  522. limit: 1,
  523. );
  524. if (rows.isNotEmpty) {
  525. return _getFileFromRow(rows[0]);
  526. } else {
  527. return null;
  528. }
  529. }
  530. Future<File> getLastCreatedFileInPath(String path) async {
  531. final db = await instance.database;
  532. final rows = await db.query(
  533. table,
  534. where: '$columnDeviceFolder = ? AND $columnIsDeleted = 0',
  535. whereArgs: [path],
  536. orderBy: '$columnCreationTime DESC',
  537. limit: 1,
  538. );
  539. if (rows.isNotEmpty) {
  540. return _getFileFromRow(rows[0]);
  541. } else {
  542. return null;
  543. }
  544. }
  545. Future<File> getLastModifiedFileInCollection(int collectionID) async {
  546. final db = await instance.database;
  547. final rows = await db.query(
  548. table,
  549. where: '$columnCollectionID = ? AND $columnIsDeleted = 0',
  550. whereArgs: [collectionID],
  551. orderBy: '$columnUpdationTime DESC',
  552. limit: 1,
  553. );
  554. if (rows.isNotEmpty) {
  555. return _getFileFromRow(rows[0]);
  556. } else {
  557. return null;
  558. }
  559. }
  560. Future<bool> doesFileExistInCollection(
  561. int uploadedFileID, int collectionID) async {
  562. final db = await instance.database;
  563. final rows = await db.query(
  564. table,
  565. where: '$columnUploadedFileID = ? AND $columnCollectionID = ?',
  566. whereArgs: [uploadedFileID, collectionID],
  567. limit: 1,
  568. );
  569. return rows.isNotEmpty;
  570. }
  571. List<File> _convertToFiles(List<Map<String, dynamic>> results) {
  572. final files = List<File>();
  573. for (final result in results) {
  574. files.add(_getFileFromRow(result));
  575. }
  576. return files;
  577. }
  578. Map<String, dynamic> _getRowForFile(File file) {
  579. final row = new Map<String, dynamic>();
  580. row[columnLocalID] = file.localID;
  581. row[columnUploadedFileID] = file.uploadedFileID;
  582. row[columnOwnerID] = file.ownerID;
  583. row[columnCollectionID] = file.collectionID;
  584. row[columnTitle] = file.title;
  585. row[columnDeviceFolder] = file.deviceFolder;
  586. if (file.location != null) {
  587. row[columnLatitude] = file.location.latitude;
  588. row[columnLongitude] = file.location.longitude;
  589. }
  590. switch (file.fileType) {
  591. case FileType.image:
  592. row[columnFileType] = 0;
  593. break;
  594. case FileType.video:
  595. row[columnFileType] = 1;
  596. break;
  597. default:
  598. row[columnFileType] = -1;
  599. }
  600. row[columnIsEncrypted] = file.isEncrypted ? 1 : 0;
  601. row[columnCreationTime] = file.creationTime;
  602. row[columnModificationTime] = file.modificationTime;
  603. row[columnUpdationTime] = file.updationTime;
  604. row[columnEncryptedKey] = file.encryptedKey;
  605. row[columnKeyDecryptionNonce] = file.keyDecryptionNonce;
  606. row[columnFileDecryptionHeader] = file.fileDecryptionHeader;
  607. row[columnThumbnailDecryptionHeader] = file.thumbnailDecryptionHeader;
  608. row[columnMetadataDecryptionHeader] = file.metadataDecryptionHeader;
  609. return row;
  610. }
  611. Map<String, dynamic> _getRowForFileWithoutCollection(File file) {
  612. final row = new Map<String, dynamic>();
  613. row[columnLocalID] = file.localID;
  614. row[columnUploadedFileID] = file.uploadedFileID;
  615. row[columnOwnerID] = file.ownerID;
  616. row[columnTitle] = file.title;
  617. row[columnDeviceFolder] = file.deviceFolder;
  618. if (file.location != null) {
  619. row[columnLatitude] = file.location.latitude;
  620. row[columnLongitude] = file.location.longitude;
  621. }
  622. switch (file.fileType) {
  623. case FileType.image:
  624. row[columnFileType] = 0;
  625. break;
  626. case FileType.video:
  627. row[columnFileType] = 1;
  628. break;
  629. default:
  630. row[columnFileType] = -1;
  631. }
  632. row[columnIsEncrypted] = file.isEncrypted ? 1 : 0;
  633. row[columnCreationTime] = file.creationTime;
  634. row[columnModificationTime] = file.modificationTime;
  635. row[columnUpdationTime] = file.updationTime;
  636. row[columnFileDecryptionHeader] = file.fileDecryptionHeader;
  637. row[columnThumbnailDecryptionHeader] = file.thumbnailDecryptionHeader;
  638. row[columnMetadataDecryptionHeader] = file.metadataDecryptionHeader;
  639. return row;
  640. }
  641. File _getFileFromRow(Map<String, dynamic> row) {
  642. final file = File();
  643. file.generatedID = row[columnGeneratedID];
  644. file.localID = row[columnLocalID];
  645. file.uploadedFileID = row[columnUploadedFileID];
  646. file.ownerID = row[columnOwnerID];
  647. file.collectionID = row[columnCollectionID];
  648. file.title = row[columnTitle];
  649. file.deviceFolder = row[columnDeviceFolder];
  650. if (row[columnLatitude] != null && row[columnLongitude] != null) {
  651. file.location = Location(row[columnLatitude], row[columnLongitude]);
  652. }
  653. file.fileType = getFileType(row[columnFileType]);
  654. file.isEncrypted = row[columnIsEncrypted] == 1;
  655. file.creationTime = int.parse(row[columnCreationTime]);
  656. file.modificationTime = int.parse(row[columnModificationTime]);
  657. file.updationTime = row[columnUpdationTime] == null
  658. ? -1
  659. : int.parse(row[columnUpdationTime]);
  660. file.encryptedKey = row[columnEncryptedKey];
  661. file.keyDecryptionNonce = row[columnKeyDecryptionNonce];
  662. file.fileDecryptionHeader = row[columnFileDecryptionHeader];
  663. file.thumbnailDecryptionHeader = row[columnThumbnailDecryptionHeader];
  664. file.metadataDecryptionHeader = row[columnMetadataDecryptionHeader];
  665. return file;
  666. }
  667. }