Skip to content

Commit

Permalink
Fix issue with saving firmware files to local machine
Browse files Browse the repository at this point in the history
  • Loading branch information
bfmvsa committed Sep 19, 2024
1 parent 3c6197d commit 7384aa6
Showing 1 changed file with 15 additions and 43 deletions.
58 changes: 15 additions & 43 deletions tabs/firmware_flasher.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ TABS.firmware_flasher.initialize = function (callback) {
}

var intel_hex = false, // standard intel hex in string format
parsed_hex = false; // parsed raw hex in array format
parsed_hex = false, // parsed raw hex in array format
file_name = 'inav.hex'; // default suggested filename of locally saved file

GUI.load(path.join(__dirname, "firmware_flasher.html"), function () {
// translate to user-selected language
Expand Down Expand Up @@ -450,6 +451,7 @@ TABS.firmware_flasher.initialize = function (callback) {
$('div.release_info .date').text(summary.date);
$('div.release_info .status').text(summary.status);
$('div.release_info .file').text(summary.file).prop('href', summary.url);
file_name = summary.file;

var formattedNotes = marked.parse(summary.notes);
$('div.release_info .notes').html(formattedNotes);
Expand Down Expand Up @@ -538,49 +540,19 @@ TABS.firmware_flasher.initialize = function (callback) {
}
});

$(document).on('click', 'span.progressLabel a.save_firmware', function () {
chrome.fileSystem.chooseEntry({type: 'saveFile', suggestedName: 'inav', accepts: [{extensions: ['hex']}]}, function (fileEntry) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
return;
}

chrome.fileSystem.getDisplayPath(fileEntry, function (path) {
console.log('Saving firmware to: ' + path);

// check if file is writable
chrome.fileSystem.isWritableEntry(fileEntry, function (isWritable) {
if (isWritable) {
var blob = new Blob([intel_hex], {type: 'text/plain'});

fileEntry.createWriter(function (writer) {
var truncated = false;

writer.onerror = function (e) {
console.error(e);
};

writer.onwriteend = function() {
if (!truncated) {
// onwriteend will be fired again when truncation is finished
truncated = true;
writer.truncate(blob.size);

return;
}
};

writer.write(blob);
}, function (e) {
console.error(e);
});
} else {
console.log('You don\'t have write permissions for this file, sorry.');
GUI.log(i18n.getMessage('writePermissionsForFile'));
}
});
});
$(document).on('click', 'span.progressLabel a.save_firmware', async function () {
const result = await dialog.showSaveDialog({
defaultPath: file_name,
});

try {
fs.writeFileSync(result.filePath, intel_hex);
GUI.log('Saved firmware to: ' + result.filePath);
}
catch(e) {
console.error(e);
GUI.log('Failed to save the file !');
}
});


Expand Down

0 comments on commit 7384aa6

Please sign in to comment.