2021-02-09 12:04:40 +00:00
|
|
|
import 'dart:async';
|
2020-08-13 01:18:41 +00:00
|
|
|
import 'dart:io' as io;
|
2021-09-28 09:24:41 +00:00
|
|
|
import 'dart:io';
|
2020-08-13 01:18:41 +00:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
2021-08-03 13:38:54 +00:00
|
|
|
import 'package:archive/archive.dart';
|
2020-08-13 01:18:41 +00:00
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
|
|
|
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
2021-07-21 20:47:43 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2021-08-02 19:39:50 +00:00
|
|
|
import 'package:motionphoto/motionphoto.dart';
|
2021-08-03 11:58:17 +00:00
|
|
|
import 'package:path/path.dart';
|
2021-08-09 06:21:09 +00:00
|
|
|
import 'package:photos/core/cache/image_cache.dart';
|
2020-08-13 20:03:29 +00:00
|
|
|
import 'package:photos/core/cache/video_cache_manager.dart';
|
2020-08-13 01:18:41 +00:00
|
|
|
import 'package:photos/core/configuration.dart';
|
2020-07-29 19:07:23 +00:00
|
|
|
import 'package:photos/core/constants.dart';
|
2021-05-29 21:58:42 +00:00
|
|
|
import 'package:photos/models/file.dart' as ente;
|
2020-08-13 01:24:48 +00:00
|
|
|
import 'package:photos/models/file_type.dart';
|
2021-08-02 19:39:50 +00:00
|
|
|
import 'package:photos/utils/file_download_util.dart';
|
2021-08-09 06:21:09 +00:00
|
|
|
import 'package:photos/utils/thumbnail_util.dart';
|
2020-08-13 01:18:41 +00:00
|
|
|
|
2020-11-22 09:16:11 +00:00
|
|
|
final _logger = Logger("FileUtil");
|
2020-09-28 16:16:04 +00:00
|
|
|
|
2021-05-29 21:58:42 +00:00
|
|
|
void preloadFile(ente.File file) {
|
2020-08-13 01:24:48 +00:00
|
|
|
if (file.fileType == FileType.video) {
|
|
|
|
return;
|
|
|
|
}
|
2021-06-11 07:19:19 +00:00
|
|
|
getFile(file);
|
2021-06-02 13:54:31 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 07:14:33 +00:00
|
|
|
// IMPORTANT: Delete the returned file if `isOrigin` is set to true
|
|
|
|
// https://github.com/CaiJingLong/flutter_photo_manager#cache-problem-of-ios
|
2021-08-23 20:29:37 +00:00
|
|
|
Future<io.File> getFile(
|
|
|
|
ente.File file, {
|
|
|
|
bool liveVideo = false,
|
|
|
|
bool isOrigin = false,
|
|
|
|
} // only relevant for live photos
|
2021-08-04 10:11:38 +00:00
|
|
|
) async {
|
2021-07-19 05:37:34 +00:00
|
|
|
if (file.isRemoteFile()) {
|
2021-08-04 10:11:38 +00:00
|
|
|
return getFileFromServer(file, liveVideo: liveVideo);
|
2020-08-13 01:18:41 +00:00
|
|
|
} else {
|
2021-08-23 20:29:37 +00:00
|
|
|
String key = file.tag() + liveVideo.toString() + isOrigin.toString();
|
2021-08-04 10:11:38 +00:00
|
|
|
final cachedFile = FileLruCache.get(key);
|
2021-06-02 13:54:31 +00:00
|
|
|
if (cachedFile == null) {
|
2021-08-23 20:29:37 +00:00
|
|
|
final diskFile = await _getLocalDiskFile(
|
|
|
|
file,
|
|
|
|
liveVideo: liveVideo,
|
|
|
|
isOrigin: isOrigin,
|
|
|
|
);
|
2021-09-28 09:24:41 +00:00
|
|
|
// do not cache origin file for IOS as they are immediately deleted
|
|
|
|
// after usage
|
|
|
|
if (!(isOrigin && Platform.isIOS)) {
|
|
|
|
FileLruCache.put(key, diskFile);
|
|
|
|
}
|
2021-06-02 13:54:31 +00:00
|
|
|
return diskFile;
|
2020-08-13 01:18:41 +00:00
|
|
|
}
|
2021-06-02 13:54:31 +00:00
|
|
|
return cachedFile;
|
2020-08-13 01:18:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 23:28:26 +00:00
|
|
|
Future<bool> doesLocalFileExist(ente.File file) async {
|
|
|
|
return await _getLocalDiskFile(file) != null;
|
|
|
|
}
|
|
|
|
|
2021-08-23 20:29:37 +00:00
|
|
|
Future<io.File> _getLocalDiskFile(
|
|
|
|
ente.File file, {
|
|
|
|
bool liveVideo = false,
|
|
|
|
bool isOrigin = false,
|
|
|
|
}) async {
|
2021-07-24 17:33:59 +00:00
|
|
|
if (file.isSharedMediaToAppSandbox()) {
|
2021-07-24 13:38:47 +00:00
|
|
|
var localFile = io.File(getSharedMediaFilePath(file));
|
|
|
|
return localFile.exists().then((exist) {
|
|
|
|
return exist ? localFile : null;
|
|
|
|
});
|
2021-08-04 10:11:38 +00:00
|
|
|
} else if (file.fileType == FileType.livePhoto && liveVideo) {
|
|
|
|
return Motionphoto.getLivePhotoFile(file.localID);
|
2021-08-09 06:21:09 +00:00
|
|
|
} else {
|
2021-07-22 14:21:24 +00:00
|
|
|
return file.getAsset().then((asset) async {
|
|
|
|
if (asset == null || !(await asset.exists)) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-08-23 20:29:37 +00:00
|
|
|
return isOrigin ? asset.originFile : asset.file;
|
2021-07-22 14:21:24 +00:00
|
|
|
});
|
2021-07-22 06:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-24 13:30:33 +00:00
|
|
|
String getSharedMediaFilePath(ente.File file) {
|
2021-08-02 19:39:50 +00:00
|
|
|
return Configuration.instance.getSharedMediaCacheDirectory() +
|
|
|
|
"/" +
|
|
|
|
file.localID.replaceAll(kSharedMediaIdentifier, '');
|
2021-07-24 13:30:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-29 21:58:42 +00:00
|
|
|
void preloadThumbnail(ente.File file) {
|
2021-07-19 05:37:34 +00:00
|
|
|
if (file.isRemoteFile()) {
|
2021-04-27 16:29:58 +00:00
|
|
|
getThumbnailFromServer(file);
|
|
|
|
} else {
|
2021-07-22 06:11:32 +00:00
|
|
|
getThumbnailFromLocal(file);
|
2021-04-27 16:29:58 +00:00
|
|
|
}
|
2020-07-29 19:07:23 +00:00
|
|
|
}
|
2020-08-13 01:24:48 +00:00
|
|
|
|
2021-10-04 06:29:22 +00:00
|
|
|
final Map<String, Future<io.File>> fileDownloadsInProgress =
|
|
|
|
<String, Future<io.File>>{};
|
2020-10-26 10:54:26 +00:00
|
|
|
|
2021-08-03 13:38:54 +00:00
|
|
|
Future<io.File> getFileFromServer(
|
|
|
|
ente.File file, {
|
|
|
|
ProgressCallback progressCallback,
|
|
|
|
bool liveVideo = false, // only needed in case of live photos
|
|
|
|
}) async {
|
|
|
|
final cacheManager = (file.fileType == FileType.video || liveVideo)
|
2021-05-02 16:05:36 +00:00
|
|
|
? VideoCacheManager.instance
|
2020-08-13 20:03:29 +00:00
|
|
|
: DefaultCacheManager();
|
2021-10-04 06:29:22 +00:00
|
|
|
final fileFromCache =
|
|
|
|
await cacheManager.getFileFromCache(file.getDownloadUrl());
|
|
|
|
if (fileFromCache != null) {
|
|
|
|
return fileFromCache.file;
|
|
|
|
}
|
|
|
|
final downloadID = file.uploadedFileID.toString() + liveVideo.toString();
|
|
|
|
if (!fileDownloadsInProgress.containsKey(downloadID)) {
|
|
|
|
if (file.fileType == FileType.livePhoto) {
|
|
|
|
fileDownloadsInProgress[downloadID] = _getLivePhotoFromServer(file,
|
|
|
|
progressCallback: progressCallback, liveVideo: liveVideo)
|
|
|
|
.whenComplete(() => fileDownloadsInProgress.remove(downloadID));
|
2021-02-08 09:05:41 +00:00
|
|
|
} else {
|
2021-10-04 06:29:22 +00:00
|
|
|
fileDownloadsInProgress[downloadID] = _downloadAndCache(
|
|
|
|
file,
|
|
|
|
cacheManager,
|
|
|
|
progressCallback: progressCallback,
|
|
|
|
).whenComplete(() => fileDownloadsInProgress.remove(downloadID));
|
2021-02-08 09:05:41 +00:00
|
|
|
}
|
2021-10-04 06:29:22 +00:00
|
|
|
}
|
|
|
|
return fileDownloadsInProgress[downloadID];
|
|
|
|
}
|
|
|
|
|
|
|
|
final Map<int, Future<_LivePhoto>> livePhotoDownloadsTracker =
|
|
|
|
<int, Future<_LivePhoto>>{};
|
|
|
|
|
|
|
|
Future<io.File> _getLivePhotoFromServer(ente.File file,
|
|
|
|
{ProgressCallback progressCallback, bool liveVideo}) async {
|
|
|
|
final downloadID = file.uploadedFileID;
|
|
|
|
try {
|
|
|
|
if (!livePhotoDownloadsTracker.containsKey(downloadID)) {
|
|
|
|
livePhotoDownloadsTracker[downloadID] =
|
|
|
|
_downloadLivePhoto(file, progressCallback: progressCallback);
|
|
|
|
}
|
|
|
|
var _livePhoto = await livePhotoDownloadsTracker[file.uploadedFileID];
|
|
|
|
livePhotoDownloadsTracker.remove(downloadID);
|
|
|
|
if (_livePhoto == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return liveVideo ? _livePhoto.video : _livePhoto.image;
|
|
|
|
} catch (e) {
|
|
|
|
livePhotoDownloadsTracker.remove(downloadID);
|
|
|
|
return null;
|
|
|
|
}
|
2020-08-13 01:24:48 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 06:29:22 +00:00
|
|
|
Future<_LivePhoto> _downloadLivePhoto(ente.File file,
|
|
|
|
{ProgressCallback progressCallback}) async {
|
2021-08-03 13:38:54 +00:00
|
|
|
return downloadAndDecrypt(file, progressCallback: progressCallback)
|
|
|
|
.then((decryptedFile) async {
|
|
|
|
if (decryptedFile == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
_logger.fine("Decoded zipped live photo from " + decryptedFile.path);
|
|
|
|
io.File imageFileCache, videoFileCache;
|
2021-08-09 06:30:30 +00:00
|
|
|
List<int> bytes = await decryptedFile.readAsBytes();
|
2021-08-03 13:38:54 +00:00
|
|
|
Archive archive = ZipDecoder().decodeBytes(bytes);
|
|
|
|
final tempPath = Configuration.instance.getTempDirectory();
|
|
|
|
// Extract the contents of Zip compressed archive to disk
|
|
|
|
for (ArchiveFile archiveFile in archive) {
|
|
|
|
if (archiveFile.isFile) {
|
|
|
|
String filename = archiveFile.name;
|
|
|
|
String fileExtension = getExtension(archiveFile.name);
|
|
|
|
String decodePath =
|
|
|
|
tempPath + file.uploadedFileID.toString() + filename;
|
|
|
|
List<int> data = archiveFile.content;
|
|
|
|
if (filename.startsWith("image")) {
|
2021-08-09 06:26:11 +00:00
|
|
|
final imageFile = io.File(decodePath);
|
|
|
|
await imageFile.create(recursive: true);
|
|
|
|
await imageFile.writeAsBytes(data);
|
2021-08-03 13:38:54 +00:00
|
|
|
io.File imageConvertedFile = imageFile;
|
|
|
|
if ((fileExtension == "unknown") ||
|
|
|
|
(io.Platform.isAndroid && fileExtension == "heic")) {
|
|
|
|
imageConvertedFile = await FlutterImageCompress.compressAndGetFile(
|
|
|
|
decodePath,
|
|
|
|
decodePath + ".jpg",
|
|
|
|
keepExif: true,
|
|
|
|
);
|
2021-08-09 06:21:09 +00:00
|
|
|
await imageFile.delete();
|
2021-08-03 13:38:54 +00:00
|
|
|
}
|
|
|
|
imageFileCache = await DefaultCacheManager().putFile(
|
|
|
|
file.getDownloadUrl(),
|
2021-08-09 06:26:11 +00:00
|
|
|
await imageConvertedFile.readAsBytes(),
|
2021-08-03 13:38:54 +00:00
|
|
|
eTag: file.getDownloadUrl(),
|
|
|
|
maxAge: Duration(days: 365),
|
|
|
|
fileExtension: fileExtension,
|
|
|
|
);
|
2021-08-09 06:21:09 +00:00
|
|
|
await imageConvertedFile.delete();
|
2021-08-03 13:38:54 +00:00
|
|
|
} else if (filename.startsWith("video")) {
|
2021-08-09 06:26:11 +00:00
|
|
|
final videoFile = io.File(decodePath);
|
|
|
|
await videoFile.create(recursive: true);
|
|
|
|
await videoFile.writeAsBytes(data);
|
2021-08-03 13:38:54 +00:00
|
|
|
videoFileCache = await VideoCacheManager.instance.putFile(
|
|
|
|
file.getDownloadUrl(),
|
2021-08-09 06:30:30 +00:00
|
|
|
await videoFile.readAsBytes(),
|
2021-08-03 13:38:54 +00:00
|
|
|
eTag: file.getDownloadUrl(),
|
|
|
|
maxAge: Duration(days: 365),
|
|
|
|
fileExtension: fileExtension,
|
|
|
|
);
|
2021-08-09 06:21:09 +00:00
|
|
|
await videoFile.delete();
|
2021-08-03 13:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 06:29:22 +00:00
|
|
|
return _LivePhoto(imageFileCache, videoFileCache);
|
2021-08-03 13:38:54 +00:00
|
|
|
}).catchError((e) {
|
2021-08-05 17:27:00 +00:00
|
|
|
_logger.warning("failed to download live photos" + e.toString());
|
2021-10-04 06:29:22 +00:00
|
|
|
throw e;
|
2021-08-03 13:38:54 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-03 11:58:17 +00:00
|
|
|
Future<io.File> _downloadAndCache(ente.File file, BaseCacheManager cacheManager,
|
2020-08-14 00:01:37 +00:00
|
|
|
{ProgressCallback progressCallback}) async {
|
2021-08-02 19:39:50 +00:00
|
|
|
return downloadAndDecrypt(file, progressCallback: progressCallback)
|
|
|
|
.then((decryptedFile) async {
|
|
|
|
if (decryptedFile == null) {
|
2020-09-28 17:05:53 +00:00
|
|
|
return null;
|
2020-09-28 16:15:21 +00:00
|
|
|
}
|
2021-08-02 19:39:50 +00:00
|
|
|
var decryptedFilePath = decryptedFile.path;
|
2021-08-03 13:38:54 +00:00
|
|
|
String fileExtension = getExtension(file.title);
|
2020-11-20 11:05:17 +00:00
|
|
|
var outputFile = decryptedFile;
|
2021-07-22 06:11:32 +00:00
|
|
|
if ((fileExtension == "unknown" && file.fileType == FileType.image) ||
|
2021-06-29 04:10:24 +00:00
|
|
|
(io.Platform.isAndroid && fileExtension == "heic")) {
|
2020-11-20 11:05:17 +00:00
|
|
|
outputFile = await FlutterImageCompress.compressAndGetFile(
|
|
|
|
decryptedFilePath,
|
|
|
|
decryptedFilePath + ".jpg",
|
|
|
|
keepExif: true,
|
|
|
|
);
|
2021-08-09 06:21:09 +00:00
|
|
|
await decryptedFile.delete();
|
2020-11-20 11:05:17 +00:00
|
|
|
}
|
2020-09-25 20:25:03 +00:00
|
|
|
final cachedFile = await cacheManager.putFile(
|
2020-09-17 18:41:14 +00:00
|
|
|
file.getDownloadUrl(),
|
2021-08-09 06:30:30 +00:00
|
|
|
await outputFile.readAsBytes(),
|
2020-09-17 18:41:14 +00:00
|
|
|
eTag: file.getDownloadUrl(),
|
|
|
|
maxAge: Duration(days: 365),
|
|
|
|
fileExtension: fileExtension,
|
|
|
|
);
|
2021-08-09 06:21:09 +00:00
|
|
|
await outputFile.delete();
|
2020-09-25 20:25:03 +00:00
|
|
|
return cachedFile;
|
2020-10-06 23:56:37 +00:00
|
|
|
}).catchError((e) {
|
2021-10-04 06:29:22 +00:00
|
|
|
_logger.warning(e, "failed to download file");
|
2020-08-13 01:24:48 +00:00
|
|
|
});
|
|
|
|
}
|
2020-08-13 21:33:31 +00:00
|
|
|
|
2021-08-03 13:38:54 +00:00
|
|
|
String getExtension(String nameOrPath) {
|
2021-08-09 06:21:09 +00:00
|
|
|
var fileExtension = "unknown";
|
2021-08-03 13:38:54 +00:00
|
|
|
try {
|
|
|
|
fileExtension = extension(nameOrPath).substring(1).toLowerCase();
|
|
|
|
} catch (e) {
|
|
|
|
_logger.severe("Could not capture file extension");
|
|
|
|
}
|
|
|
|
return fileExtension;
|
|
|
|
}
|
|
|
|
|
2020-11-22 17:55:34 +00:00
|
|
|
Future<Uint8List> compressThumbnail(Uint8List thumbnail) {
|
|
|
|
return FlutterImageCompress.compressWithList(
|
|
|
|
thumbnail,
|
2021-07-21 20:47:43 +00:00
|
|
|
minHeight: kCompressedThumbnailResolution,
|
|
|
|
minWidth: kCompressedThumbnailResolution,
|
2020-11-22 17:55:34 +00:00
|
|
|
quality: 25,
|
|
|
|
);
|
|
|
|
}
|
2021-04-19 12:56:12 +00:00
|
|
|
|
2021-08-09 06:21:09 +00:00
|
|
|
Future<void> clearCache(ente.File file) async {
|
2021-04-19 12:56:12 +00:00
|
|
|
if (file.fileType == FileType.video) {
|
2021-05-02 16:05:36 +00:00
|
|
|
VideoCacheManager.instance.removeFile(file.getDownloadUrl());
|
2021-04-19 12:56:12 +00:00
|
|
|
} else {
|
|
|
|
DefaultCacheManager().removeFile(file.getDownloadUrl());
|
|
|
|
}
|
2021-05-07 15:29:33 +00:00
|
|
|
final cachedThumbnail = io.File(
|
|
|
|
Configuration.instance.getThumbnailCacheDirectory() +
|
|
|
|
"/" +
|
|
|
|
file.uploadedFileID.toString());
|
|
|
|
if (cachedThumbnail.existsSync()) {
|
2021-08-09 06:21:09 +00:00
|
|
|
await cachedThumbnail.delete();
|
2021-05-07 15:29:33 +00:00
|
|
|
}
|
2021-04-19 12:56:12 +00:00
|
|
|
}
|
2021-10-04 06:29:22 +00:00
|
|
|
|
|
|
|
class _LivePhoto {
|
|
|
|
final io.File image;
|
|
|
|
final io.File video;
|
|
|
|
|
|
|
|
_LivePhoto(this.image, this.video);
|
|
|
|
}
|