nc-photos/app/lib/use_case/put_file_binary.dart

42 lines
1.1 KiB
Dart
Raw Normal View History

2021-04-10 06:28:12 +02:00
import 'dart:typed_data';
import 'package:logging/logging.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/account.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/exception.dart';
import 'package:nc_photos/use_case/create_dir.dart';
2022-12-16 16:01:04 +01:00
import 'package:np_codegen/np_codegen.dart';
import 'package:path/path.dart' as path_lib;
2021-04-10 06:28:12 +02:00
2022-12-16 16:01:04 +01:00
part 'put_file_binary.g.dart';
@npLog
2021-04-10 06:28:12 +02:00
class PutFileBinary {
PutFileBinary(this.fileRepo);
/// Upload file to [path]
Future<void> call(
Account account,
String path,
Uint8List content, {
bool shouldCreateMissingDir = false,
}) async {
try {
await fileRepo.putBinary(account, path, content);
} catch (e) {
if (e is ApiException &&
(e.response.statusCode == 404 || e.response.statusCode == 409) &&
shouldCreateMissingDir) {
// no dir
_log.info("[call] Auto creating parent dirs");
await CreateDir(fileRepo)(account, path_lib.dirname(path));
await fileRepo.putBinary(account, path, content);
} else {
rethrow;
}
}
}
2021-04-10 06:28:12 +02:00
final FileRepo fileRepo;
}