From 771eb37c7fdcfb34074c6fa3887ee880222e0bc8 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Tue, 17 Jun 2025 22:14:40 -0400 Subject: [PATCH] dev: add save file by moving from appdata --- src/gui/src/IPC.js | 37 ++++++++++++++++++++++++++++++++++ src/gui/src/helpers.js | 1 + src/puter-js/src/modules/UI.js | 11 +++++++--- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/gui/src/IPC.js b/src/gui/src/IPC.js index 453a784a..120bc463 100644 --- a/src/gui/src/IPC.js +++ b/src/gui/src/IPC.js @@ -1326,6 +1326,38 @@ const ipc_listener = async (event, handled) => { if ( written ) return true; $(el_filedialog_window).find('.window-disable-mask, .busy-indicator').hide(); }; + + const handle_move_save = async ({ source_path, target_path, el_filedialog_window }) => { + // source path must be in appdata directory + const stat_info = await puter.fs.stat(source_path); + if ( ! stat_info.appdata_app ) { + await puter.ui.alert(`the app ${app_uuid} attempted to ` + + `move data owned by the user illegaly`); + return; + } + + if ( stat_info.appdata_app !== app_uuid ) { + await puter.ui.alert(`the app ${app_uuid} attempted to ` + + `move data owned by ${stat_info.appdata_app}`); + return; + } + + console.log('supposedly we\'re writing this file now'); + + const written = await window.handle_same_name_exists({ + action: async ({ overwrite }) => { + if ( overwrite ) { + await puter.fs.delete(target_path); + } + console.log('performing move operation', {source_path, target_path}); + await puter.fs.move(source_path, target_path); + }, + parent_uuid: $(el_filedialog_window).attr('data-element_uuid'), + }); + + if ( written ) return true; + $(el_filedialog_window).find('.window-disable-mask, .busy-indicator').hide(); + }; await UIWindow({ path: '/' + window.user.username + '/Desktop', @@ -1348,6 +1380,11 @@ const ipc_listener = async (event, handled) => { if (event.data.url){ done = await handle_url_save({ target_path }); + } else if ( event.data.source_path ) { + done = await handle_move_save({ + source_path: event.data.source_path, + target_path, + }); } else { done = await handle_data_save({ target_path, el_filedialog_window }); } diff --git a/src/gui/src/helpers.js b/src/gui/src/helpers.js index 0a307a42..3f434bb2 100644 --- a/src/gui/src/helpers.js +++ b/src/gui/src/helpers.js @@ -2781,6 +2781,7 @@ window.handle_same_name_exists = async ({ }) => { try { await action({ overwrite: false }); + return true; } catch ( err ) { if ( err.code !== 'item_with_same_name_exists' ) { console.error(err); diff --git a/src/puter-js/src/modules/UI.js b/src/puter-js/src/modules/UI.js index 8f65899e..4e5ef2d7 100644 --- a/src/puter-js/src/modules/UI.js +++ b/src/puter-js/src/modules/UI.js @@ -713,17 +713,22 @@ class UI extends EventListener { }) } - showSaveFilePicker = function(content, suggestedName){ + showSaveFilePicker = function(content, suggestedName, type){ return new Promise((resolve) => { const msg_id = this.#messageID++; - const url = (Object.prototype.toString.call(content) === '[object URL]' ? content : undefined); + if ( ! type && Object.prototype.toString.call(content) === '[object URL]' ) { + type = 'url'; + } + const url = type === 'url' ? content.toString() : undefined; + const source_path = type === 'move' ? content : undefined; if(this.env === 'app'){ this.messageTarget?.postMessage({ msg: "showSaveFilePicker", appInstanceID: this.appInstanceID, content: url ? undefined : content, - url: url ? url.toString() : undefined, + url, + source_path, suggestedName: suggestedName ?? '', env: this.env, uuid: msg_id