mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-02-25 02:48:54 +01:00
Rename file if the same name exists in destination
This commit is contained in:
parent
0689040ccc
commit
2a462cf906
2 changed files with 64 additions and 10 deletions
|
@ -17,17 +17,54 @@ class Move {
|
||||||
File file,
|
File file,
|
||||||
String destination, {
|
String destination, {
|
||||||
bool shouldCreateMissingDir = false,
|
bool shouldCreateMissingDir = false,
|
||||||
|
bool shouldOverwrite = false,
|
||||||
|
bool shouldRenameOnOverwrite = false,
|
||||||
|
}) =>
|
||||||
|
_doWork(
|
||||||
|
account,
|
||||||
|
file,
|
||||||
|
destination,
|
||||||
|
shouldCreateMissingDir: shouldCreateMissingDir,
|
||||||
|
shouldOverwrite: shouldOverwrite,
|
||||||
|
shouldRenameOnOverwrite: shouldRenameOnOverwrite,
|
||||||
|
);
|
||||||
|
|
||||||
|
Future<void> _doWork(
|
||||||
|
Account account,
|
||||||
|
File file,
|
||||||
|
String destination, {
|
||||||
|
required bool shouldCreateMissingDir,
|
||||||
|
required bool shouldOverwrite,
|
||||||
|
required bool shouldRenameOnOverwrite,
|
||||||
|
int retryCount = 1,
|
||||||
}) async {
|
}) async {
|
||||||
|
final to = _renameDestination(destination, retryCount);
|
||||||
|
if (retryCount > 1) {
|
||||||
|
_log.info("[call] Retry with: '$to'");
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
await fileRepo.move(account, file, destination);
|
await fileRepo.move(account, file, to, shouldOverwrite: shouldOverwrite);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e is ApiException &&
|
if (e is ApiException) {
|
||||||
e.response.statusCode == 409 &&
|
if (e.response.statusCode == 409 && shouldCreateMissingDir) {
|
||||||
shouldCreateMissingDir) {
|
|
||||||
// no dir
|
// no dir
|
||||||
_log.info("[call] Auto creating parent dirs");
|
_log.info("[call] Auto creating parent dirs");
|
||||||
await CreateDir(fileRepo)(account, path.dirname(destination));
|
await CreateDir(fileRepo)(account, path.dirname(to));
|
||||||
await fileRepo.move(account, file, destination);
|
await fileRepo.move(account, file, to,
|
||||||
|
shouldOverwrite: shouldOverwrite);
|
||||||
|
} else if (e.response.statusCode == 412 && shouldRenameOnOverwrite) {
|
||||||
|
return _doWork(
|
||||||
|
account,
|
||||||
|
file,
|
||||||
|
to,
|
||||||
|
shouldCreateMissingDir: shouldCreateMissingDir,
|
||||||
|
shouldOverwrite: shouldOverwrite,
|
||||||
|
shouldRenameOnOverwrite: shouldRenameOnOverwrite,
|
||||||
|
retryCount: retryCount + 1,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
rethrow;
|
rethrow;
|
||||||
}
|
}
|
||||||
|
@ -37,6 +74,19 @@ class Move {
|
||||||
.fire(FileMovedEvent(account, file, destination));
|
.fire(FileMovedEvent(account, file, destination));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String _renameDestination(String destination, int retryCount) {
|
||||||
|
if (retryCount < 2) {
|
||||||
|
return destination;
|
||||||
|
}
|
||||||
|
final temp =
|
||||||
|
"${path.dirname(destination)}/${path.basenameWithoutExtension(destination)} ($retryCount)";
|
||||||
|
if (path.extension(destination).isEmpty) {
|
||||||
|
return temp;
|
||||||
|
} else {
|
||||||
|
return "$temp.${path.extension(destination)}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
final FileRepo fileRepo;
|
final FileRepo fileRepo;
|
||||||
|
|
||||||
static final _log = Logger("use_case.move.Move");
|
static final _log = Logger("use_case.move.Move");
|
||||||
|
|
|
@ -10,8 +10,12 @@ class RestoreTrashbin {
|
||||||
RestoreTrashbin(this.fileRepo);
|
RestoreTrashbin(this.fileRepo);
|
||||||
|
|
||||||
Future<void> call(Account account, File file) async {
|
Future<void> call(Account account, File file) async {
|
||||||
await Move(fileRepo).call(account, file,
|
await Move(fileRepo)(
|
||||||
"remote.php/dav/trashbin/${account.username}/restore/${path.basename(file.path)}");
|
account,
|
||||||
|
file,
|
||||||
|
"remote.php/dav/trashbin/${account.username}/restore/${path.basename(file.path)}",
|
||||||
|
shouldOverwrite: true,
|
||||||
|
);
|
||||||
KiwiContainer()
|
KiwiContainer()
|
||||||
.resolve<EventBus>()
|
.resolve<EventBus>()
|
||||||
.fire(FileTrashbinRestoredEvent(account, file));
|
.fire(FileTrashbinRestoredEvent(account, file));
|
||||||
|
|
Loading…
Reference in a new issue