nc-photos/app/lib/use_case/move.dart

97 lines
2.9 KiB
Dart
Raw Normal View History

2021-08-13 12:39:17 +02:00
import 'package:event_bus/event_bus.dart';
import 'package:kiwi/kiwi.dart';
2021-07-22 08:13:27 +02:00
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/di_container.dart';
2021-07-22 08:13:27 +02:00
import 'package:nc_photos/entity/file.dart';
2021-10-08 09:01:27 +02:00
import 'package:nc_photos/entity/file_util.dart' as file_util;
2021-08-13 12:39:17 +02:00
import 'package:nc_photos/event/event.dart';
2021-07-22 08:13:27 +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';
2022-01-31 10:47:37 +01:00
import 'package:path/path.dart' as path_lib;
2021-07-22 08:13:27 +02:00
2022-12-16 16:01:04 +01:00
part 'move.g.dart';
@npLog
2021-07-22 08:13:27 +02:00
class Move {
Move(this._c) : assert(require(_c));
static bool require(DiContainer c) => DiContainer.has(c, DiType.fileRepo);
2021-07-22 08:13:27 +02:00
/// Move a file from its original location to [destination]
Future<void> call(
Account account,
File file,
String destination, {
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,
2021-07-22 08:13:27 +02:00
}) async {
final to = _renameDestination(destination, retryCount);
if (retryCount > 1) {
_log.info("[call] Retry with: '$to'");
}
2021-07-22 08:13:27 +02:00
try {
await _c.fileRepo
.move(account, file, to, shouldOverwrite: shouldOverwrite);
2021-07-22 08:13:27 +02:00
} catch (e) {
if (e is ApiException) {
if (e.response.statusCode == 409 && shouldCreateMissingDir) {
// no dir
_log.info("[call] Auto creating parent dirs");
2022-01-31 10:47:37 +01:00
await CreateDir(_c.fileRepo)(account, path_lib.dirname(to));
await _c.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;
}
2021-07-22 08:13:27 +02:00
} else {
rethrow;
}
}
2021-08-13 12:39:17 +02:00
KiwiContainer()
.resolve<EventBus>()
.fire(FileMovedEvent(account, file, destination));
2021-07-22 08:13:27 +02:00
}
String _renameDestination(String destination, int retryCount) {
if (retryCount < 2) {
return destination;
}
2021-10-08 09:01:27 +02:00
final newName =
2022-01-31 10:47:37 +01:00
file_util.renameConflict(path_lib.basename(destination), retryCount);
return "${path_lib.dirname(destination)}/$newName";
}
final DiContainer _c;
2021-07-22 08:13:27 +02:00
}