From 8ab3ca56330d82fda58ebc7bac2f86f694cb965f Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 29 Jul 2024 18:06:38 +0800 Subject: [PATCH] :arrow_up: Support latest Paperclip --- lib/providers/content/attachment.dart | 51 ------------------- lib/screens/account/personalize.dart | 6 +-- .../attachment_list_fullscreen.dart | 2 - .../attachments/attachment_publish.dart | 49 ++++-------------- 4 files changed, 11 insertions(+), 97 deletions(-) diff --git a/lib/providers/content/attachment.dart b/lib/providers/content/attachment.dart index 2c278f0..cf0acf6 100644 --- a/lib/providers/content/attachment.dart +++ b/lib/providers/content/attachment.dart @@ -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 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 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> 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> 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 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 createAttachment( Uint8List data, String path, - String hash, String usage, Map? 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), diff --git a/lib/screens/account/personalize.dart b/lib/screens/account/personalize.dart index b2e8eb4..5d2d736 100644 --- a/lib/screens/account/personalize.dart +++ b/lib/screens/account/personalize.dart @@ -86,15 +86,11 @@ class _PersonalizeScreenState extends State { 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); diff --git a/lib/widgets/attachments/attachment_list_fullscreen.dart b/lib/widgets/attachments/attachment_list_fullscreen.dart index e69116e..7dad122 100644 --- a/lib/widgets/attachments/attachment_list_fullscreen.dart +++ b/lib/widgets/attachments/attachment_list_fullscreen.dart @@ -60,11 +60,9 @@ class _AttachmentListFullScreenState extends State { DismissiblePageDismissDirection.multi: 0.05, }, onDragStart: () { - print('start dragging'); setState(() => _showDetails = false); }, onDragEnd: () { - print('stop dragging'); setState(() => _showDetails = true); }, child: GestureDetector( diff --git a/lib/widgets/attachments/attachment_publish.dart b/lib/widgets/attachments/attachment_publish.dart index 394a0b4..bc3b3b8 100644 --- a/lib/widgets/attachments/attachment_publish.dart +++ b/lib/widgets/attachments/attachment_publish.dart @@ -53,15 +53,11 @@ class _AttachmentPublishPopupState extends State { 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 { 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 { List 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 { setState(() => _isBusy = true); - Map 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 { 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 uploadAttachment(Uint8List data, String path, String hash, - Map? metadata) async { + Future uploadAttachment( + Uint8List data, String path, Map? metadata) async { final AttachmentProvider provider = Get.find(); try { context.showSnackbar((PlatformInfo.isWeb @@ -178,7 +157,6 @@ class _AttachmentPublishPopupState extends State { final resp = await provider.createAttachment( data, path, - hash, widget.usage, metadata, ); @@ -279,15 +257,7 @@ class _AttachmentPublishPopupState extends State { setState(() => _isBusy = true); for (final file in detail.files) { final data = await file.readAsBytes(); - final hash = await calculateBytesSha256(data); - - Map 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 { _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;