mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-01-23 17:26:18 +01:00
36 lines
1 KiB
Dart
36 lines
1 KiB
Dart
|
import 'dart:io';
|
||
|
|
||
|
import 'package:flutter/services.dart';
|
||
|
import 'package:nc_photos/account.dart';
|
||
|
import 'package:nc_photos/entity/file.dart';
|
||
|
import 'package:nc_photos/exception.dart';
|
||
|
import 'package:nc_photos/mobile/android/media_store.dart';
|
||
|
import 'package:nc_photos/platform/downloader.dart' as itf;
|
||
|
import 'package:path/path.dart' as path;
|
||
|
|
||
|
class Downloader extends itf.Downloader {
|
||
|
@override
|
||
|
downloadFile(Account account, File file) {
|
||
|
if (Platform.isAndroid) {
|
||
|
return _downloadFileAndroid(account, file);
|
||
|
} else {
|
||
|
throw UnimplementedError();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<void> _downloadFileAndroid(Account account, File file) async {
|
||
|
final fileRepo = FileRepo(FileCachedDataSource());
|
||
|
final fileContent = await fileRepo.getBinary(account, file);
|
||
|
try {
|
||
|
await MediaStore.saveFileToDownload(
|
||
|
path.basename(file.path), fileContent);
|
||
|
} on PlatformException catch (e) {
|
||
|
if (e.code == MediaStore.exceptionCodePermissionError) {
|
||
|
throw PermissionException();
|
||
|
} else {
|
||
|
rethrow;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|