From 9afa5b920fd036bf09aa5a164df8959f6fae8755 Mon Sep 17 00:00:00 2001 From: Weixuan Fu Date: Sun, 16 Jun 2024 01:23:42 +0800 Subject: [PATCH] add try catch around send2trash, change all nodecafe.org to nodecafe.co --- __init__.py | 14 ++++++-- service/media_service.py | 2 +- service/twoway_sync_folder_service.py | 11 ++++-- ui/src/db-tables/UserSettingsTable.ts | 2 +- ui/src/settings/CloudHostSetting.tsx | 48 +++++++++++---------------- 5 files changed, 42 insertions(+), 35 deletions(-) diff --git a/__init__.py b/__init__.py index e66c866..1bdab8f 100644 --- a/__init__.py +++ b/__init__.py @@ -13,7 +13,10 @@ from .service.media_service import * from .service.file_sync_service import * from .service.db_service import get_my_workflows_dir from .service.node_service import * -from send2trash import send2trash +try: + from send2trash import send2trash +except ImportError: + send2trash = None WEB_DIRECTORY = "entry" NODE_CLASS_MAPPINGS = {} @@ -120,8 +123,13 @@ async def delete_file(request): full_path = os.path.join(my_workflows_dir, file_path) if os.path.exists(full_path): - send2trash(full_path) - "File deleted successfully" + if send2trash: + send2trash(full_path) + else: + os.remove(full_path) + print("❌⛔️send2trash is not available. Deleting file permanently. Please `pip install send2trash`") + + return "File deleted successfully" else: return "File not found" diff --git a/service/media_service.py b/service/media_service.py index 5cfe256..6281380 100644 --- a/service/media_service.py +++ b/service/media_service.py @@ -9,7 +9,7 @@ from .model_manager.model_preview import get_thumbnail_for_image_file image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp'] video_extensions = ['.mp4', '.mov', '.avi', '.webm', '.mkv'] -API_URL = 'https://www.nodecafe.org/api/image/upload' +API_URL = 'https://www.nodecafe.co/api/image/upload' def view_media(filename, isPreview = False, isInput = False): if not filename: return web.Response(status=404) diff --git a/service/twoway_sync_folder_service.py b/service/twoway_sync_folder_service.py index e6b539c..e14a81a 100644 --- a/service/twoway_sync_folder_service.py +++ b/service/twoway_sync_folder_service.py @@ -3,9 +3,12 @@ import shutil from aiohttp import web import os from pathlib import Path -from send2trash import send2trash import server from .db_service import get_my_workflows_dir +try: + from send2trash import send2trash +except ImportError: + send2trash = None @server.PromptServer.instance.routes.post('/workspace/folder/create') async def create_folder(request): @@ -32,7 +35,11 @@ def delete_folder_sync(reqJson): folder_path = reqJson.get('path') folder_path = os.path.join(get_my_workflows_dir(), folder_path) try: - send2trash(folder_path) + if send2trash: + send2trash(folder_path) + else: + shutil.rmtree(folder_path) + print("❌⛔️send2trash is not available. Deleting file permanently. Please `pip install send2trash`") return {"success": True} except Exception as e: return {"success": False, "error": str(e)} diff --git a/ui/src/db-tables/UserSettingsTable.ts b/ui/src/db-tables/UserSettingsTable.ts index 1999f9c..d186a01 100644 --- a/ui/src/db-tables/UserSettingsTable.ts +++ b/ui/src/db-tables/UserSettingsTable.ts @@ -43,7 +43,7 @@ export class UserSettingsTable extends TableBase { autoSaveDuration: 3, twoWaySync: false, foldersOnTop: false, - cloudHost: "https://www.nodecafe.org", + cloudHost: "https://www.nodecafe.co", overwriteCurWorkflowWhenDroppingFileToCanvas: false, maximumChangelogNumber: 80, hideCoverImage: false, diff --git a/ui/src/settings/CloudHostSetting.tsx b/ui/src/settings/CloudHostSetting.tsx index dad3d85..96d1a13 100644 --- a/ui/src/settings/CloudHostSetting.tsx +++ b/ui/src/settings/CloudHostSetting.tsx @@ -1,27 +1,29 @@ -import { - Input, - Stack, - useToast, - Text, - HStack, - Flex, - Button, -} from "@chakra-ui/react"; -import { useEffect, useRef } from "react"; +import { Input, Stack, useToast, Text, Flex, Button } from "@chakra-ui/react"; +import { useEffect, useState } from "react"; import { userSettingsTable } from "../db-tables/WorkspaceDB"; export default function CloudHostSetting() { - const inputRef = useRef(null); const toast = useToast(); + const [text, setText] = useState(""); + const getSettings = () => { userSettingsTable?.getSetting("cloudHost").then((res) => { - inputRef.current?.setAttribute("value", res ?? ""); + setText(res ?? ""); }); }; useEffect(() => { getSettings(); }, []); + const submitChange = async (text: string) => { + await userSettingsTable?.upsert({ cloudHost: text }); + getSettings(); + toast({ + title: "Setting saved. Please refresh to see the changes.", + status: "success", + duration: 4000, + }); + }; return ( @@ -30,26 +32,16 @@ export default function CloudHostSetting() { { - console.log("onBlur", inputRef.current?.value); - userSettingsTable?.upsert({ cloudHost: inputRef.current?.value }); - getSettings(); - toast({ - title: "Setting saved. Please refresh to see the changes.", - status: "success", - duration: 4000, - }); - }} + value={text} + onChange={(e) => setText(e.target.value)} + onBlur={() => submitChange(text)} />