2022-08-08 17:46:12 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2022-08-11 15:13:33 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-08-08 17:46:12 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2024-02-24 05:38:57 +02:00
|
|
|
import 'package:immich_mobile/extensions/response_extensions.dart';
|
2022-11-08 19:00:24 +02:00
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2022-08-08 17:46:12 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/api.provider.dart';
|
2023-10-19 22:19:00 +02:00
|
|
|
import 'package:logging/logging.dart';
|
2022-08-08 17:46:12 +02:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:share_plus/share_plus.dart';
|
|
|
|
import 'api.service.dart';
|
|
|
|
|
|
|
|
final shareServiceProvider =
|
2022-08-11 15:13:33 +02:00
|
|
|
Provider((ref) => ShareService(ref.watch(apiServiceProvider)));
|
2022-08-08 17:46:12 +02:00
|
|
|
|
|
|
|
class ShareService {
|
|
|
|
final ApiService _apiService;
|
2023-10-19 22:19:00 +02:00
|
|
|
final Logger _log = Logger("ShareService");
|
2022-08-08 17:46:12 +02:00
|
|
|
|
|
|
|
ShareService(this._apiService);
|
|
|
|
|
2023-10-19 22:19:00 +02:00
|
|
|
Future<bool> shareAsset(Asset asset) async {
|
|
|
|
return await shareAssets([asset]);
|
2022-08-08 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 22:19:00 +02:00
|
|
|
Future<bool> shareAssets(List<Asset> assets) async {
|
|
|
|
try {
|
|
|
|
final downloadedXFiles = <XFile>[];
|
|
|
|
|
|
|
|
for (var asset in assets) {
|
2024-02-20 18:03:24 +02:00
|
|
|
if (asset.isLocal) {
|
|
|
|
// Prefer local assets to share
|
|
|
|
File? f = await asset.local!.file;
|
|
|
|
downloadedXFiles.add(XFile(f!.path));
|
|
|
|
} else if (asset.isRemote) {
|
|
|
|
// Download remote asset otherwise
|
2023-10-19 22:19:00 +02:00
|
|
|
final tempDir = await getTemporaryDirectory();
|
|
|
|
final fileName = asset.fileName;
|
|
|
|
final tempFile = await File('${tempDir.path}/$fileName').create();
|
2024-02-08 23:57:54 +02:00
|
|
|
final res = await _apiService.downloadApi
|
|
|
|
.downloadFileWithHttpInfo(asset.remoteId!);
|
2023-10-19 22:19:00 +02:00
|
|
|
|
|
|
|
if (res.statusCode != 200) {
|
|
|
|
_log.severe(
|
2024-02-24 05:38:57 +02:00
|
|
|
"Asset download for ${asset.fileName} failed",
|
|
|
|
res.toLoggerString(),
|
2023-10-19 22:19:00 +02:00
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tempFile.writeAsBytesSync(res.bodyBytes);
|
|
|
|
downloadedXFiles.add(XFile(tempFile.path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (downloadedXFiles.isEmpty) {
|
|
|
|
_log.warning("No asset can be retrieved for share");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (downloadedXFiles.length != assets.length) {
|
|
|
|
_log.warning(
|
|
|
|
"Partial share - Requested: ${assets.length}, Sharing: ${downloadedXFiles.length}",
|
|
|
|
);
|
2022-11-08 19:00:24 +02:00
|
|
|
}
|
2022-08-08 17:46:12 +02:00
|
|
|
|
2023-10-19 22:19:00 +02:00
|
|
|
Share.shareXFiles(
|
|
|
|
downloadedXFiles,
|
|
|
|
sharePositionOrigin: Rect.zero,
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
2024-02-24 05:38:57 +02:00
|
|
|
_log.severe("Share failed", error);
|
2023-10-19 22:19:00 +02:00
|
|
|
}
|
|
|
|
return false;
|
2022-08-08 17:46:12 +02:00
|
|
|
}
|
|
|
|
}
|