1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2025-06-18 02:23:12 +02:00

File upload

This commit is contained in:
Philipp Heckel 2022-04-01 08:41:45 -04:00
parent 2bdae49425
commit aabae53e5d
2 changed files with 115 additions and 33 deletions
web/src/app

View file

@ -1,4 +1,6 @@
import {
basicAuth,
encodeBase64,
fetchLinesIterator,
maybeWithBasicAuth,
topicShortUrl,
@ -42,6 +44,43 @@ class Api {
});
}
publishXHR(baseUrl, topic, body, headers, onProgress) {
const url = topicUrl(baseUrl, topic);
const xhr = new XMLHttpRequest();
console.log(`[Api] Publishing message to ${url}`);
const send = new Promise(function (resolve, reject) {
xhr.open("PUT", url);
xhr.addEventListener('readystatechange', (ev) => {
if (xhr.readyState === 4 && xhr.status >= 200 && xhr.status <= 299) {
console.log(`[Api] Publish successful`, ev);
resolve(xhr.response);
} else if (xhr.readyState === 4) {
console.log(`[Api] Publish failed (1)`, ev);
xhr.abort();
reject(ev);
}
})
xhr.onerror = (ev) => {
console.log(`[Api] Publish failed (2)`, ev);
reject(ev);
};
xhr.upload.addEventListener("progress", onProgress);
if (body.type) {
xhr.overrideMimeType(body.type);
}
for (const [key, value] of Object.entries(headers)) {
xhr.setRequestHeader(key, value);
}
xhr.send(body);
});
send.abort = () => {
console.log(`[Api] Publish aborted by user`);
xhr.abort();
}
return send;
}
async auth(baseUrl, topic, user) {
const url = topicUrlAuth(baseUrl, topic);
console.log(`[Api] Checking auth for ${url}`);