2021-08-05 07:48:32 +02:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:nc_photos/account.dart';
|
|
|
|
import 'package:nc_photos/api/api.dart';
|
2022-10-15 16:29:18 +02:00
|
|
|
import 'package:nc_photos/entity/file_descriptor.dart';
|
2021-08-05 07:48:32 +02:00
|
|
|
import 'package:nc_photos/exception.dart';
|
2021-08-06 19:11:00 +02:00
|
|
|
import 'package:nc_photos/type.dart';
|
2021-08-05 07:48:32 +02:00
|
|
|
|
|
|
|
class RequestPublicLink {
|
|
|
|
/// Request a temporary unique public link to [file]
|
2022-10-15 16:29:18 +02:00
|
|
|
Future<String> call(Account account, FileDescriptor file) async {
|
2021-08-05 07:48:32 +02:00
|
|
|
final response =
|
2022-10-15 16:29:18 +02:00
|
|
|
await Api(account).ocs().dav().direct().post(fileId: file.fdId);
|
2021-08-05 07:48:32 +02:00
|
|
|
if (!response.isGood) {
|
|
|
|
_log.severe("[call] Failed requesting server: $response");
|
|
|
|
throw ApiException(
|
|
|
|
response: response,
|
2022-07-21 07:45:49 +02:00
|
|
|
message:
|
|
|
|
"Server responed with an error: HTTP ${response.statusCode}");
|
2021-08-05 07:48:32 +02:00
|
|
|
}
|
2021-08-06 19:11:00 +02:00
|
|
|
final JsonObj json = jsonDecode(response.body)["ocs"];
|
2021-08-05 07:48:32 +02:00
|
|
|
if (json["meta"]["statuscode"] != 200) {
|
|
|
|
_log.shout(
|
|
|
|
"[call] Failed requesting server: ${jsonEncode(json["meta"])}");
|
|
|
|
throw ApiException(
|
|
|
|
response: response,
|
|
|
|
message:
|
2022-07-21 07:45:49 +02:00
|
|
|
"Server responed with an error: HTTP ${json["meta"]["statuscode"]} ${json["meta"]["message"]}");
|
2021-08-05 07:48:32 +02:00
|
|
|
}
|
|
|
|
return json["data"]["url"];
|
|
|
|
}
|
|
|
|
|
|
|
|
static final _log = Logger("use_case.request_public_link.RequestPublicLink");
|
|
|
|
}
|