Skip to content

Commit ad67d98

Browse files
committed
Improve dav file system interface
1 parent 792044f commit ad67d98

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,19 @@ class DavRemoteDirectoryFileSystem extends RemoteDirectoryFileSystem {
6464
final rootDirectory =
6565
storage.buildVariantUri(variant: config.currentPathVariant);
6666
if (response == null) {
67-
return null;
67+
throw Exception('Failed to read asset: ${storage.identifier} $path');
6868
}
6969
var content = await getBodyString(response);
7070
if (response.statusCode == 404 && path.isEmpty) {
7171
await createRequest([], method: 'MKCOL');
7272
response = await createRequest(path.split('/'), method: 'PROPFIND');
73+
content = await getBodyString(response!);
7374
}
74-
if (response?.statusCode != 207 ||
75+
if (response.statusCode != 207 ||
7576
fileName == null ||
7677
rootDirectory == null) {
77-
return null;
78+
throw Exception(
79+
'Failed to read asset: ${response.statusCode} ${response.reasonPhrase} $path');
7880
}
7981
final xml = XmlDocument.parse(content);
8082
final responses = xml.findAllElements('d:response').where((element) {
@@ -83,7 +85,8 @@ class DavRemoteDirectoryFileSystem extends RemoteDirectoryFileSystem {
8385
}).toList();
8486

8587
if (responses.isEmpty) {
86-
return null;
88+
throw Exception(
89+
'Failed to read asset: No matching response found for $fileName in $path');
8790
}
8891

8992
final currentElement = responses.first;
@@ -201,25 +204,19 @@ class DavRemoteDirectoryFileSystem extends RemoteDirectoryFileSystem {
201204
@override
202205
Future<void> updateFile(String path, Uint8List data,
203206
{bool forceSync = false}) async {
204-
// Create a copy of the path and remove the leading slash if it exists
205-
String modifiedPath = path;
206-
if (modifiedPath.startsWith('/')) {
207-
modifiedPath = modifiedPath.substring(1);
208-
}
209-
// Cache check
207+
path = normalizePath(path);
208+
if (path.startsWith('/')) path = path.substring(1);
210209
if (!forceSync && storage.hasDocumentCached(path)) {
211210
cacheContent(path, data);
212211
}
213212

214-
// Create directory if not exists
215213
final directoryPath = p.dirname(path);
216214
if (!await hasAsset(directoryPath)) {
217215
await createDirectory(directoryPath);
218216
}
219217

220-
// Request to overwrite the file
221-
final response = await createRequest(p.split(modifiedPath),
222-
method: 'PUT', bodyBytes: data);
218+
final response =
219+
await createRequest(path.split('/'), method: 'PUT', bodyBytes: data);
223220
if (response?.statusCode == 200 ||
224221
response?.statusCode == 201 ||
225222
response?.statusCode == 204) {

0 commit comments

Comments
 (0)