Skip to content

Commit 739bae9

Browse files
committed
Fix renaming, duplicating and moving on native and deep folders
1 parent 38fd0a6 commit 739bae9

File tree

7 files changed

+111
-12
lines changed

7 files changed

+111
-12
lines changed

packages/lw_file_system/lib/src/api/base/directory.dart

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@ mixin GeneralDirectoryFileSystem<T> on GeneralFileSystem {
156156
if (newName.startsWith('/')) {
157157
newName = newName.substring(1);
158158
}
159-
final asset = await getAsset(path);
160-
if (asset == null) return null;
161159
final newPath = '${path.substring(0, path.lastIndexOf('/') + 1)}$newName';
162160
return moveAsset(path, newPath);
163161
}
@@ -168,7 +166,7 @@ mixin GeneralDirectoryFileSystem<T> on GeneralFileSystem {
168166
bool forceSync = false,
169167
}) async {
170168
path = normalizePath(path);
171-
final asset = await getAsset(path);
169+
final asset = await getAsset(path, listLevel: allListLevel);
172170
if (asset == null) return null;
173171
if (asset is FileSystemFile<T>) {
174172
final data = asset.data;
@@ -229,15 +227,7 @@ mixin GeneralDirectoryFileSystem<T> on GeneralFileSystem {
229227
String path,
230228
String newPath, {
231229
bool forceSync = false,
232-
}) async {
233-
path = normalizePath(path);
234-
newPath = normalizePath(newPath);
235-
if (path == newPath) return getAsset(path);
236-
var asset = await duplicateAsset(path, newPath, forceSync: forceSync);
237-
if (asset == null) return null;
238-
await deleteAsset(path);
239-
return asset;
240-
}
230+
});
241231

242232
@override
243233
Future<void> reset() async {

packages/lw_file_system/lib/src/api/file_system_dav.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,45 @@ class DavRemoteDirectoryFileSystem extends RemoteDirectoryFileSystem {
3131
return RawFileSystemDirectory(location);
3232
}
3333

34+
@override
35+
Future<FileSystemEntity<Uint8List>?> moveAsset(
36+
String path,
37+
String newPath, {
38+
bool forceSync = false,
39+
}) async {
40+
path = normalizePath(path);
41+
newPath = normalizePath(newPath);
42+
if (path == newPath) return getAsset(path);
43+
44+
if (path.startsWith('/')) {
45+
path = path.substring(1);
46+
}
47+
if (newPath.startsWith('/')) {
48+
newPath = newPath.substring(1);
49+
}
50+
51+
final destinationUri = storage.buildVariantUri(
52+
variant: config.currentPathVariant,
53+
path: newPath.split('/'),
54+
);
55+
56+
if (destinationUri == null) return null;
57+
58+
final response = await createRequest(
59+
path.split('/'),
60+
method: 'MOVE',
61+
headers: {'Destination': destinationUri.toString(), 'Overwrite': 'T'},
62+
);
63+
64+
if (response == null) return null;
65+
66+
if (response.statusCode != 201 && response.statusCode != 204) {
67+
throw Exception('Failed to move asset: ${response.statusCode}');
68+
}
69+
70+
return getAsset(newPath);
71+
}
72+
3473
@override
3574
Future<void> deleteAsset(String path) async {
3675
path = normalizePath(path);

packages/lw_file_system/lib/src/api/file_system_html.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ mixin WebFileSystem on GeneralFileSystem {
8484
class WebDirectoryFileSystem extends DirectoryFileSystem with WebFileSystem {
8585
WebDirectoryFileSystem({required super.config, super.createDefault});
8686

87+
@override
88+
Future<FileSystemEntity<Uint8List>?> moveAsset(
89+
String path,
90+
String newPath, {
91+
bool forceSync = false,
92+
}) async {
93+
path = normalizePath(path);
94+
newPath = normalizePath(newPath);
95+
if (path == newPath) return getAsset(path);
96+
var asset = await duplicateAsset(path, newPath, forceSync: forceSync);
97+
if (asset == null) return null;
98+
await deleteAsset(path);
99+
return asset;
100+
}
101+
87102
@override
88103
Future<void> deleteAsset(String path) async {
89104
path = normalizePath(path);

packages/lw_file_system/lib/src/api/file_system_html_stub.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ class WebDirectoryFileSystem extends DirectoryFileSystem {
4747
Future<void> runInitialize() {
4848
throw UnimplementedError();
4949
}
50+
51+
@override
52+
Future<FileSystemEntity<Uint8List>?> moveAsset(
53+
String path,
54+
String newPath, {
55+
bool forceSync = false,
56+
}) {
57+
// TODO: implement moveAsset
58+
throw UnimplementedError();
59+
}
5060
}
5161

5262
class WebKeyFileSystem extends KeyFileSystem {

packages/lw_file_system/lib/src/api/file_system_io.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,35 @@ class IODirectoryFileSystem extends DirectoryFileSystem {
8484
});
8585
}
8686

87+
@override
88+
Future<FileSystemEntity<Uint8List>?> moveAsset(
89+
String path,
90+
String newPath, {
91+
bool forceSync = false,
92+
}) async {
93+
path = normalizePath(path);
94+
newPath = normalizePath(newPath);
95+
if (path == newPath) return getAsset(path);
96+
97+
return _lock.synchronized(() async {
98+
final oldFile = File(await getAbsolutePath(path));
99+
final oldDir = Directory(await getAbsolutePath(path));
100+
final newAbsolutePath = await getAbsolutePath(newPath);
101+
102+
if (await oldFile.exists()) {
103+
await oldFile.rename(newAbsolutePath);
104+
return FileSystemFile(
105+
AssetLocation.local(newPath),
106+
data: await File(newAbsolutePath).readAsBytes(),
107+
);
108+
} else if (await oldDir.exists()) {
109+
await oldDir.rename(newAbsolutePath);
110+
return getAsset(newPath, listLevel: 0);
111+
}
112+
return null;
113+
});
114+
}
115+
87116
@override
88117
Future<void> deleteAsset(String path) async {
89118
path = normalizePath(path);

packages/lw_file_system/lib/src/api/file_system_remote.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mixin RemoteFileSystem on GeneralFileSystem {
1919
String method = 'GET',
2020
List<int>? bodyBytes,
2121
String? body,
22+
Map<String, String>? headers,
2223
}) async {
2324
final url = storage.buildVariantUri(
2425
variant: config.currentPathVariant,
@@ -33,6 +34,11 @@ mixin RemoteFileSystem on GeneralFileSystem {
3334
'Authorization',
3435
'Basic ${base64Encode(utf8.encode('${storage.username}:${await config.passwordStorage.read(storage)}'))}',
3536
);
37+
if (headers != null) {
38+
headers.forEach((key, value) {
39+
request.headers.add(key, value);
40+
});
41+
}
3642
if (body != null) {
3743
final bytes = utf8.encode(body);
3844
request.headers.add('Content-Length', bytes.length.toString());

packages/lw_file_system/lib/src/api/typed.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ class TypedDirectoryFileSystem<T> extends TypedFileSystem<T>
143143
@override
144144
Future<bool> moveAbsolute(String oldPath, String newPath) =>
145145
fileSystem.moveAbsolute(oldPath, newPath);
146+
147+
@override
148+
Future<FileSystemEntity<T>?> moveAsset(
149+
String path,
150+
String newPath, {
151+
bool forceSync = false,
152+
}) {
153+
// TODO: implement moveAsset
154+
throw UnimplementedError();
155+
}
146156
}
147157

148158
class TypedKeyFileSystem<T> extends TypedFileSystem<T>

0 commit comments

Comments
 (0)