From 6b1e7b6ac8bd407c2076c8ea8aa5144e068e0786 Mon Sep 17 00:00:00 2001 From: "Nathan.fooo" <86001920+appflowy@users.noreply.github.com> Date: Tue, 25 Jun 2024 17:36:41 +0800 Subject: [PATCH] chore: set document id (#5585) * chore: set document id * feat: set document id to upload image api * chore: fmt --------- Co-authored-by: Lucas.Xu --- .../copy_and_paste/custom_paste_command.dart | 14 +++++++++++++- .../copy_and_paste/paste_from_image.dart | 8 ++++++-- .../header/document_header_node_widget.dart | 2 +- .../editor_plugins/image/image_placeholder.dart | 6 +++++- .../editor_plugins/image/image_util.dart | 4 ++-- .../widgets/auto_completion_node_widget.dart | 4 ++-- .../page_style/_page_style_cover_image.dart | 4 +++- .../page_style/page_style_bottom_sheet.dart | 2 +- .../tests/chat/ai_tool_test.rs | 4 +--- .../rust-lib/flowy-document/src/event_handler.rs | 12 ++++++------ 10 files changed, 40 insertions(+), 20 deletions(-) diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/custom_paste_command.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/custom_paste_command.dart index 842f00dbe9..0d04a2765f 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/custom_paste_command.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/custom_paste_command.dart @@ -1,3 +1,4 @@ +import 'package:appflowy/plugins/document/application/prelude.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/clipboard_service.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/editor_state_paste_node_extension.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_html.dart'; @@ -8,6 +9,7 @@ import 'package:appflowy/startup/startup.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:string_validator/string_validator.dart'; /// Paste. @@ -70,8 +72,18 @@ CommandShortcutEventHandler _pasteCommandHandler = (editorState) { } if (image != null && image.$2?.isNotEmpty == true) { + final documentBloc = + editorState.document.root.context?.read(); + final documentId = documentBloc?.documentId; + if (documentId == null || documentId.isEmpty) { + return; + } await editorState.deleteSelectionIfNeeded(); - final result = await editorState.pasteImage(image.$1, image.$2!); + final result = await editorState.pasteImage( + image.$1, + image.$2!, + documentId, + ); if (result) { return; } diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_image.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_image.dart index afa7ae5274..c6c8d2162c 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_image.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_image.dart @@ -21,7 +21,11 @@ extension PasteFromImage on EditorState { 'gif', ]; - Future pasteImage(String format, Uint8List imageBytes) async { + Future pasteImage( + String format, + Uint8List imageBytes, + String documentId, + ) async { if (!supportedImageFormats.contains(format)) { return false; } @@ -56,7 +60,7 @@ extension PasteFromImage on EditorState { if (isLocalMode) { path = await saveImageToLocalStorage(copyToPath); } else { - final result = await saveImageToCloudStorage(copyToPath); + final result = await saveImageToCloudStorage(copyToPath, documentId); final errorMessage = result.$2; diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/header/document_header_node_widget.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/header/document_header_node_widget.dart index 3ac9067b89..aa15ea7419 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/header/document_header_node_widget.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/header/document_header_node_widget.dart @@ -638,7 +638,7 @@ class DocumentCoverState extends State { details = await saveImageToLocalStorage(details); } else { // else we should save the image to cloud storage - (details, _) = await saveImageToCloudStorage(details); + (details, _) = await saveImageToCloudStorage(details, widget.view.id); } } widget.onChangeCover(type, details); diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/image_placeholder.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/image_placeholder.dart index d8370fe823..6fe7822e68 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/image_placeholder.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/image_placeholder.dart @@ -216,12 +216,16 @@ class ImagePlaceholderState extends State { // don't limit the image size for local mode. path = await saveImageToLocalStorage(url); } else { + final documentId = context.read().documentId; + if (documentId.isEmpty) { + return; + } // else we should save the image to cloud storage setState(() { showLoading = true; this.errorMessage = null; }); - (path, errorMessage) = await saveImageToCloudStorage(url); + (path, errorMessage) = await saveImageToCloudStorage(url, documentId); setState(() { showLoading = false; this.errorMessage = errorMessage; diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/image_util.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/image_util.dart index 0710ffccd1..8918ccb21e 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/image_util.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/image_util.dart @@ -39,6 +39,7 @@ Future saveImageToLocalStorage(String localImagePath) async { Future<(String? path, String? errorMessage)> saveImageToCloudStorage( String localImagePath, + String documentId, ) async { final size = localImagePath.fileSize; if (size == null || size > 10 * 1024 * 1024) { @@ -52,8 +53,7 @@ Future<(String? path, String? errorMessage)> saveImageToCloudStorage( Log.debug("Uploading image local path: $localImagePath"); final result = await documentService.uploadFile( localFilePath: localImagePath, - // TODO(lucas): replace with actual documentId - documentId: "temp", + documentId: documentId, ); return result.fold( (s) async { diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/openai/widgets/auto_completion_node_widget.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/openai/widgets/auto_completion_node_widget.dart index 6f07521858..41bfed3b45 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/openai/widgets/auto_completion_node_widget.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/openai/widgets/auto_completion_node_widget.dart @@ -288,8 +288,8 @@ class _AutoCompletionBlockComponentState return; } final textRobot = TextRobot(editorState: editorState); - final aiResposity = AppFlowyAIService(); - await aiResposity.streamCompletion( + final aiService = AppFlowyAIService(); + await aiService.streamCompletion( text: _rewritePrompt(previousOutput), completionType: CompletionTypePB.ContinueWriting, onStart: () async { diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/page_style/_page_style_cover_image.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/page_style/_page_style_cover_image.dart index fd0aa86fa7..c90e30ac34 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/page_style/_page_style_cover_image.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/page_style/_page_style_cover_image.dart @@ -31,8 +31,10 @@ import 'package:image_picker/image_picker.dart'; class PageStyleCoverImage extends StatelessWidget { PageStyleCoverImage({ super.key, + required this.documentId, }); + final String documentId; late final ImagePicker _imagePicker = ImagePicker(); @override @@ -230,7 +232,7 @@ class PageStyleCoverImage extends StatelessWidget { type = PageStyleCoverImageType.localImage; } else { // else we should save the image to cloud storage - (result, _) = await saveImageToCloudStorage(path); + (result, _) = await saveImageToCloudStorage(path, documentId); type = PageStyleCoverImageType.customImage; } if (!context.mounted) { diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/page_style/page_style_bottom_sheet.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/page_style/page_style_bottom_sheet.dart index 29dec9ad67..555881a38f 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/page_style/page_style_bottom_sheet.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/page_style/page_style_bottom_sheet.dart @@ -30,7 +30,7 @@ class PageStyleBottomSheet extends StatelessWidget { fontSize: 14.0, ), const VSpace(8.0), - PageStyleCoverImage(), + PageStyleCoverImage(documentId: view.id), const VSpace(20.0), // layout: font size, line height and font family. FlowyText( diff --git a/frontend/rust-lib/event-integration-test/tests/chat/ai_tool_test.rs b/frontend/rust-lib/event-integration-test/tests/chat/ai_tool_test.rs index 52e2d5a11a..3bef3a81b0 100644 --- a/frontend/rust-lib/event-integration-test/tests/chat/ai_tool_test.rs +++ b/frontend/rust-lib/event-integration-test/tests/chat/ai_tool_test.rs @@ -1,8 +1,6 @@ - use event_integration_test::user_event::user_localhost_af_cloud; use event_integration_test::EventIntegrationTest; -use flowy_chat::entities::{CompletionTypePB}; - +use flowy_chat::entities::CompletionTypePB; use std::time::Duration; diff --git a/frontend/rust-lib/flowy-document/src/event_handler.rs b/frontend/rust-lib/flowy-document/src/event_handler.rs index 0dca0d2ccb..2eabbf639a 100644 --- a/frontend/rust-lib/flowy-document/src/event_handler.rs +++ b/frontend/rust-lib/flowy-document/src/event_handler.rs @@ -420,11 +420,11 @@ pub(crate) async fn upload_file_handler( params: AFPluginData, manager: AFPluginState>, ) -> DataResult { - let AFPluginData(UploadFileParamsPB { + let UploadFileParamsPB { workspace_id, document_id, local_file_path, - }) = params; + } = params.try_into_inner()?; let manager = upgrade_document(manager)?; let url = manager @@ -442,10 +442,10 @@ pub(crate) async fn download_file_handler( params: AFPluginData, manager: AFPluginState>, ) -> FlowyResult<()> { - let AFPluginData(UploadedFilePB { + let UploadedFilePB { url, local_file_path, - }) = params; + } = params.try_into_inner()?; let manager = upgrade_document(manager)?; manager.download_file(local_file_path, url).await @@ -456,10 +456,10 @@ pub(crate) async fn delete_file_handler( params: AFPluginData, manager: AFPluginState>, ) -> FlowyResult<()> { - let AFPluginData(UploadedFilePB { + let UploadedFilePB { url, local_file_path, - }) = params; + } = params.try_into_inner()?; let manager = upgrade_document(manager)?; manager.delete_file(local_file_path, url).await }