List shares for a dir

This commit is contained in:
Ming Ming 2021-08-13 18:42:31 +08:00
parent b1d37f7b73
commit b9bf48c6ed
2 changed files with 35 additions and 9 deletions

View file

@ -5,6 +5,7 @@ import 'package:nc_photos/entity/file.dart';
class Share with EquatableMixin { class Share with EquatableMixin {
Share({ Share({
required this.id, required this.id,
required this.path,
required this.shareType, required this.shareType,
required this.shareWith, required this.shareWith,
required this.shareWithDisplayName, required this.shareWithDisplayName,
@ -14,6 +15,7 @@ class Share with EquatableMixin {
toString() { toString() {
return "$runtimeType {" return "$runtimeType {"
"id: $id, " "id: $id, "
"path: $path, "
"shareType: $shareType, " "shareType: $shareType, "
"shareWith: $shareWith, " "shareWith: $shareWith, "
"shareWithDisplayName: $shareWithDisplayName, " "shareWithDisplayName: $shareWithDisplayName, "
@ -23,12 +25,14 @@ class Share with EquatableMixin {
@override @override
get props => [ get props => [
id, id,
path,
shareType, shareType,
shareWith, shareWith,
shareWithDisplayName, shareWithDisplayName,
]; ];
final String id; final String id;
final String path;
final int shareType; final int shareType;
final String shareWith; final String shareWith;
final String shareWithDisplayName; final String shareWithDisplayName;
@ -41,6 +45,10 @@ class ShareRepo {
Future<List<Share>> list(Account account, File file) => Future<List<Share>> list(Account account, File file) =>
this.dataSrc.list(account, file); this.dataSrc.list(account, file);
/// See [ShareDataSource.listDir]
Future<List<Share>> listDir(Account account, File dir) =>
this.dataSrc.listDir(account, dir);
/// See [ShareDataSource.create] /// See [ShareDataSource.create]
Future<Share> create(Account account, File file, String shareWith) => Future<Share> create(Account account, File file, String shareWith) =>
this.dataSrc.create(account, file, shareWith); this.dataSrc.create(account, file, shareWith);
@ -56,6 +64,9 @@ abstract class ShareDataSource {
/// List all shares from a given file /// List all shares from a given file
Future<List<Share>> list(Account account, File file); Future<List<Share>> list(Account account, File file);
/// List all shares from a given directory
Future<List<Share>> listDir(Account account, File dir);
/// Share a file/folder with a user /// Share a file/folder with a user
Future<Share> create(Account account, File file, String shareWith); Future<Share> create(Account account, File file, String shareWith);

View file

@ -15,16 +15,17 @@ class ShareRemoteDataSource implements ShareDataSource {
final response = await Api(account).ocs().filesSharing().shares().get( final response = await Api(account).ocs().filesSharing().shares().get(
path: file.strippedPath, path: file.strippedPath,
); );
if (!response.isGood) { return _onListResult(response);
_log.severe("[list] Failed requesting server: $response"); }
throw ApiException(
response: response,
message: "Failed communicating with server: ${response.statusCode}");
}
final json = jsonDecode(response.body); @override
final List<JsonObj> dataJson = json["ocs"]["data"].cast<JsonObj>(); listDir(Account account, File dir) async {
return _ShareParser().parseList(dataJson); _log.info("[listDir] ${dir.path}");
final response = await Api(account).ocs().filesSharing().shares().get(
path: dir.strippedPath,
subfiles: true,
);
return _onListResult(response);
} }
@override @override
@ -60,6 +61,19 @@ class ShareRemoteDataSource implements ShareDataSource {
} }
} }
List<Share> _onListResult(Response response) {
if (!response.isGood) {
_log.severe("[_onListResult] Failed requesting server: $response");
throw ApiException(
response: response,
message: "Failed communicating with server: ${response.statusCode}");
}
final json = jsonDecode(response.body);
final List<JsonObj> dataJson = json["ocs"]["data"].cast<JsonObj>();
return _ShareParser().parseList(dataJson);
}
static final _log = Logger("entity.share.data_source.ShareRemoteDataSource"); static final _log = Logger("entity.share.data_source.ShareRemoteDataSource");
} }
@ -79,6 +93,7 @@ class _ShareParser {
Share parseSingle(JsonObj json) { Share parseSingle(JsonObj json) {
return Share( return Share(
id: json["id"], id: json["id"],
path: json["path"],
shareType: json["share_type"], shareType: json["share_type"],
shareWith: json["share_with"], shareWith: json["share_with"],
shareWithDisplayName: json["share_with_displayname"], shareWithDisplayName: json["share_with_displayname"],