2022-04-02 19:31:53 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2023-02-04 22:42:42 +02:00
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2022-08-03 07:04:34 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/api.provider.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:immich_mobile/shared/services/api.service.dart';
|
2023-10-19 22:19:00 +02:00
|
|
|
import 'package:logging/logging.dart';
|
2022-04-02 19:31:53 +02:00
|
|
|
|
|
|
|
import 'package:photo_manager/photo_manager.dart';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
final imageViewerServiceProvider =
|
|
|
|
Provider((ref) => ImageViewerService(ref.watch(apiServiceProvider)));
|
|
|
|
|
2022-04-02 19:31:53 +02:00
|
|
|
class ImageViewerService {
|
2022-07-13 14:23:48 +02:00
|
|
|
final ApiService _apiService;
|
2023-10-19 22:19:00 +02:00
|
|
|
final Logger _log = Logger("ImageViewerService");
|
2022-08-03 07:04:34 +02:00
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
ImageViewerService(this._apiService);
|
|
|
|
|
2023-02-04 22:42:42 +02:00
|
|
|
Future<bool> downloadAssetToDevice(Asset asset) async {
|
2022-04-02 19:31:53 +02:00
|
|
|
try {
|
2022-11-19 07:12:54 +02:00
|
|
|
// Download LivePhotos image and motion part
|
2023-02-04 22:42:42 +02:00
|
|
|
if (asset.isImage && asset.livePhotoVideoId != null) {
|
2022-11-19 07:12:54 +02:00
|
|
|
var imageResponse = await _apiService.assetApi.downloadFileWithHttpInfo(
|
2023-02-04 22:42:42 +02:00
|
|
|
asset.remoteId!,
|
2022-11-19 07:12:54 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
var motionReponse = await _apiService.assetApi.downloadFileWithHttpInfo(
|
|
|
|
asset.livePhotoVideoId!,
|
|
|
|
);
|
2022-04-02 19:31:53 +02:00
|
|
|
|
2023-10-19 22:19:00 +02:00
|
|
|
if (imageResponse.statusCode != 200 ||
|
|
|
|
motionReponse.statusCode != 200) {
|
|
|
|
final failedResponse =
|
|
|
|
imageResponse.statusCode != 200 ? imageResponse : motionReponse;
|
|
|
|
_log.severe(
|
|
|
|
"Motion asset download failed with status - ${failedResponse.statusCode} and response - ${failedResponse.body}",
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-19 07:12:54 +02:00
|
|
|
final AssetEntity? entity;
|
2022-04-02 19:31:53 +02:00
|
|
|
|
2022-11-19 07:12:54 +02:00
|
|
|
final tempDir = await getTemporaryDirectory();
|
|
|
|
File videoFile = await File('${tempDir.path}/livephoto.mov').create();
|
|
|
|
File imageFile = await File('${tempDir.path}/livephoto.heic').create();
|
|
|
|
videoFile.writeAsBytesSync(motionReponse.bodyBytes);
|
|
|
|
imageFile.writeAsBytesSync(imageResponse.bodyBytes);
|
|
|
|
|
|
|
|
entity = await PhotoManager.editor.darwin.saveLivePhoto(
|
|
|
|
imageFile: imageFile,
|
|
|
|
videoFile: videoFile,
|
2023-02-04 22:42:42 +02:00
|
|
|
title: asset.fileName,
|
2022-04-02 19:31:53 +02:00
|
|
|
);
|
2022-11-19 07:12:54 +02:00
|
|
|
|
|
|
|
return entity != null;
|
2022-04-02 19:31:53 +02:00
|
|
|
} else {
|
2023-02-04 22:42:42 +02:00
|
|
|
var res = await _apiService.assetApi
|
|
|
|
.downloadFileWithHttpInfo(asset.remoteId!);
|
2022-04-02 19:31:53 +02:00
|
|
|
|
2023-10-19 22:19:00 +02:00
|
|
|
if (res.statusCode != 200) {
|
|
|
|
_log.severe(
|
|
|
|
"Asset download failed with status - ${res.statusCode} and response - ${res.body}",
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-19 07:12:54 +02:00
|
|
|
final AssetEntity? entity;
|
|
|
|
|
2023-02-04 22:42:42 +02:00
|
|
|
if (asset.isImage) {
|
2022-11-19 07:12:54 +02:00
|
|
|
entity = await PhotoManager.editor.saveImage(
|
|
|
|
res.bodyBytes,
|
2023-02-04 22:42:42 +02:00
|
|
|
title: asset.fileName,
|
2022-11-19 07:12:54 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
final tempDir = await getTemporaryDirectory();
|
2023-02-04 22:42:42 +02:00
|
|
|
File tempFile =
|
|
|
|
await File('${tempDir.path}/${asset.fileName}').create();
|
2022-11-19 07:12:54 +02:00
|
|
|
tempFile.writeAsBytesSync(res.bodyBytes);
|
2023-02-04 22:42:42 +02:00
|
|
|
entity = await PhotoManager.editor
|
|
|
|
.saveVideo(tempFile, title: asset.fileName);
|
2022-11-19 07:12:54 +02:00
|
|
|
}
|
|
|
|
return entity != null;
|
|
|
|
}
|
2022-04-02 19:31:53 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error saving file $e");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|