Solian/lib/providers/content/attachment.dart

140 lines
3.9 KiB
Dart
Raw Normal View History

2024-05-22 15:18:01 +00:00
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'package:crypto/crypto.dart';
import 'package:get/get.dart';
import 'package:path/path.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/services.dart';
import 'package:image/image.dart' as img;
Future<String> calculateFileSha256(File file) async {
final bytes = await Isolate.run(() => file.readAsBytesSync());
final digest = await Isolate.run(() => sha256.convert(bytes));
return digest.toString();
}
Future<double> calculateFileAspectRatio(File file) async {
final bytes = await Isolate.run(() => file.readAsBytesSync());
final decoder = await Isolate.run(() => img.findDecoderForData(bytes));
if (decoder == null) return 1;
final image = await Isolate.run(() => decoder.decode(bytes));
if (image == null) return 1;
return image.width / image.height;
}
class AttachmentProvider extends GetConnect {
2024-06-01 13:39:28 +00:00
static Map<String, String> mimetypeOverrides = {'mov': 'video/quicktime'};
2024-05-22 15:18:01 +00:00
@override
void onInit() {
httpClient.baseUrl = ServiceFinder.services['paperclip'];
}
2024-06-01 13:39:28 +00:00
final Map<int, Response> _cachedResponses = {};
2024-05-27 15:07:01 +00:00
2024-06-01 13:39:28 +00:00
Future<Response> getMetadata(int id, {noCache = false}) async {
if (!noCache && _cachedResponses.containsKey(id)) {
return _cachedResponses[id]!;
}
final resp = await get('/api/attachments/$id/meta');
_cachedResponses[id] = resp;
return resp;
}
2024-05-22 15:18:01 +00:00
Future<Response> createAttachment(File file, String hash, String usage,
{double? ratio}) async {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
final client = auth.configureClient(
service: 'paperclip',
2024-06-03 15:36:46 +00:00
timeout: const Duration(minutes: 3),
);
2024-05-22 15:18:01 +00:00
final filePayload =
MultipartFile(await file.readAsBytes(), filename: basename(file.path));
final fileAlt = basename(file.path).contains('.')
? basename(file.path).substring(0, basename(file.path).lastIndexOf('.'))
: basename(file.path);
2024-05-27 15:07:01 +00:00
final fileExt = basename(file.path)
.substring(basename(file.path).lastIndexOf('.') + 1)
.toLowerCase();
// Override for some files cannot be detected mimetype by server-side
String? mimetypeOverride;
if (mimetypeOverrides.keys.contains(fileExt)) {
mimetypeOverride = mimetypeOverrides[fileExt];
}
2024-06-03 15:36:46 +00:00
final payload = FormData({
'alt': fileAlt,
'file': filePayload,
'hash': hash,
'usage': usage,
if (mimetypeOverride != null) 'mimetype': mimetypeOverride,
'metadata': jsonEncode({
if (ratio != null) 'ratio': ratio,
2024-05-22 15:18:01 +00:00
}),
2024-06-03 15:36:46 +00:00
});
final resp = await client.post('/api/attachments', payload);
2024-05-27 15:07:01 +00:00
if (resp.statusCode != 200) {
2024-06-03 15:36:46 +00:00
throw Exception(resp.bodyString);
2024-05-22 15:18:01 +00:00
}
2024-05-27 15:07:01 +00:00
return resp;
2024-05-22 15:18:01 +00:00
}
Future<Response> updateAttachment(
int id,
String alt,
String usage, {
double? ratio,
bool isMature = false,
}) async {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
final client = auth.configureClient(service: 'paperclip');
2024-05-22 15:18:01 +00:00
var resp = await client.put('/api/attachments/$id', {
'metadata': {
if (ratio != null) 'ratio': ratio,
},
'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();
if (!await auth.isAuthorized) throw Exception('unauthorized');
final client = auth.configureClient(service: 'paperclip');
2024-05-22 15:18:01 +00:00
var resp = await client.delete('/api/attachments/$id');
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
}