Add use case to create dirs

This commit is contained in:
Ming Ming 2021-05-21 04:10:35 +08:00
parent 9c37b1f0d8
commit e5434a64e4
2 changed files with 35 additions and 5 deletions

View file

@ -15,6 +15,7 @@ import 'package:nc_photos/exception.dart';
import 'package:nc_photos/int_util.dart' as int_util; import 'package:nc_photos/int_util.dart' as int_util;
import 'package:nc_photos/iterable_extension.dart'; import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/list_extension.dart'; import 'package:nc_photos/list_extension.dart';
import 'package:nc_photos/use_case/create_dir.dart';
import 'package:nc_photos/use_case/get_file_binary.dart'; import 'package:nc_photos/use_case/get_file_binary.dart';
import 'package:nc_photos/use_case/ls.dart'; import 'package:nc_photos/use_case/ls.dart';
import 'package:nc_photos/use_case/put_file_binary.dart'; import 'package:nc_photos/use_case/put_file_binary.dart';
@ -304,7 +305,7 @@ class AlbumRemoteDataSource implements AlbumDataSource {
if (e.response.statusCode == 404) { if (e.response.statusCode == 404) {
_log.info("[create] Missing album dir, creating"); _log.info("[create] Missing album dir, creating");
// no dir // no dir
await _createDir(account); await CreateDir(fileRepo)(account, getAlbumFileRoot(account));
// then retry // then retry
await PutFileBinary(fileRepo)( await PutFileBinary(fileRepo)(
account, filePath, utf8.encode(jsonEncode(album.toRemoteJson()))); account, filePath, utf8.encode(jsonEncode(album.toRemoteJson())));
@ -336,10 +337,6 @@ class AlbumRemoteDataSource implements AlbumDataSource {
return "${timestamp.toRadixString(16)}-${random.toRadixString(16).padLeft(6, '0')}.json"; return "${timestamp.toRadixString(16)}-${random.toRadixString(16).padLeft(6, '0')}.json";
} }
Future<void> _createDir(Account account) {
return FileWebdavDataSource().createDir(account, getAlbumFileRoot(account));
}
static final _log = Logger("entity.album.AlbumRemoteDataSource"); static final _log = Logger("entity.album.AlbumRemoteDataSource");
} }

View file

@ -0,0 +1,33 @@
import 'package:nc_photos/account.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/exception.dart';
import 'package:path/path.dart' as path_lib;
class CreateDir {
CreateDir(this.fileRepo);
/// Create a directory recursively at [path]
///
/// [path] should be a relative WebDAV path like
/// remote.php/dav/files/admin/new/dir
Future<void> call(Account account, String path) async {
try {
await fileRepo.createDir(account, path);
} on ApiException catch (e) {
if (e.response.statusCode == 409) {
// parent dir missing
if (path.contains("/") && path != "/") {
await call(account, path_lib.dirname(path));
await fileRepo.createDir(account, path);
} else {
// ?
rethrow;
}
} else {
rethrow;
}
}
}
final FileRepo fileRepo;
}