|
@@ -6,11 +6,13 @@ import 'package:dio/dio.dart';
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
|
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
|
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
|
import 'package:photo_manager/photo_manager.dart';
|
|
import 'package:photo_manager/photo_manager.dart';
|
|
|
|
+import 'package:photos/core/cache/image_cache.dart';
|
|
import 'package:photos/core/cache/thumbnail_cache.dart';
|
|
import 'package:photos/core/cache/thumbnail_cache.dart';
|
|
import 'package:photos/core/configuration.dart';
|
|
import 'package:photos/core/configuration.dart';
|
|
import 'package:photos/core/constants.dart';
|
|
import 'package:photos/core/constants.dart';
|
|
import 'package:photos/db/files_db.dart';
|
|
import 'package:photos/db/files_db.dart';
|
|
import 'package:photos/models/file.dart';
|
|
import 'package:photos/models/file.dart';
|
|
|
|
+import 'package:photos/models/file_type.dart';
|
|
|
|
|
|
import 'crypto_util.dart';
|
|
import 'crypto_util.dart';
|
|
|
|
|
|
@@ -26,48 +28,18 @@ Future<void> deleteFiles(List<File> files,
|
|
}
|
|
}
|
|
|
|
|
|
void preloadFile(File file) {
|
|
void preloadFile(File file) {
|
|
- // TODO
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-Future<Uint8List> getBytes(File file, {int quality = 100}) async {
|
|
|
|
|
|
+ if (file.fileType == FileType.video) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
if (file.localID == null) {
|
|
if (file.localID == null) {
|
|
- if (!file.isEncrypted) {
|
|
|
|
- return DefaultCacheManager()
|
|
|
|
- .getSingleFile(file.getDownloadUrl())
|
|
|
|
- .then((file) => file.readAsBytesSync());
|
|
|
|
- } else {
|
|
|
|
- return DefaultCacheManager()
|
|
|
|
- .getFileFromCache(file.getDownloadUrl())
|
|
|
|
- .then((info) {
|
|
|
|
- if (info == null) {
|
|
|
|
- final temporaryPath = Configuration.instance.getTempDirectory() +
|
|
|
|
- file.generatedID.toString() +
|
|
|
|
- ".aes";
|
|
|
|
- return Dio()
|
|
|
|
- .download(file.getDownloadUrl(), temporaryPath)
|
|
|
|
- .then((_) async {
|
|
|
|
- final data = await CryptoUtil.decryptFileToData(
|
|
|
|
- temporaryPath, Configuration.instance.getKey());
|
|
|
|
- io.File(temporaryPath).deleteSync();
|
|
|
|
- DefaultCacheManager().putFile(file.getDownloadUrl(), data);
|
|
|
|
- return data;
|
|
|
|
- });
|
|
|
|
- } else {
|
|
|
|
- return info.file.readAsBytesSync();
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
|
|
+ _getBytesFromServer(file);
|
|
} else {
|
|
} else {
|
|
- final originalBytes = (await file.getAsset()).originBytes;
|
|
|
|
- if (extension(file.title) == ".HEIC" || quality != 100) {
|
|
|
|
- return originalBytes.then((bytes) {
|
|
|
|
- return FlutterImageCompress.compressWithList(bytes, quality: quality)
|
|
|
|
- .then((converted) {
|
|
|
|
- return Uint8List.fromList(converted);
|
|
|
|
|
|
+ if (FileLruCache.get(file) == null) {
|
|
|
|
+ file.getAsset().then((asset) {
|
|
|
|
+ asset.file.then((assetFile) {
|
|
|
|
+ FileLruCache.put(file, assetFile);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
- } else {
|
|
|
|
- return originalBytes;
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -85,3 +57,56 @@ void preloadLocalFileThumbnail(File file) {
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+Future<Uint8List> getBytes(File file, {int quality = 100}) async {
|
|
|
|
+ if (file.localID == null) {
|
|
|
|
+ return _getBytesFromServer(file);
|
|
|
|
+ } else {
|
|
|
|
+ return await _getBytesFromDisk(file, quality);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+Future<Uint8List> _getBytesFromDisk(File file, int quality) async {
|
|
|
|
+ final originalBytes = (await file.getAsset()).originBytes;
|
|
|
|
+ if (extension(file.title) == ".HEIC" || quality != 100) {
|
|
|
|
+ return originalBytes.then((bytes) {
|
|
|
|
+ return FlutterImageCompress.compressWithList(bytes, quality: quality)
|
|
|
|
+ .then((converted) {
|
|
|
|
+ return Uint8List.fromList(converted);
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ return originalBytes;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+Future<Uint8List> _getBytesFromServer(File file) async {
|
|
|
|
+ if (!file.isEncrypted) {
|
|
|
|
+ return DefaultCacheManager()
|
|
|
|
+ .getSingleFile(file.getDownloadUrl())
|
|
|
|
+ .then((file) => file.readAsBytesSync());
|
|
|
|
+ } else {
|
|
|
|
+ return DefaultCacheManager()
|
|
|
|
+ .getFileFromCache(file.getDownloadUrl())
|
|
|
|
+ .then((info) {
|
|
|
|
+ if (info == null) {
|
|
|
|
+ return _downloadAndDecrypt(file);
|
|
|
|
+ } else {
|
|
|
|
+ return info.file.readAsBytesSync();
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+Future<Uint8List> _downloadAndDecrypt(File file) async {
|
|
|
|
+ final temporaryPath = Configuration.instance.getTempDirectory() +
|
|
|
|
+ file.generatedID.toString() +
|
|
|
|
+ ".aes";
|
|
|
|
+ return Dio().download(file.getDownloadUrl(), temporaryPath).then((_) async {
|
|
|
|
+ final data = await CryptoUtil.decryptFileToData(
|
|
|
|
+ temporaryPath, Configuration.instance.getKey());
|
|
|
|
+ io.File(temporaryPath).deleteSync();
|
|
|
|
+ DefaultCacheManager().putFile(file.getDownloadUrl(), data);
|
|
|
|
+ return data;
|
|
|
|
+ });
|
|
|
|
+}
|