2020-04-16 10:06:01 +00:00
|
|
|
import 'package:comunic/models/api_request.dart';
|
|
|
|
|
|
|
|
/// Virtual directory helper
|
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
|
|
|
|
enum VirtualDirectoryType { USER, GROUP, NONE }
|
|
|
|
|
|
|
|
class VirtualDirectoryResult {
|
|
|
|
final VirtualDirectoryType type;
|
2022-03-10 18:39:57 +00:00
|
|
|
final int? id;
|
2020-04-16 10:06:01 +00:00
|
|
|
|
|
|
|
const VirtualDirectoryResult({
|
2022-03-10 18:39:57 +00:00
|
|
|
required this.type,
|
2020-04-16 10:06:01 +00:00
|
|
|
this.id,
|
2022-03-11 15:27:01 +00:00
|
|
|
});
|
2020-04-16 10:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class VirtualDirectoryHelper {
|
|
|
|
/// Find a virtual directory
|
|
|
|
Future<VirtualDirectoryResult> find(String directory) async {
|
|
|
|
final response = await APIRequest(
|
|
|
|
uri: "virtualDirectory/find",
|
|
|
|
needLogin: true,
|
|
|
|
args: {"directory": directory}).exec();
|
|
|
|
|
|
|
|
switch (response.code) {
|
|
|
|
case 404:
|
|
|
|
return VirtualDirectoryResult(type: VirtualDirectoryType.NONE);
|
|
|
|
|
|
|
|
case 200:
|
2022-03-10 19:36:55 +00:00
|
|
|
final id = response.getObject()["id"];
|
|
|
|
final kind = response.getObject()["kind"];
|
2020-04-16 10:06:01 +00:00
|
|
|
switch (kind) {
|
|
|
|
case "user":
|
|
|
|
return VirtualDirectoryResult(
|
|
|
|
type: VirtualDirectoryType.USER, id: id);
|
|
|
|
case "group":
|
|
|
|
return VirtualDirectoryResult(
|
|
|
|
type: VirtualDirectoryType.GROUP, id: id);
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw Exception("Unsupported virtual directory kind: $kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Exception("Could not get virtual directory!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|