2022-04-02 10:31:53 -07:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
|
|
|
import 'package:immich_mobile/constants/hive_box.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/immich_asset.model.dart';
|
|
|
|
import 'package:path/path.dart' as p;
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
|
|
|
import 'package:photo_manager/photo_manager.dart';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
|
|
|
|
class ImageViewerService {
|
|
|
|
Future<bool> downloadAssetToDevice(ImmichAsset asset) async {
|
|
|
|
try {
|
|
|
|
String fileName = p.basename(asset.originalPath);
|
|
|
|
var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
2022-06-25 13:12:47 -07:00
|
|
|
Uri filePath = Uri.parse(
|
|
|
|
"$savedEndpoint/asset/download?aid=${asset.deviceAssetId}&did=${asset.deviceId}&isThumb=false");
|
2022-04-02 10:31:53 -07:00
|
|
|
|
|
|
|
var res = await http.get(
|
|
|
|
filePath,
|
2022-06-25 13:12:47 -07:00
|
|
|
headers: {
|
|
|
|
"Authorization": "Bearer ${Hive.box(userInfoBox).get(accessTokenKey)}"
|
|
|
|
},
|
2022-04-02 10:31:53 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
final AssetEntity? entity;
|
|
|
|
|
|
|
|
if (asset.type == 'IMAGE') {
|
|
|
|
entity = await PhotoManager.editor.saveImage(
|
|
|
|
res.bodyBytes,
|
|
|
|
title: p.basename(asset.originalPath),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
final tempDir = await getTemporaryDirectory();
|
|
|
|
File tempFile = await File('${tempDir.path}/$fileName').create();
|
|
|
|
tempFile.writeAsBytesSync(res.bodyBytes);
|
|
|
|
entity = await PhotoManager.editor.saveVideo(tempFile, title: fileName);
|
|
|
|
}
|
|
|
|
|
2022-06-30 18:08:49 -07:00
|
|
|
return entity != null;
|
2022-04-02 10:31:53 -07:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error saving file $e");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|