2021-04-10 06:28:12 +02:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
2021-05-23 19:24:16 +02:00
|
|
|
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';
|
2021-05-23 19:24:16 +02:00
|
|
|
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';
|
2021-05-23 19:24:16 +02:00
|
|
|
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]
|
2021-05-23 19:24:16 +02:00
|
|
|
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 &&
|
2022-07-08 10:30:51 +02:00
|
|
|
(e.response.statusCode == 404 || e.response.statusCode == 409) &&
|
2021-05-23 19:24:16 +02:00
|
|
|
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;
|
|
|
|
}
|