From de9c6e15ef5886ded8c0a8a129ea8a8cca692b03 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Thu, 22 Jul 2021 14:13:27 +0800 Subject: [PATCH] Move op --- lib/use_case/move.dart | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 lib/use_case/move.dart diff --git a/lib/use_case/move.dart b/lib/use_case/move.dart new file mode 100644 index 00000000..3fd4c910 --- /dev/null +++ b/lib/use_case/move.dart @@ -0,0 +1,37 @@ +import 'package:logging/logging.dart'; +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'; +import 'package:path/path.dart' as path; + +class Move { + Move(this.fileRepo); + + /// Move a file from its original location to [destination] + Future call( + Account account, + File file, + String destination, { + bool shouldCreateMissingDir = false, + }) async { + try { + await fileRepo.move(account, file, destination); + } catch (e) { + if (e is ApiException && + e.response.statusCode == 409 && + shouldCreateMissingDir) { + // no dir + _log.info("[call] Auto creating parent dirs"); + await CreateDir(fileRepo)(account, path.dirname(destination)); + await fileRepo.move(account, file, destination); + } else { + rethrow; + } + } + } + + final FileRepo fileRepo; + + static final _log = Logger("use_case.move.Move"); +}