-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayground.js
More file actions
74 lines (59 loc) · 1.96 KB
/
Copy pathplayground.js
File metadata and controls
74 lines (59 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
async function runPlayground() {
const apiKey = document.getElementById("apiKey").value;
const operation = document.getElementById("operation").value;
const width = document.getElementById("width").value;
const watermark = document.getElementById("watermark").value;
const fileInput = document.getElementById("imageFile");
const status = document.getElementById("status");
const preview = document.getElementById("preview");
const previewText = document.getElementById("previewText");
const downloadLink = document.getElementById("downloadLink");
if (!apiKey || !fileInput.files.length) {
status.innerText = "❌ API key and image required";
return;
}
status.innerText = "⏳ Processing...";
preview.style.display = "none";
previewText.style.display = "block";
const file = fileInput.files[0];
const formData = new FormData();
formData.append("file", file);
let endpoint = "";
let url = "https://fileops-api.onrender.com";
if (operation === "compress") {
endpoint = "/compress-image";
}
if (operation === "resize") {
endpoint = `/resize-image?width=${width || ""}`;
}
if (operation === "watermark") {
endpoint = `/watermark-image?text=${watermark || "FileOps"}`;
}
if (operation === "convert") {
endpoint = "/convert-image?target_format=jpeg";
}
try {
const res = await fetch(url + endpoint, {
method: "POST",
headers: {
"x-api-key": apiKey
},
body: formData
});
if (!res.ok) {
const text = await res.text();
status.innerText = "❌ Error: " + text;
return;
}
const blob = await res.blob();
const objectURL = URL.createObjectURL(blob);
preview.src = objectURL;
preview.style.display = "block";
previewText.style.display = "none";
downloadLink.href = objectURL;
downloadLink.querySelector("button").disabled = false;
status.innerText = "✅ Success";
} catch (err) {
status.innerText = "❌ Network error";
}
}