Переход к медиа на экране профиля, часть issue #45
This commit is contained in:
@@ -51,6 +51,52 @@ Future<MediaSaveResult> saveImageFromUrl(String url) async {
|
||||
}
|
||||
}
|
||||
|
||||
enum SaveMediaKind { image, video, file }
|
||||
|
||||
Future<MediaSaveResult> saveMediaFile({
|
||||
required String cacheName,
|
||||
required Future<String?> Function() resolveUrl,
|
||||
required String saveName,
|
||||
required SaveMediaKind kind,
|
||||
}) async {
|
||||
try {
|
||||
var file = await MediaCache.existing(cacheName);
|
||||
if (file == null) {
|
||||
final url = await resolveUrl();
|
||||
if (url == null || url.isEmpty) {
|
||||
return const MediaSaveResult(ok: false, error: 'нет ссылки');
|
||||
}
|
||||
file = await MediaCache.getOrDownload(cacheName, url);
|
||||
}
|
||||
if (file == null) {
|
||||
return const MediaSaveResult(ok: false, error: 'не удалось загрузить');
|
||||
}
|
||||
|
||||
final toGallery =
|
||||
kind == SaveMediaKind.image || kind == SaveMediaKind.video;
|
||||
if (!kIsWeb && (Platform.isAndroid || Platform.isIOS) && toGallery) {
|
||||
final state = await PhotoManager.requestPermissionExtend();
|
||||
if (!state.isAuth && !state.hasAccess) {
|
||||
return const MediaSaveResult(ok: false, error: 'нет доступа к галерее');
|
||||
}
|
||||
if (kind == SaveMediaKind.video) {
|
||||
await PhotoManager.editor.saveVideo(file, title: saveName);
|
||||
} else {
|
||||
final bytes = await file.readAsBytes();
|
||||
await PhotoManager.editor.saveImage(bytes, filename: saveName);
|
||||
}
|
||||
return const MediaSaveResult(ok: true, toGallery: true);
|
||||
}
|
||||
|
||||
final dir = await _targetDirectory();
|
||||
final target = File('${dir.path}${Platform.pathSeparator}$saveName');
|
||||
await file.copy(target.path);
|
||||
return MediaSaveResult(ok: true, location: target.path);
|
||||
} catch (e) {
|
||||
return MediaSaveResult(ok: false, error: e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Future<Directory> _targetDirectory() async {
|
||||
try {
|
||||
final downloads = await getDownloadsDirectory();
|
||||
|
||||
Reference in New Issue
Block a user