Files
GDevelop/newIDE/electron-app/app/LocalFileUploader.js
Florian Rival 057c0a1d13 Add support for the Asset Store on the desktop app
Also:
* Automatically download resources when a project made on the web-app is opened on the desktop-app
* Rework loader for when opening a project or launching a preview
* Shorten names for resources added from the resource store, in the web-app
2020-11-12 21:14:18 +00:00

56 lines
1.7 KiB
JavaScript

const fs = require('fs');
const axios = require('axios');
const { Transform } = require('stream');
module.exports = {
uploadLocalFile: (localFilePath, uploadOptions, onProgress) => {
return new Promise((resolve, reject) => {
fs.stat(localFilePath, (err, stats) => {
if (err) {
reject(err);
return;
}
if (!stats || !stats.size) {
reject(
new Error(
'Unable to find the size - or empty size - for ' + localFilePath
)
);
return;
}
const fileSize = stats.size;
const fileReadStream = fs.createReadStream(localFilePath);
// Use a transform to follow the progress of the upload
// (though not very accurate because following the progress
// of the read on disk..)
let loadedBytes = 0;
const fileWithProgressReadStream = new Transform({
transform(chunk, encoding, callback) {
loadedBytes += chunk.length;
onProgress(loadedBytes, fileSize);
callback(undefined, chunk);
},
});
fileReadStream.pipe(fileWithProgressReadStream);
resolve(
axios({
method: 'PUT',
url: uploadOptions.signedUrl,
data: fileWithProgressReadStream,
headers: {
'Content-Type': uploadOptions.contentType,
'Content-Length': fileSize,
},
// Allow any arbitrary large file to be sent
maxContentLength: Infinity,
// onUploadProgress does not work on Node.js
})
);
});
}).then(() => undefined);
},
};