From 1918fd6c3024e7b0d43ca2b0ce549b3dca5ad247 Mon Sep 17 00:00:00 2001 From: Simon Le Marchant Date: Thu, 29 Sep 2022 12:50:02 -0400 Subject: [PATCH] v1.2.5 - Fixes for user data path location, and by extension, the image upload control type. --- package.json | 2 +- src/background.js | 9 ++++++--- src/components/Control.vue | 23 +++++++++++++---------- src/components/ControlForm.vue | 2 +- src/preload.js | 26 ++++++++++++++------------ 5 files changed, 35 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index 5b14c77..d2dbe1d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mit-illuminations", - "version": "v1.2.4", + "version": "v1.2.5", "private": true, "description": "Illuminations by MIT - Turn P5 Code into Light Shows", "author": "Sosolimited in collaboration with MIT", diff --git a/src/background.js b/src/background.js index 8e79ec9..0291411 100644 --- a/src/background.js +++ b/src/background.js @@ -48,17 +48,20 @@ function createWindow() { nodeIntegration: true, contextIsolation: false, webSecurity: false, - preload: path.join(__dirname, 'preload.js') + preload: path.join(__dirname, 'preload.js'), + devTools: true, + additionalArguments: [userDataPath] } }); if (process.env.WEBPACK_DEV_SERVER_URL) { splash.loadURL(`file://${__dirname}/bundled/splash.html`).catch(console.log); - win.loadURL(`${process.env.WEBPACK_DEV_SERVER_URL}?userDataPath=${encodeURIComponent(userDataPath)}`).catch(console.log); + win.loadURL(`${process.env.WEBPACK_DEV_SERVER_URL}`).catch(console.log); + win.webContents.openDevTools(); } else { createProtocol('app'); splash.loadURL(`app://./splash.html`).catch(console.log); - win.loadURL(`app://./index.html?userDataPath=${encodeURIComponent(userDataPath)}`).catch(error => { + win.loadURL(`app://./index.html`).catch(error => { throw new Error(error.message); }); } diff --git a/src/components/Control.vue b/src/components/Control.vue index 5ea962f..435ddab 100644 --- a/src/components/Control.vue +++ b/src/components/Control.vue @@ -49,11 +49,12 @@ @@ -117,15 +118,17 @@ export default { console.log(`[control] delete unused control ${this.controlObj.id}`) }, copyAsset(src) { - const newId = nanoid(10) - const fse = window.fse - fse.copyAsset(src.path, newId) - // TODO: Make sure this emit happens *after* copyAsset completes. - setTimeout( - () => - this.$emit('updateControlValue', `${newId}.png`, this.controlObj.id), - 2000 - ); + console.log("Copy asset called."); + const newId = nanoid(10); + window.fse.copyAsset(src.path, newId).then((status) => { + if (status) { + setTimeout(() => { + this.$emit('updateControlValue', `${newId}.png`, this.controlObj.id); + }, 1000); + } else { + alert("This image was unable to be imported, please try again."); + } + }); } } } diff --git a/src/components/ControlForm.vue b/src/components/ControlForm.vue index 44a306f..809d840 100644 --- a/src/components/ControlForm.vue +++ b/src/components/ControlForm.vue @@ -146,7 +146,7 @@ export default { if (this.controlObj.type.toLowerCase() === 'color') { return '#EC3131' } else if (this.controlObj.type.toLowerCase() === 'image') { - return 'gray' + return 'gray.png' } else { return Math.round((this.controlObj.min + this.controlObj.max) / 30) || 0 } diff --git a/src/preload.js b/src/preload.js index 9f210a5..9b2d76e 100644 --- a/src/preload.js +++ b/src/preload.js @@ -2,7 +2,6 @@ const dgram = require('dgram') const electronStore = require('electron-store') const fse = require('fs-extra') const path = require('path') -const qs = require('querystring'); const kinetSocket = dgram.createSocket('udp4') // IPv4 @@ -17,10 +16,7 @@ const kinetSocket = dgram.createSocket('udp4') // IPv4 // Win: %APPDATA%/.local/share/mit-illuminations/config.json // See https://issuehunt.io/r/sindresorhus/electron-store/issues/181. -// wes note: this is now being passed as a query string to the URL of this -// page (when loaded from the electron background process) -- this gets around -// the need to use IPC to look it up -const userDataPath = qs.decode(location.search.slice(1)).userDataPath; +const userDataPath = window.process.argv.slice(-1)[0]; /** * Create the Electron Store @@ -32,7 +28,7 @@ const estore = new electronStore({ window.dgram = {}; window.dgram.send = (buf, port, ip) => { - kinetSocket.send(buf, port, ip, function(error){ + kinetSocket.send(buf, port, ip, function (error) { if (error) { console.log(error); } @@ -42,13 +38,19 @@ window.dgram.send = (buf, port, ip) => { window.estore = estore; window.fse = {}; -window.fse.copyAsset = (src, id) => { - const ext = path.extname(src) +window.fse.copyAsset = async (src, id) => { + const ext = path.extname(src); + console.log(userDataPath, id, ext, src); const dest = path.join(userDataPath, 'user_uploads', `${id}${ext}`); - - fse.copy(src, dest, (err) => { - if (err) return console.error(err) - }).catch(console.log); + try { + await fse.copy(src, dest, (err) => { + if (err) return console.error(err) + }); + return true; + } catch (error) { + console.error(error); + return false; + } };