nc-photos/lib/mobile/downloader.dart
2021-04-10 12:28:12 +08:00

35 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;
}
}
}
}