2024-05-22 15:18:01 +00:00
|
|
|
import 'dart:convert';
|
2024-06-29 12:25:29 +00:00
|
|
|
import 'dart:typed_data';
|
2024-05-22 15:18:01 +00:00
|
|
|
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:path/path.dart';
|
2024-07-29 09:56:36 +00:00
|
|
|
import 'package:solian/models/attachment.dart';
|
2024-05-22 15:18:01 +00:00
|
|
|
import 'package:solian/providers/auth.dart';
|
|
|
|
import 'package:solian/services.dart';
|
2024-08-02 07:49:32 +00:00
|
|
|
import 'package:dio/dio.dart' as dio;
|
2024-06-30 09:43:36 +00:00
|
|
|
|
2024-05-22 15:18:01 +00:00
|
|
|
class AttachmentProvider extends GetConnect {
|
2024-06-29 12:25:29 +00:00
|
|
|
static Map<String, String> mimetypeOverrides = {
|
|
|
|
'mov': 'video/quicktime',
|
|
|
|
'mp4': 'video/mp4'
|
|
|
|
};
|
2024-06-01 13:39:28 +00:00
|
|
|
|
2024-05-22 15:18:01 +00:00
|
|
|
@override
|
|
|
|
void onInit() {
|
2024-07-16 11:46:53 +00:00
|
|
|
httpClient.baseUrl = ServiceFinder.buildUrl('files', null);
|
2024-05-22 15:18:01 +00:00
|
|
|
}
|
|
|
|
|
2024-07-29 09:56:36 +00:00
|
|
|
final Map<int, Attachment> _cachedResponses = {};
|
2024-05-27 15:07:01 +00:00
|
|
|
|
2024-07-29 09:56:36 +00:00
|
|
|
Future<Attachment?> getMetadata(int id, {noCache = false}) async {
|
2024-06-01 13:39:28 +00:00
|
|
|
if (!noCache && _cachedResponses.containsKey(id)) {
|
|
|
|
return _cachedResponses[id]!;
|
|
|
|
}
|
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
final resp = await get('/attachments/$id/meta');
|
2024-07-29 09:56:36 +00:00
|
|
|
if (resp.statusCode == 200) {
|
|
|
|
final result = Attachment.fromJson(resp.body);
|
|
|
|
if (result.destination != 0 && result.isAnalyzed) {
|
|
|
|
_cachedResponses[id] = result;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2024-06-01 13:39:28 +00:00
|
|
|
|
2024-07-29 09:56:36 +00:00
|
|
|
return null;
|
2024-06-01 13:39:28 +00:00
|
|
|
}
|
2024-05-22 15:18:01 +00:00
|
|
|
|
2024-08-02 07:49:32 +00:00
|
|
|
Future<Attachment> createAttachment(
|
2024-08-01 14:13:08 +00:00
|
|
|
Uint8List data, String path, String usage, Map<String, dynamic>? metadata,
|
|
|
|
{Function(double)? onProgress}) async {
|
2024-05-22 15:18:01 +00:00
|
|
|
final AuthProvider auth = Get.find();
|
2024-07-24 17:18:47 +00:00
|
|
|
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
2024-05-22 15:18:01 +00:00
|
|
|
|
2024-08-02 07:49:32 +00:00
|
|
|
final filePayload =
|
|
|
|
dio.MultipartFile.fromBytes(data, filename: basename(path));
|
2024-06-29 12:25:29 +00:00
|
|
|
final fileAlt = basename(path).contains('.')
|
|
|
|
? basename(path).substring(0, basename(path).lastIndexOf('.'))
|
|
|
|
: basename(path);
|
|
|
|
final fileExt = basename(path)
|
|
|
|
.substring(basename(path).lastIndexOf('.') + 1)
|
2024-05-27 15:07:01 +00:00
|
|
|
.toLowerCase();
|
|
|
|
|
|
|
|
// Override for some files cannot be detected mimetype by server-side
|
|
|
|
String? mimetypeOverride;
|
|
|
|
if (mimetypeOverrides.keys.contains(fileExt)) {
|
|
|
|
mimetypeOverride = mimetypeOverrides[fileExt];
|
|
|
|
}
|
2024-08-02 07:49:32 +00:00
|
|
|
final payload = dio.FormData.fromMap({
|
2024-06-03 15:36:46 +00:00
|
|
|
'alt': fileAlt,
|
|
|
|
'file': filePayload,
|
|
|
|
'usage': usage,
|
|
|
|
if (mimetypeOverride != null) 'mimetype': mimetypeOverride,
|
2024-07-19 15:56:59 +00:00
|
|
|
'metadata': jsonEncode(metadata),
|
2024-06-03 15:36:46 +00:00
|
|
|
});
|
2024-08-02 07:49:32 +00:00
|
|
|
final resp = await dio.Dio(
|
|
|
|
dio.BaseOptions(
|
|
|
|
baseUrl: ServiceFinder.buildUrl('files', null),
|
|
|
|
headers: {'Authorization': 'Bearer ${auth.credentials!.accessToken}'},
|
|
|
|
),
|
|
|
|
).post(
|
2024-08-01 14:13:08 +00:00
|
|
|
'/attachments',
|
2024-08-02 07:49:32 +00:00
|
|
|
data: payload,
|
|
|
|
onSendProgress: (count, total) {
|
|
|
|
if (onProgress != null) onProgress(count / total);
|
2024-08-01 14:13:08 +00:00
|
|
|
},
|
|
|
|
);
|
2024-05-27 15:07:01 +00:00
|
|
|
if (resp.statusCode != 200) {
|
2024-08-02 07:49:32 +00:00
|
|
|
throw Exception(resp.data);
|
2024-05-22 15:18:01 +00:00
|
|
|
}
|
|
|
|
|
2024-08-02 07:49:32 +00:00
|
|
|
return Attachment.fromJson(resp.data);
|
2024-05-22 15:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Response> updateAttachment(
|
|
|
|
int id,
|
|
|
|
String alt,
|
|
|
|
String usage, {
|
|
|
|
bool isMature = false,
|
|
|
|
}) async {
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-07-24 17:18:47 +00:00
|
|
|
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
2024-05-22 15:18:01 +00:00
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
final client = auth.configureClient('files');
|
2024-05-22 15:18:01 +00:00
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
var resp = await client.put('/attachments/$id', {
|
2024-05-22 15:18:01 +00:00
|
|
|
'alt': alt,
|
|
|
|
'usage': usage,
|
|
|
|
'is_mature': isMature,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (resp.statusCode != 200) {
|
|
|
|
throw Exception(resp.bodyString);
|
|
|
|
}
|
|
|
|
|
2024-05-27 15:07:01 +00:00
|
|
|
return resp;
|
2024-05-22 15:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Response> deleteAttachment(int id) async {
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-07-24 17:18:47 +00:00
|
|
|
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
2024-05-22 15:18:01 +00:00
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
final client = auth.configureClient('files');
|
2024-05-22 15:18:01 +00:00
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
var resp = await client.delete('/attachments/$id');
|
2024-05-22 15:18:01 +00:00
|
|
|
if (resp.statusCode != 200) {
|
|
|
|
throw Exception(resp.bodyString);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
}
|
2024-06-01 13:39:28 +00:00
|
|
|
|
|
|
|
void clearCache({int? id}) {
|
|
|
|
if (id != null) {
|
|
|
|
_cachedResponses.remove(id);
|
|
|
|
} else {
|
|
|
|
_cachedResponses.clear();
|
|
|
|
}
|
|
|
|
}
|
2024-05-22 15:18:01 +00:00
|
|
|
}
|