From e5434a64e42a36c37cccfdd93ddc3b43e2e940db Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Fri, 21 May 2021 04:10:35 +0800 Subject: [PATCH] Add use case to create dirs --- lib/entity/album.dart | 7 ++----- lib/use_case/create_dir.dart | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 lib/use_case/create_dir.dart diff --git a/lib/entity/album.dart b/lib/entity/album.dart index e5c8109e..fbdadae9 100644 --- a/lib/entity/album.dart +++ b/lib/entity/album.dart @@ -15,6 +15,7 @@ import 'package:nc_photos/exception.dart'; import 'package:nc_photos/int_util.dart' as int_util; import 'package:nc_photos/iterable_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/ls.dart'; import 'package:nc_photos/use_case/put_file_binary.dart'; @@ -304,7 +305,7 @@ class AlbumRemoteDataSource implements AlbumDataSource { if (e.response.statusCode == 404) { _log.info("[create] Missing album dir, creating"); // no dir - await _createDir(account); + await CreateDir(fileRepo)(account, getAlbumFileRoot(account)); // then retry await PutFileBinary(fileRepo)( 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"; } - Future _createDir(Account account) { - return FileWebdavDataSource().createDir(account, getAlbumFileRoot(account)); - } - static final _log = Logger("entity.album.AlbumRemoteDataSource"); } diff --git a/lib/use_case/create_dir.dart b/lib/use_case/create_dir.dart new file mode 100644 index 00000000..04814b77 --- /dev/null +++ b/lib/use_case/create_dir.dart @@ -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 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; +}