-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(nextcloud): Add low-level helpers for WebDavResponse #2561
base: main
Are you sure you want to change the base?
Changes from all commits
8de0f13
a6d2a30
81ab415
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export 'options_parser.dart'; | ||
export 'webdav_response_converter.dart'; | ||
export 'webdav_response_helpers.dart'; | ||
export 'webdav_uri.dart'; | ||
export 'xml_date_time_converter.dart'; | ||
export 'xml_duration_converter.dart'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'package:nextcloud/src/api/webdav/models/models.dart'; | ||
|
||
/// Helpers for WebDavResponse | ||
extension WebDavResponseHelpers on WebDavResponse { | ||
/// All available props. | ||
WebDavProp get props => propstats.singleWhere((propstat) => propstat.status.contains('200 OK')).prop; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the typical prop count on a file? How expensive is this operation? The second commit calls There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are usually two propstats (one 200, one 404). This could also be changed to a |
||
|
||
/// The path of the resource. | ||
PathUri get path { | ||
final href = PathUri.parse(Uri.decodeFull(this.href!)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the null assertion? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There can be responses without a href which are not valid. Consumers need to filter these out (we already did in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But this means that unfiltered responses will throw here. Then the filtering can be done based on the |
||
return PathUri( | ||
isAbsolute: false, | ||
isDirectory: href.isDirectory, | ||
// The server might be hosted at a subpath, so we don't have a fixed number of path segments to remove | ||
pathSegments: href.pathSegments.sublist(href.pathSegments.indexOf('remote.php') + 2), | ||
); | ||
} | ||
|
||
/// The name of the resource. | ||
String get name => path.name; | ||
|
||
/// Whether the file is hidden. | ||
bool get isHidden => props.ncHidden ?? name.startsWith('.'); | ||
|
||
/// Whether the file is a directory | ||
bool get isDirectory => props.davResourcetype?.collection != null || path.isDirectory; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
import 'package:nextcloud/src/api/webdav/models/models.dart'; | ||
import 'package:nextcloud/src/api/webdav/utils/utils.dart'; | ||
import 'package:test/expect.dart'; | ||
import 'package:test/scaffolding.dart'; | ||
|
||
void main() { | ||
test('props', () { | ||
expect( | ||
const WebDavResponse( | ||
href: null, | ||
propstats: [ | ||
WebDavPropstat( | ||
status: 'HTTP/1.1 404 Not Found', | ||
prop: WebDavProp(ocSize: 1), | ||
), | ||
WebDavPropstat( | ||
status: 'HTTP/1.1 200 OK', | ||
prop: WebDavProp(ocSize: 2), | ||
), | ||
], | ||
).props.ocSize, | ||
2, | ||
); | ||
}); | ||
|
||
group('path', () { | ||
group('Root', () { | ||
test('File', () { | ||
expect( | ||
const WebDavResponse( | ||
href: '/remote.php/webdav/abc/def.txt', | ||
propstats: [], | ||
).path, | ||
PathUri.parse('abc/def.txt'), | ||
); | ||
}); | ||
|
||
test('Directory', () { | ||
expect( | ||
const WebDavResponse( | ||
href: '/remote.php/webdav/abc/def/', | ||
propstats: [], | ||
).path, | ||
PathUri.parse('abc/def/'), | ||
); | ||
}); | ||
}); | ||
|
||
group('Subpath', () { | ||
test('File', () { | ||
expect( | ||
const WebDavResponse( | ||
href: '/subpath/remote.php/webdav/abc/def.txt', | ||
propstats: [], | ||
).path, | ||
PathUri.parse('abc/def.txt'), | ||
); | ||
}); | ||
|
||
test('Directory', () { | ||
expect( | ||
const WebDavResponse( | ||
href: '/subpath/remote.php/webdav/abc/def/', | ||
propstats: [], | ||
).path, | ||
PathUri.parse('abc/def/'), | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
group('name', () { | ||
test('File', () { | ||
expect( | ||
const WebDavResponse( | ||
href: '/remote.php/webdav/abc/def.txt', | ||
propstats: [], | ||
).name, | ||
'def.txt', | ||
); | ||
}); | ||
|
||
test('Directory', () { | ||
expect( | ||
const WebDavResponse( | ||
href: '/remote.php/webdav/abc/def/', | ||
propstats: [], | ||
).name, | ||
'def', | ||
); | ||
}); | ||
}); | ||
|
||
group('isHidden', () { | ||
group('From prop', () { | ||
test('File', () { | ||
expect( | ||
const WebDavResponse( | ||
href: '/remote.php/webdav/abc/def.txt', | ||
propstats: [ | ||
WebDavPropstat( | ||
status: 'HTTP/1.1 200 OK', | ||
prop: WebDavProp( | ||
ncHidden: true, | ||
), | ||
), | ||
], | ||
).isHidden, | ||
true, | ||
); | ||
}); | ||
|
||
test('Directory', () { | ||
expect( | ||
const WebDavResponse( | ||
href: '/remote.php/webdav/abc/def/', | ||
propstats: [ | ||
WebDavPropstat( | ||
status: 'HTTP/1.1 200 OK', | ||
prop: WebDavProp( | ||
ncHidden: true, | ||
), | ||
), | ||
], | ||
).isHidden, | ||
true, | ||
); | ||
}); | ||
}); | ||
|
||
group('From path', () { | ||
test('File', () { | ||
expect( | ||
const WebDavResponse( | ||
href: '/remote.php/webdav/abc/.def.txt', | ||
propstats: [ | ||
WebDavPropstat( | ||
status: 'HTTP/1.1 200 OK', | ||
prop: WebDavProp(), | ||
), | ||
], | ||
).isHidden, | ||
true, | ||
); | ||
}); | ||
|
||
test('Directory', () { | ||
expect( | ||
const WebDavResponse( | ||
href: '/remote.php/webdav/abc/.def/', | ||
propstats: [ | ||
WebDavPropstat( | ||
status: 'HTTP/1.1 200 OK', | ||
prop: WebDavProp(), | ||
), | ||
], | ||
).isHidden, | ||
true, | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
group('isDirectory', () { | ||
group('From prop', () { | ||
test('File', () { | ||
expect( | ||
const WebDavResponse( | ||
href: '/remote.php/webdav/abc/def.txt', | ||
propstats: [ | ||
WebDavPropstat( | ||
status: 'HTTP/1.1 200 OK', | ||
prop: WebDavProp( | ||
davResourcetype: WebDavResourcetype( | ||
collection: null, | ||
), | ||
), | ||
), | ||
], | ||
).isDirectory, | ||
false, | ||
); | ||
}); | ||
|
||
test('Directory', () { | ||
expect( | ||
const WebDavResponse( | ||
href: '/remote.php/webdav/abc/def/', | ||
propstats: [ | ||
WebDavPropstat( | ||
status: 'HTTP/1.1 200 OK', | ||
prop: WebDavProp( | ||
davResourcetype: WebDavResourcetype( | ||
collection: [], | ||
), | ||
), | ||
), | ||
], | ||
).isDirectory, | ||
true, | ||
); | ||
}); | ||
}); | ||
|
||
group('From path', () { | ||
test('File', () { | ||
expect( | ||
const WebDavResponse( | ||
href: '/remote.php/webdav/abc/def.txt', | ||
propstats: [ | ||
WebDavPropstat( | ||
status: 'HTTP/1.1 200 OK', | ||
prop: WebDavProp(), | ||
), | ||
], | ||
).isDirectory, | ||
false, | ||
); | ||
}); | ||
|
||
test('Directory', () { | ||
expect( | ||
const WebDavResponse( | ||
href: '/remote.php/webdav/abc/def/', | ||
propstats: [ | ||
WebDavPropstat( | ||
status: 'HTTP/1.1 200 OK', | ||
prop: WebDavProp(), | ||
), | ||
], | ||
).isDirectory, | ||
true, | ||
); | ||
}); | ||
}); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we just make these helpers available on the
WebDavResponse
directly?I understand that this will make the generation a bit harder, but it also allows us to cache these operations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I can give it a try. Caching is indeed a good reason to move it inline.