feat: images

This commit is contained in:
Florian Sylvain
2024-06-13 13:34:10 +02:00
parent eb98a1003e
commit c25fb1b0a3
19 changed files with 387 additions and 100 deletions
@@ -30,7 +30,7 @@
</div>
</div>
<div class="text-black-50 d-flex align-items-center justify-content-center h-100 py-3">
<textarea id="postBody" name="postBody">{{.Body}}</textarea>
<textarea id="postBody" name="postBody">{{.Post.Body}}</textarea>
</div>
</form>
@@ -49,43 +49,43 @@
},
license_key: 'gpl',
images_upload_handler: (blobInfo, progress) => new Promise((resolve, reject) => {
// const xhr = new XMLHttpRequest();
// xhr.withCredentials = false;
// xhr.open('POST', 'postAcceptor.php');
//
// xhr.upload.onprogress = (e) => {
// progress(e.loaded / e.total * 100);
// };
//
// xhr.onload = () => {
// if (xhr.status === 403) {
// reject({message: 'HTTP Error: ' + xhr.status, remove: true});
// return;
// }
//
// if (xhr.status < 200 || xhr.status >= 300) {
// reject('HTTP Error: ' + xhr.status);
// return;
// }
//
// const json = JSON.parse(xhr.responseText);
//
// if (!json || typeof json.location != 'string') {
// reject('Invalid JSON: ' + xhr.responseText);
// return;
// }
//
// resolve(json.location);
// };
//
// xhr.onerror = () => {
// reject('Image upload failed due to a XHR Transport error. Code: ' + xhr.status);
// };
//
// const formData = new FormData();
// formData.append('file', blobInfo.blob(), blobInfo.filename());
//
// xhr.send(formData);
const xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.open('POST', `/post/{{.Post.ID}}/image/create`);
xhr.upload.onprogress = (e) => {
progress(e.loaded / e.total * 100);
};
xhr.onload = () => {
if (xhr.status === 403) {
reject({message: 'HTTP Error: ' + xhr.status, remove: true});
return;
}
if (xhr.status < 200 || xhr.status >= 300) {
reject('HTTP Error: ' + xhr.status);
return;
}
const json = JSON.parse(xhr.responseText);
if (!json || typeof json.location != 'string') {
reject('Invalid JSON: ' + xhr.responseText);
return;
}
resolve(`http{{ if .Secured }}s{{ end }}://${window.location.host}${json.location}`);
};
xhr.onerror = () => {
reject('Image upload failed due to a XHR Transport error. Code: ' + xhr.status);
};
const formData = new FormData();
formData.append('file', blobInfo.blob(), blobInfo.filename());
xhr.send(formData);
}),
})
</script>