10
0
mirror of https://github.com/LCPQ/QUESTDB_website.git synced 2024-11-04 21:24:00 +01:00
QUESTDB_website/static/js/getTextFromFile.js

59 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-11-16 12:44:21 +01:00
var fileCache = function () {
2020-11-25 01:52:53 +01:00
var site_url = "/"+window.location.pathname.split('/')[1];
var json_url = site_url+"/data/database.json";
2020-11-16 12:44:21 +01:00
var req = new XMLHttpRequest();
2020-11-16 12:56:16 +01:00
req.open("GET",json_url, false);
2020-11-16 12:44:21 +01:00
req.send();
2020-11-16 12:56:16 +01:00
return JSON.parse(req.responseText);
2020-11-16 12:44:21 +01:00
}
_cache = fileCache();
2020-11-16 12:56:16 +01:00
function getTextFromFileUrl(url,header={}) {
2020-11-30 00:17:08 +01:00
if (url in _cache) {
return _cache[url];
} else {
return async () => {await getTextFromFileUrlAsync(url)};
}
2020-11-16 12:44:21 +01:00
}
async function getTextFromFileUrlAsync(url,header={}) {
2019-09-09 15:15:39 +02:00
return new Promise(function (resolve, reject) {
var req = new XMLHttpRequest();
2019-10-02 19:20:37 +02:00
req.open("GET",url, true);
for (const [key, val] of Object.entries(header)) {
req.setRequestHeader(key,val)
}
2019-09-09 15:15:39 +02:00
req.onreadystatechange = function() {
if (req.readyState == 4) {
2020-05-27 09:35:50 +02:00
if (req.status == 200 || req.status==304) {//when a good response is given do this
2019-09-09 15:15:39 +02:00
var text = req.responseText;
resolve(text);
} else {
reject({
status: req.status,
statusText: req.statusText
});
}
}
}
req.send();
});
2019-10-02 19:20:37 +02:00
}
async function getTextFromUploadedFileAsync(inputFile){
const temporaryFileReader = new FileReader();
return new Promise((resolve, reject) => {
temporaryFileReader.onerror = () => {
temporaryFileReader.abort();
reject(new DOMException("Problem parsing input file."));
};
temporaryFileReader.onload = () => {
resolve(temporaryFileReader.result);
};
temporaryFileReader.readAsText(inputFile);
});
2020-11-16 12:44:21 +01:00
};