From a63953178980e2a7e85bb33398112cfbb181a54e Mon Sep 17 00:00:00 2001 From: ninjadev64 <63245705+ninjadev64@users.noreply.github.com> Date: Wed, 11 Dec 2024 20:29:10 +0000 Subject: [PATCH] Implement installing plugins from local files (#33) --- src-tauri/src/events/frontend/plugins.rs | 25 +++++++++++++------- src/components/PluginManager.svelte | 30 ++++++++++++++++++++---- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src-tauri/src/events/frontend/plugins.rs b/src-tauri/src/events/frontend/plugins.rs index e5cec3f..1fee975 100644 --- a/src-tauri/src/events/frontend/plugins.rs +++ b/src-tauri/src/events/frontend/plugins.rs @@ -46,14 +46,23 @@ pub async fn list_plugins() -> Result, Error> { } #[command] -pub async fn install_plugin(app: AppHandle, id: String, url: Option) -> Result<(), Error> { - let resp = match reqwest::get(url.unwrap_or(format!("https://plugins.amankhanna.me/rezipped/{id}.zip"))).await { - Ok(resp) => resp, - Err(error) => return Err(anyhow::Error::from(error).into()), - }; - let bytes = match resp.bytes().await { - Ok(bytes) => bytes, - Err(error) => return Err(anyhow::Error::from(error).into()), +pub async fn install_plugin(app: AppHandle, id: String, url: Option, file: Option) -> Result<(), Error> { + let bytes = match file { + None => { + let resp = match reqwest::get(url.unwrap_or(format!("https://plugins.amankhanna.me/rezipped/{id}.zip"))).await { + Ok(resp) => resp, + Err(error) => return Err(anyhow::Error::from(error).into()), + }; + use std::ops::Deref; + match resp.bytes().await { + Ok(bytes) => bytes.deref().to_owned(), + Err(error) => return Err(anyhow::Error::from(error).into()), + } + } + Some(path) => match std::fs::read(path) { + Ok(bytes) => bytes, + Err(error) => return Err(anyhow::Error::from(error).into()), + }, }; let _ = crate::plugins::deactivate_plugin(&app, &format!("{}.sdPlugin", id)).await; diff --git a/src/components/PluginManager.svelte b/src/components/PluginManager.svelte index 66951b1..80bdc8a 100644 --- a/src/components/PluginManager.svelte +++ b/src/components/PluginManager.svelte @@ -4,6 +4,7 @@ import ArrowClockwise from "phosphor-svelte/lib/ArrowClockwise"; import CloudArrowDown from "phosphor-svelte/lib/CloudArrowDown"; + import FileArrowUp from "phosphor-svelte/lib/FileArrowUp"; import Trash from "phosphor-svelte/lib/Trash"; import ListedPlugin from "./ListedPlugin.svelte"; import Popup from "./Popup.svelte"; @@ -13,17 +14,17 @@ import { invoke } from "@tauri-apps/api/core"; import { onOpenUrl } from "@tauri-apps/plugin-deep-link"; - import { ask, message } from "@tauri-apps/plugin-dialog"; + import { ask, message, open } from "@tauri-apps/plugin-dialog"; export let actionList: () => ActionList; export let deviceSelector: () => DeviceSelector; let showPopup: boolean; - async function installPlugin(id: string, name: string, url: string | undefined = undefined) { - if (!await ask(`Install "${name}"? It may take a while to download the plugin.`, { title: `Install "${name}"?` })) return; + async function installPlugin(id: string, name: string, url: string | null = null, file: string | null = null) { + if (!file && !await ask(`Install "${name}"? It may take a while to download the plugin.`, { title: `Install "${name}"?` })) return; try { - await invoke("install_plugin", { id, url }); + await invoke("install_plugin", { id, url, file }); message(`Successfully installed "${name}".`, { title: `Installed "${name}"` }); actionList().reload(); installed = await invoke("list_plugins"); @@ -83,6 +84,16 @@ await installPlugin(plugin.id, plugin.name); } + async function installPluginFile() { + const path = await open({ multiple: false, directory: false }); + const id = prompt("Plugin ID:"); + if (!id || id.split(".").length < 3) { + message("Failed to install plugin from file: invalid plugin ID", { title: "Failed to install" }); + return; + } + installPlugin(id, id, null, path); + } + async function removePlugin(plugin: any) { if (!await ask(`Are you sure you want to remove "${plugin.name}"?`, { title: `Remove "${plugin.name}"?` })) return; try { @@ -156,7 +167,16 @@ {/each} -

Plugin store

+
+

Plugin store

+ +