⬆️ Support latest Paperclip

This commit is contained in:
LittleSheep 2024-07-29 18:06:38 +08:00
parent 3db6850d89
commit 8ab3ca5633
4 changed files with 11 additions and 97 deletions

View File

@ -1,59 +1,11 @@
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'dart:typed_data';
import 'package:crypto/crypto.dart';
import 'package:get/get.dart';
import 'package:path/path.dart';
import 'package:solian/models/attachment.dart';
import 'package:solian/platform.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/services.dart';
import 'package:image/image.dart' as img;
Future<String> calculateBytesSha256(Uint8List data) async {
Digest digest;
if (PlatformInfo.isWeb) {
digest = sha256.convert(data);
} else {
digest = await Isolate.run(() => sha256.convert(data));
}
return digest.toString();
}
Future<String> calculateFileSha256(File file) async {
Uint8List bytes;
if (PlatformInfo.isWeb) {
bytes = await file.readAsBytes();
} else {
bytes = await Isolate.run(() => file.readAsBytesSync());
}
return await calculateBytesSha256(bytes);
}
Future<Map<String, dynamic>> calculateImageData(Uint8List data) async {
if (PlatformInfo.isWeb) {
return {};
}
final decoder = await Isolate.run(() => img.findDecoderForData(data));
if (decoder == null) return {};
final image = await Isolate.run(() => decoder.decode(data));
if (image == null) return {};
return {
'width': image.width,
'height': image.height,
'ratio': image.width / image.height
};
}
Future<Map<String, dynamic>> calculateImageMetaFromFile(File file) async {
if (PlatformInfo.isWeb) {
return {};
}
final bytes = await Isolate.run(() => file.readAsBytesSync());
return await calculateImageData(bytes);
}
class AttachmentProvider extends GetConnect {
static Map<String, String> mimetypeOverrides = {
@ -79,7 +31,6 @@ class AttachmentProvider extends GetConnect {
if (result.destination != 0 && result.isAnalyzed) {
_cachedResponses[id] = result;
}
print(result);
return result;
}
@ -89,7 +40,6 @@ class AttachmentProvider extends GetConnect {
Future<Response> createAttachment(
Uint8List data,
String path,
String hash,
String usage,
Map<String, dynamic>? metadata,
) async {
@ -117,7 +67,6 @@ class AttachmentProvider extends GetConnect {
final payload = FormData({
'alt': fileAlt,
'file': filePayload,
'hash': hash,
'usage': usage,
if (mimetypeOverride != null) 'mimetype': mimetypeOverride,
'metadata': jsonEncode(metadata),

View File

@ -86,15 +86,11 @@ class _PersonalizeScreenState extends State<PersonalizeScreen> {
Response? attachResp;
try {
final file = File(image.path);
final hash = await calculateFileSha256(file);
final meta = await calculateImageMetaFromFile(file);
attachResp = await provider.createAttachment(
await file.readAsBytes(),
file.path,
hash,
'p.$position',
{...meta},
null
);
} catch (e) {
setState(() => _isBusy = false);

View File

@ -60,11 +60,9 @@ class _AttachmentListFullScreenState extends State<AttachmentListFullScreen> {
DismissiblePageDismissDirection.multi: 0.05,
},
onDragStart: () {
print('start dragging');
setState(() => _showDetails = false);
},
onDragEnd: () {
print('stop dragging');
setState(() => _showDetails = true);
},
child: GestureDetector(

View File

@ -53,15 +53,11 @@ class _AttachmentPublishPopupState extends State<AttachmentPublishPopup> {
for (final media in medias) {
final file = File(media.path);
final hash = await calculateFileSha256(file);
final meta = await calculateImageMetaFromFile(file);
try {
await uploadAttachment(
await file.readAsBytes(),
file.path,
hash,
{...meta},
null,
);
} catch (err) {
context.showErrorDialog(err);
@ -81,12 +77,9 @@ class _AttachmentPublishPopupState extends State<AttachmentPublishPopup> {
setState(() => _isBusy = true);
final file = File(media.path);
final hash = await calculateFileSha256(file);
try {
await uploadAttachment(await file.readAsBytes(), file.path, hash, {
'ratio': 16 / 9,
});
await uploadAttachment(await file.readAsBytes(), file.path, null);
} catch (err) {
context.showErrorDialog(err);
}
@ -108,9 +101,8 @@ class _AttachmentPublishPopupState extends State<AttachmentPublishPopup> {
List<File> files = result.paths.map((path) => File(path!)).toList();
for (final file in files) {
final hash = await calculateFileSha256(file);
try {
await uploadAttachment(await file.readAsBytes(), file.path, hash, null);
await uploadAttachment(await file.readAsBytes(), file.path, null);
} catch (err) {
context.showErrorDialog(err);
}
@ -133,20 +125,9 @@ class _AttachmentPublishPopupState extends State<AttachmentPublishPopup> {
setState(() => _isBusy = true);
Map<String, dynamic> metadata;
final file = File(media.path);
final hash = await calculateFileSha256(file);
if (isVideo) {
metadata = {'ratio': 16 / 9};
} else {
metadata = await calculateImageMetaFromFile(file);
}
try {
await uploadAttachment(await file.readAsBytes(), file.path, hash, {
...metadata,
});
await uploadAttachment(await file.readAsBytes(), file.path, null);
} catch (err) {
context.showErrorDialog(err);
}
@ -160,15 +141,13 @@ class _AttachmentPublishPopupState extends State<AttachmentPublishPopup> {
setState(() => _isBusy = true);
final hash = await calculateBytesSha256(data);
final meta = await calculateImageData(data);
uploadAttachment(data, 'Pasted Image', hash, {...meta});
uploadAttachment(data, 'Pasted Image', null);
setState(() => _isBusy = false);
}
Future<void> uploadAttachment(Uint8List data, String path, String hash,
Map<String, dynamic>? metadata) async {
Future<void> uploadAttachment(
Uint8List data, String path, Map<String, dynamic>? metadata) async {
final AttachmentProvider provider = Get.find();
try {
context.showSnackbar((PlatformInfo.isWeb
@ -178,7 +157,6 @@ class _AttachmentPublishPopupState extends State<AttachmentPublishPopup> {
final resp = await provider.createAttachment(
data,
path,
hash,
widget.usage,
metadata,
);
@ -279,15 +257,7 @@ class _AttachmentPublishPopupState extends State<AttachmentPublishPopup> {
setState(() => _isBusy = true);
for (final file in detail.files) {
final data = await file.readAsBytes();
final hash = await calculateBytesSha256(data);
Map<String, dynamic> meta = {};
if (file.mimeType?.split('/').firstOrNull == 'image') {
meta = await calculateImageData(data);
}
uploadAttachment(data, file.path, hash, {...meta});
uploadAttachment(data, file.path, null);
}
setState(() => _isBusy = false);
},
@ -507,7 +477,8 @@ class _AttachmentEditorDialogState extends State<AttachmentEditorDialog> {
_isMature = widget.item.isMature;
_altController.text = widget.item.alt;
if (['image', 'video'].contains(widget.item.mimetype.split('/').firstOrNull)) {
if (['image', 'video']
.contains(widget.item.mimetype.split('/').firstOrNull)) {
_ratioController.text =
widget.item.metadata?['ratio']?.toString() ?? 1.toString();
_hasAspectRatio = true;