2021-04-10 06:28:12 +02:00
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:nc_photos/account.dart';
|
|
|
|
import 'package:nc_photos/entity/file.dart';
|
2021-05-24 09:09:25 +02:00
|
|
|
import 'package:nc_photos/entity/file/data_source.dart';
|
2021-04-10 06:28:12 +02:00
|
|
|
import 'package:nc_photos/exception.dart';
|
|
|
|
import 'package:nc_photos/mobile/android/media_store.dart';
|
|
|
|
import 'package:nc_photos/platform/downloader.dart' as itf;
|
2021-04-29 17:42:44 +02:00
|
|
|
import 'package:nc_photos/platform/k.dart' as platform_k;
|
2021-04-29 17:44:00 +02:00
|
|
|
import 'package:nc_photos/use_case/get_file_binary.dart';
|
2021-04-10 06:28:12 +02:00
|
|
|
import 'package:path/path.dart' as path;
|
|
|
|
|
|
|
|
class Downloader extends itf.Downloader {
|
|
|
|
@override
|
|
|
|
downloadFile(Account account, File file) {
|
2021-04-29 17:42:44 +02:00
|
|
|
if (platform_k.isAndroid) {
|
2021-04-10 06:28:12 +02:00
|
|
|
return _downloadFileAndroid(account, file);
|
|
|
|
} else {
|
|
|
|
throw UnimplementedError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 17:09:46 +02:00
|
|
|
Future<String> _downloadFileAndroid(Account account, File file) async {
|
2021-04-10 06:28:12 +02:00
|
|
|
final fileRepo = FileRepo(FileCachedDataSource());
|
2021-04-29 17:44:00 +02:00
|
|
|
final fileContent = await GetFileBinary(fileRepo)(account, file);
|
2021-04-10 06:28:12 +02:00
|
|
|
try {
|
2021-04-30 17:09:46 +02:00
|
|
|
return await MediaStore.saveFileToDownload(
|
2021-04-10 06:28:12 +02:00
|
|
|
path.basename(file.path), fileContent);
|
|
|
|
} on PlatformException catch (e) {
|
|
|
|
if (e.code == MediaStore.exceptionCodePermissionError) {
|
|
|
|
throw PermissionException();
|
|
|
|
} else {
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|