nc-photos/app/lib/entity/share.dart

229 lines
5.5 KiB
Dart
Raw Normal View History

2021-08-07 22:34:44 +02:00
import 'package:equatable/equatable.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file_descriptor.dart';
2023-08-25 18:37:17 +02:00
import 'package:np_string/np_string.dart';
2022-01-31 10:47:37 +01:00
import 'package:path/path.dart' as path_lib;
2022-12-08 16:39:13 +01:00
import 'package:to_string/to_string.dart';
part 'share.g.dart';
2021-08-07 22:34:44 +02:00
enum ShareType {
user,
group,
publicLink,
email,
federatedCloudShare,
circle,
talk,
}
extension ShareTypeExtension on ShareType {
static ShareType fromValue(int shareTypeVal) {
switch (shareTypeVal) {
case 0:
return ShareType.user;
case 1:
return ShareType.group;
case 3:
return ShareType.publicLink;
case 4:
return ShareType.email;
case 6:
return ShareType.federatedCloudShare;
case 7:
return ShareType.circle;
case 10:
return ShareType.talk;
default:
throw ArgumentError("Invalid shareType: $shareTypeVal");
}
}
int toValue() {
switch (this) {
case ShareType.user:
return 0;
case ShareType.group:
return 1;
case ShareType.publicLink:
return 3;
case ShareType.email:
return 4;
case ShareType.federatedCloudShare:
return 6;
case ShareType.circle:
return 7;
case ShareType.talk:
return 10;
}
}
}
2021-10-06 22:32:36 +02:00
enum ShareItemType {
file,
folder,
}
extension ShareItemTypeExtension on ShareItemType {
static ShareItemType fromValue(String itemTypeVal) {
switch (itemTypeVal) {
case "file":
return ShareItemType.file;
case "folder":
return ShareItemType.folder;
default:
throw ArgumentError("Invalid itemType: $itemTypeVal");
}
}
String toValue() {
switch (this) {
case ShareItemType.file:
return "file";
case ShareItemType.folder:
return "folder";
}
}
}
2022-12-08 16:39:13 +01:00
@toString
2021-08-07 22:34:44 +02:00
class Share with EquatableMixin {
Share({
required this.id,
required this.shareType,
2021-10-06 22:32:36 +02:00
required this.stime,
2021-10-11 19:03:37 +02:00
required this.uidOwner,
required this.displaynameOwner,
required this.uidFileOwner,
2021-10-08 17:14:24 +02:00
required String path,
2021-10-06 22:32:36 +02:00
required this.itemType,
required this.mimeType,
required this.itemSource,
2021-08-07 22:34:44 +02:00
required this.shareWith,
required this.shareWithDisplayName,
this.url,
2021-10-08 17:14:24 +02:00
}) : path = path.trimAny("/");
2021-08-07 22:34:44 +02:00
@override
2022-12-08 16:39:13 +01:00
String toString() => _$toString();
2021-08-07 22:34:44 +02:00
@override
get props => [
id,
shareType,
2021-10-06 22:32:36 +02:00
stime,
2021-10-11 19:03:37 +02:00
uidOwner,
displaynameOwner,
uidFileOwner,
2021-10-06 22:32:36 +02:00
path,
itemType,
mimeType,
itemSource,
2021-08-07 22:34:44 +02:00
shareWith,
shareWithDisplayName,
url,
2021-08-07 22:34:44 +02:00
];
2021-10-06 22:32:36 +02:00
// see: https://doc.owncloud.com/server/latest/developer_manual/core/apis/ocs-share-api.html#response-attributes-2
2021-08-07 22:34:44 +02:00
final String id;
final ShareType shareType;
2021-10-06 22:32:36 +02:00
final DateTime stime;
2021-11-12 22:13:02 +01:00
final CiString uidOwner;
2021-10-11 19:03:37 +02:00
final String displaynameOwner;
final CiString uidFileOwner;
2021-10-06 22:32:36 +02:00
final String path;
final ShareItemType itemType;
final String mimeType;
final int itemSource;
2021-11-12 22:13:02 +01:00
final CiString? shareWith;
2021-08-07 22:34:44 +02:00
final String shareWithDisplayName;
final String? url;
2021-08-07 22:34:44 +02:00
}
2021-10-06 22:32:36 +02:00
extension ShareExtension on Share {
2022-01-31 10:47:37 +01:00
String get filename => path_lib.basename(path);
2021-10-06 22:32:36 +02:00
}
2021-08-07 22:34:44 +02:00
class ShareRepo {
ShareRepo(this.dataSrc);
/// See [ShareDataSource.list]
Future<List<Share>> list(
Account account,
FileDescriptor file, {
bool? isIncludeReshare,
}) =>
dataSrc.list(account, file, isIncludeReshare: isIncludeReshare);
2021-08-07 22:34:44 +02:00
2021-08-13 12:42:31 +02:00
/// See [ShareDataSource.listDir]
Future<List<Share>> listDir(Account account, File dir) =>
2021-09-15 08:58:06 +02:00
dataSrc.listDir(account, dir);
2021-08-13 12:42:31 +02:00
2021-10-06 22:32:36 +02:00
/// See [ShareDataSource.listAll]
Future<List<Share>> listAll(Account account) => dataSrc.listAll(account);
/// See [ShareDataSource.reverseList]
Future<List<Share>> reverseList(Account account, File file) =>
dataSrc.reverseList(account, file);
2021-10-11 19:03:37 +02:00
/// See [ShareDataSource.reverseListAll]
Future<List<Share>> reverseListAll(Account account) =>
dataSrc.reverseListAll(account);
2021-08-07 22:34:44 +02:00
/// See [ShareDataSource.create]
Future<Share> create(
Account account, FileDescriptor file, String shareWith) =>
2021-09-15 08:58:06 +02:00
dataSrc.create(account, file, shareWith);
2021-08-07 22:34:44 +02:00
/// See [ShareDataSource.createLink]
Future<Share> createLink(
Account account,
File file, {
String? password,
}) =>
dataSrc.createLink(account, file, password: password);
2021-08-07 22:34:44 +02:00
/// See [ShareDataSource.delete]
Future<void> delete(Account account, Share share) =>
2021-09-15 08:58:06 +02:00
dataSrc.delete(account, share);
2021-08-07 22:34:44 +02:00
final ShareDataSource dataSrc;
}
abstract class ShareDataSource {
/// List all shares from a given file
Future<List<Share>> list(
Account account,
FileDescriptor file, {
bool? isIncludeReshare,
});
2021-08-07 22:34:44 +02:00
2021-08-13 12:42:31 +02:00
/// List all shares from a given directory
Future<List<Share>> listDir(Account account, File dir);
2021-10-06 22:32:36 +02:00
/// List all shares from a given user
Future<List<Share>> listAll(Account account);
/// List all shares by other users from a given file
Future<List<Share>> reverseList(Account account, File file);
2021-10-11 19:03:37 +02:00
/// List all shares by other users with a given user
Future<List<Share>> reverseListAll(Account account);
2021-08-07 22:34:44 +02:00
/// Share a file/folder with a user
Future<Share> create(Account account, FileDescriptor file, String shareWith);
2021-08-07 22:34:44 +02:00
/// Share a file/folder with a share link
///
/// If [password] is not null, the share link will be password protected
Future<Share> createLink(
Account account,
File file, {
String? password,
});
2021-08-07 22:34:44 +02:00
/// Remove the given share
Future<void> delete(Account account, Share share);
}