diff --git a/gui/main.js b/gui/main.js index d12df0b1..542a1fdf 100644 --- a/gui/main.js +++ b/gui/main.js @@ -511,7 +511,7 @@ ipcMain.on("select-file", (event, data) => { }); }); -//select file +//select image file ipcMain.on("select-user-image", (event, data) => { dialog .showOpenDialog({ @@ -523,17 +523,16 @@ ipcMain.on("select-user-image", (event, data) => { console.log(filepath.filePaths[0]); try { - //fs.readFile(filepath.filePaths[0], 'utf8', function (err, data) { - //Has to be binary - fs.readFile(filepath.filePaths[0], "binary", function (err, data) { - console.log(data.length); + // read data as base64 which makes conversion to blob easier + fs.readFile(filepath.filePaths[0], "base64", function (err, data) { var filename = path.basename(filepath.filePaths[0]); var mimeType = mime.getType(filename); - console.log(mimeType); + if (mimeType == "" || mimeType == null) { mimeType = "plain/text"; } + chat.webContents.send("return-select-user-image", { data: data, mime: mimeType, diff --git a/gui/preload-chat.js b/gui/preload-chat.js index 7d80adbe..46188633 100644 --- a/gui/preload-chat.js +++ b/gui/preload-chat.js @@ -2,6 +2,8 @@ const path = require("path"); const { ipcRenderer } = require("electron"); const { v4: uuidv4 } = require("uuid"); const imageCompression = require("browser-image-compression"); +const blobUtil = require("blob-util"); + // https://stackoverflow.com/a/26227660 var appDataFolder = @@ -494,24 +496,15 @@ ipcRenderer.on("return-selected-files", (event, arg) => { }); ipcRenderer.on("return-select-user-image", (event, arg) => { - filetype = arg.mime; - //console.log(filetype); + let imageFiletype = arg.mime; + let imageFile = arg.data; - file = arg.data; - //filename = arg.filename; - - //console.log(arg.data) - - //document.getElementById("userImage").src = ''; - - //var imageFile = arg.data; - var imageFile = arg.data; - console.log(imageFile); + imageFile = blobUtil.base64StringToBlob(imageFile, imageFiletype); var options = { maxSizeMB: 1, - maxWidthOrHeight: 1920, - useWebWorker: true, + maxWidthOrHeight: 200, + useWebWorker: false, }; imageCompression(imageFile, options)