Skip to content

Commit

Permalink
Check bitmap files in the browser, before upload. This gives the user…
Browse files Browse the repository at this point in the history
… more information in case the file format is not supported.
  • Loading branch information
BlueAndi committed Dec 4, 2023
1 parent 2e4a78d commit 545218f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 36 deletions.
43 changes: 40 additions & 3 deletions data/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ utils.makeRequest = function(options) {
var urlEncodedPar = "";
var isJsonResponse = false;
var isFirst = true;
var key;

if ("object" === typeof options.formData) {
formData = options.formData;
Expand All @@ -41,7 +42,7 @@ utils.makeRequest = function(options) {
if ("get" === options.method.toLowerCase()) {
urlEncodedPar += "?";

for(var key in options.parameter) {
for(key in options.parameter) {
if (true === isFirst) {
isFirst = false;
} else {
Expand All @@ -54,7 +55,7 @@ utils.makeRequest = function(options) {
} else {
formData = new FormData();

for(var key in options.parameter) {
for(key in options.parameter) {
formData.append(key, options.parameter[key]);
}
}
Expand Down Expand Up @@ -127,4 +128,40 @@ utils.readJsonFile = function(file) {
}
rawFile.send(null);
});
};
};

utils.checkBMPFile = function(file) {
return new Promise(function(resolve, reject) {
var reader = new FileReader();

reader.onload = function(e) {
resolve(e.target.result);
};

reader.readAsArrayBuffer(file);
}).then(function(buffer) {
var bitmapHeaderSize = 54;
var header = new Uint8Array(buffer, 0, bitmapHeaderSize);
var planes = (header[27] << 8) | (header[26] << 0);
var bitsPerPixel = (header[29] << 8) | (header[28] << 0);
var compression = (header[33] << 24) | (header[32] << 16) | (header[31] << 8) | (header[30] << 0);
var paletteColors = (header[49] << 24) | (header[48] << 16) | (header[47] << 8) | (header[46] << 0);
var promise = null;

if ("BM" !== String.fromCharCode.apply(null, header.subarray(0, 2))) {
promise = Promise.reject("No bitmap file.");
} else if (1 !== planes) {
promise = Promise.reject("Only 1 plane is supported.");
} else if ((24 !== bitsPerPixel) && (32 !== bitsPerPixel)) {
promise = Promise.reject("Only 24 or 32 bpp are supported.");
} else if (0 !== compression) {
promise = Promise.reject("No compression is supported.");
} else if (0 !== paletteColors) {
promise = Promise.reject("Color palette not supported.");
} else {
promise = Promise.resolve();
}

return promise;
});
};
26 changes: 15 additions & 11 deletions lib/IconTextLampPlugin/web/IconTextLampPlugin.html
Original file line number Diff line number Diff line change
Expand Up @@ -257,21 +257,25 @@ <h3 class="mt-1">Lamp</h3>
function uploadIcon(pluginUid, file) {
disableUI();

return utils.makeRequest({
method: "POST",
url: "/rest/api/v1/display/uid/" + pluginUid + "/bitmap",
isJsonResponse: true,
parameter: {
file: file
},
headers: {
"X-File-Size": file.size
}
return utils.checkBMPFile(file).then(function() {
return utils.makeRequest({
method: "POST",
url: "/rest/api/v1/display/uid/" + pluginUid + "/bitmap",
isJsonResponse: true,
parameter: {
file: file
},
headers: {
"X-File-Size": file.size
}
});
}).then(function(rsp) {
updateForms();
alert("Ok.");
}).catch(function(rsp) {
if (("undefined" !== typeof rsp.error) && ("string" === typeof rsp.error.msg)) {
if ("string" === typeof rsp) {
alert("Error: " + rsp);
} else if (("undefined" !== typeof rsp) && ("undefined" !== typeof rsp.error) && ("string" === typeof rsp.error.msg)) {
alert("Error: " + rsp.error.msg);
} else {
alert("Failed.");
Expand Down
26 changes: 15 additions & 11 deletions lib/IconTextPlugin/web/IconTextPlugin.html
Original file line number Diff line number Diff line change
Expand Up @@ -214,21 +214,25 @@ <h3 class="mt-1">Text</h3>
function uploadIcon(pluginUid, file) {
disableUI();

return utils.makeRequest({
method: "POST",
url: "/rest/api/v1/display/uid/" + pluginUid + "/bitmap",
isJsonResponse: true,
parameter: {
file: file
},
headers: {
"X-File-Size": file.size
}
return utils.checkBMPFile(file).then(function() {
return utils.makeRequest({
method: "POST",
url: "/rest/api/v1/display/uid/" + pluginUid + "/bitmap",
isJsonResponse: true,
parameter: {
file: file
},
headers: {
"X-File-Size": file.size
}
});
}).then(function(rsp) {
updateForms();
alert("Ok.");
}).catch(function(rsp) {
if (("undefined" !== typeof rsp.error) && ("string" === typeof rsp.error.msg)) {
if ("string" === typeof rsp) {
alert("Error: " + rsp);
} else if (("undefined" !== typeof rsp) && ("undefined" !== typeof rsp.error) && ("string" === typeof rsp.error.msg)) {
alert("Error: " + rsp.error.msg);
} else {
alert("Failed.");
Expand Down
26 changes: 15 additions & 11 deletions lib/ThreeIconPlugin/web/ThreeIconPlugin.html
Original file line number Diff line number Diff line change
Expand Up @@ -302,21 +302,25 @@ <h3 class="mt-1">Animation configuration</h3>
function setIcon(pluginUid, file, index) {
disableUI();

return utils.makeRequest({
method: "POST",
url: "/rest/api/v1/display/uid/" + pluginUid + "/bitmap/" + index,
isJsonResponse: true,
parameter: {
file: file
},
headers: {
"X-File-Size": file.size
}
return utils.checkBMPFile(file).then(function() {
return utils.makeRequest({
method: "POST",
url: "/rest/api/v1/display/uid/" + pluginUid + "/bitmap/" + index,
isJsonResponse: true,
parameter: {
file: file
},
headers: {
"X-File-Size": file.size
}
});
}).then(function(rsp) {
updateForms();
alert("Ok.");
}).catch(function(rsp) {
if (("undefined" !== typeof rsp.error) && ("string" === typeof rsp.error.msg)) {
if ("string" === typeof rsp) {
alert("Error: " + rsp);
} else if (("undefined" !== typeof rsp) && ("undefined" !== typeof rsp.error) && ("string" === typeof rsp.error.msg)) {
alert("Error: " + rsp.error.msg);
} else {
alert("Failed.");
Expand Down

0 comments on commit 545218f

Please sign in to comment.