feat: no_transform

This commit is contained in:
kieran 2024-05-29 12:14:00 +01:00
parent a85dbf88b9
commit a9a9ba6328
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
3 changed files with 77 additions and 44 deletions

View File

@ -6,7 +6,8 @@ Image hosting service
- [NIP-96 Support](https://github.com/nostr-protocol/nips/blob/master/96.md) - [NIP-96 Support](https://github.com/nostr-protocol/nips/blob/master/96.md)
- [Blossom Support](https://github.com/hzrd149/blossom/blob/master/buds/bud-01.md) - [Blossom Support](https://github.com/hzrd149/blossom/blob/master/buds/bud-01.md)
- Image compression to WebP (FFMPEG, NIP-96) - Image compression to WebP (FFMPEG, NIP-96)
- Blurhash calculation (NIP-96) - Blurhash calculation (NIP-96 only)
- AI image labeling ([ViT224](https://huggingface.co/google/vit-base-patch16-224))
## Planned ## Planned
- Torrent seed V2 - Torrent seed V2

View File

@ -117,6 +117,7 @@ struct Nip96Form<'r> {
caption: Option<&'r str>, caption: Option<&'r str>,
media_type: Option<&'r str>, media_type: Option<&'r str>,
content_type: Option<&'r str>, content_type: Option<&'r str>,
no_transform: Option<bool>,
} }
pub fn nip96_routes() -> Vec<Route> { pub fn nip96_routes() -> Vec<Route> {
@ -161,6 +162,9 @@ async fn upload(
return Nip96Response::error("File too large"); return Nip96Response::error("File too large");
} }
} }
if form.size > settings.max_upload_bytes {
return Nip96Response::error("File too large");
}
let file = match form.file.open().await { let file = match form.file.open().await {
Ok(f) => f, Ok(f) => f,
Err(e) => return Nip96Response::error(&format!("Could not open file: {}", e)), Err(e) => return Nip96Response::error(&format!("Could not open file: {}", e)),
@ -175,7 +179,7 @@ async fn upload(
} }
} }
match fs match fs
.put(file, mime_type, true) .put(file, mime_type, !form.no_transform.unwrap_or(false))
.await .await
{ {
Ok(blob) => { Ok(blob) => {

View File

@ -7,45 +7,67 @@
html { html {
background-color: black; background-color: black;
color: white; color: white;
font-size: 15px;
font-weight: 400;
font-family: Arial, serif;
}
.flex {
display: flex;
}
.flex-col {
flex-direction: column;
}
.gap-2 {
gap: 0.5rem;
} }
</style> </style>
<script> <script>
async function uploadFiles(e) { async function uploadFiles(e) {
const input = document.querySelector("#file"); try {
const file = input.files[0]; const input = document.querySelector("#file");
console.debug(file); const file = input.files[0];
console.debug(file);
const fd = new FormData(); const fd = new FormData();
fd.append("size", file.size.toString()); fd.append("size", file.size.toString());
fd.append("caption", file.name); fd.append("caption", file.name);
fd.append("media_type", file.type); fd.append("media_type", file.type);
fd.append("file", file); fd.append("file", file);
fd.append("no_transform", document.querySelector("#no_transform").checked.toString())
const auth_event = await window.nostr.signEvent({ const auth_event = await window.nostr.signEvent({
kind: 27235, kind: 27235,
created_at: Math.floor(new Date().getTime() / 1000), created_at: Math.floor(new Date().getTime() / 1000),
content: "", content: "",
tags: [ tags: [
["u", `${window.location.protocol}//${window.location.host}/n96`], ["u", `${window.location.protocol}//${window.location.host}/n96`],
["method", "POST"] ["method", "POST"]
] ]
}); });
const rsp = await fetch("/n96", { const rsp = await fetch("/n96", {
body: fd, body: fd,
method: "POST", method: "POST",
headers: { headers: {
accept: "application/json", accept: "application/json",
authorization: `Nostr ${btoa(JSON.stringify(auth_event))}`, authorization: `Nostr ${btoa(JSON.stringify(auth_event))}`,
}, },
}); });
console.debug(rsp); console.debug(rsp);
const text = await rsp.text(); const text = await rsp.text();
if (rsp.ok) { if (rsp.ok) {
document.querySelector("#log").append(JSON.stringify(JSON.parse(text), undefined, 2)); document.querySelector("#log").append(JSON.stringify(JSON.parse(text), undefined, 2));
} else { } else {
document.querySelector("#log").append(text); document.querySelector("#log").append(text);
}
document.querySelector("#log").append("\n");
} catch (ex) {
if (ex instanceof Error) {
alert(ex.message);
}
} }
document.querySelector("#log").append("\n");
} }
</script> </script>
</head> </head>
@ -53,19 +75,25 @@
<h1> <h1>
Welcome to void_cat_rs Welcome to void_cat_rs
</h1> </h1>
<div> <div class="flex flex-col gap-2">
<p> <div>
Upload a file using NIP-96 Upload a file using NIP-96
</p> </div>
<small> <div style="color: #ff8383;">
You must have a nostr extension for this to work You must have a nostr extension for this to work
</small> </div>
</div>
<div>
<input type="file" id="file"> <input type="file" id="file">
<button type="submit" onclick="uploadFiles(event)"> <div>
Upload <input type="checkbox" id="no_transform">
</button> <label for="no_transform">
Disable compression (images)
</label>
</div>
<div>
<button type="submit" onclick="uploadFiles(event)">
Upload
</button>
</div>
</div> </div>
<pre id="log"></pre> <pre id="log"></pre>
</body> </body>