Switched to using e-z.gg
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
import { $, S3Client } from "bun";
|
||||
import { $ } from "bun";
|
||||
import { configDotenv } from "dotenv";
|
||||
import { homedir } from "os";
|
||||
import { readdirSync, readFileSync, statSync } from "fs";
|
||||
import { join } from "path";
|
||||
import { setConsoleTitle } from "@ocbwoy3/libocbwoy3";
|
||||
import { UploadToEZ } from "../lib/EZUploader";
|
||||
|
||||
setConsoleTitle("R2 Uploader");
|
||||
setConsoleTitle("Screenshot Uploader");
|
||||
|
||||
try {
|
||||
const start = Date.now();
|
||||
@@ -14,15 +15,6 @@ try {
|
||||
path: `${homedir()}/.ocbwoy3-dotfiles-SECRET-DO-NOT-TOUCH.env`
|
||||
});
|
||||
|
||||
const bucket = new S3Client({
|
||||
accessKeyId: process.env.S3_ACCESS_KEY,
|
||||
secretAccessKey: process.env.S3_SECRET_KEY,
|
||||
bucket: process.env.S3_BUCKET_NAME,
|
||||
endpoint: process.env.S3_ENDPOINT_URL,
|
||||
});
|
||||
|
||||
let urlParams = "";
|
||||
|
||||
const screenshotsDir = join(homedir(), "Pictures", "Screenshots");
|
||||
const files = readdirSync(screenshotsDir);
|
||||
|
||||
@@ -35,29 +27,11 @@ try {
|
||||
|
||||
const filePath = join(screenshotsDir, latestFile);
|
||||
|
||||
let [ _, regretevator, floorNum ] = latestFile.match(/\-(regretevator)\-?([0-9]+)?\.png$/) || [];
|
||||
const url = await UploadToEZ(readFileSync(filePath));
|
||||
|
||||
// the devs changed rich presence, what's the point of parsing the filename if tuxstrap doesn't update the state file anymore?
|
||||
if (regretevator === "regretevator") {
|
||||
latestFile = latestFile.replace(`-regretevator${floorNum ? `-${floorNum}` : ""}.png`,".png")
|
||||
// OR i could keep the filename and have my worker parse the filename
|
||||
if (floorNum) {
|
||||
urlParams = `?floor=${floorNum}`
|
||||
}
|
||||
}
|
||||
|
||||
const file = bucket.file(latestFile)
|
||||
await file.write(readFileSync(filePath), {
|
||||
type: "image/png"
|
||||
})
|
||||
$`echo "https://i.darktru.win/${encodeURIComponent(latestFile)}${urlParams}" | wl-copy -n`.nothrow().catch(a => { });
|
||||
$`notify-send "Screenshot" "Uploaded in ${Date.now() - start}ms<br/><small>${latestFile}</small>"`.nothrow().catch(a => { });
|
||||
$`echo "${url}" | wl-copy -n`.nothrow().catch(a => { });
|
||||
$`notify-send "Screenshot" "Uploaded to e-z.host in ${Date.now() - start}ms, URL copied to clipboard"`.nothrow().catch(a => { });
|
||||
} catch (e_) {
|
||||
const cx = `${e_}`.toLowerCase();
|
||||
if (cx.includes("enable r2") && cx.includes("cloudflare dashboard")) {
|
||||
$`notify-send "Screenshot" "YOU OWE CLOUDFLARE MONEY!!!!"`.nothrow().catch(a => { });
|
||||
process.exit(0);
|
||||
}
|
||||
$`notify-send "Screenshot" "${`${e_}`}"`.nothrow().catch(a => { });
|
||||
}
|
||||
|
||||
|
||||
49
scripts/lib/EZUploader.ts
Normal file
49
scripts/lib/EZUploader.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { homedir } from "os";
|
||||
const API_URL = "https://api.e-z.host/files";
|
||||
|
||||
/**
|
||||
* Uploads a PNG image to e-z and returns the embed URL.
|
||||
* Throws an error if the file is not a PNG.
|
||||
*
|
||||
* @param content Image content as a Buffer
|
||||
* @returns The uploaded image's URL
|
||||
*/
|
||||
export async function UploadToEZ(content: Buffer): Promise<string> {
|
||||
if (!process.env.EZ_API_KEY) {
|
||||
throw new Error(`Missing \`EZ_API_KEY\`, make sure you're loading the env from \`${homedir()}/.ocbwoy3-dotfiles-SECRET-DO-NOT-TOUCH.env\`!`)
|
||||
}
|
||||
|
||||
if (!(content[0] === 0x89 && content[1] === 0x50 && content[2] === 0x4e)) {
|
||||
throw new Error("File is not a PNG.");
|
||||
}
|
||||
|
||||
const form = new FormData();
|
||||
form.append(
|
||||
"file",
|
||||
new Blob([content], { type: "image/png" }),
|
||||
"screenshot.png"
|
||||
);
|
||||
|
||||
const res = await fetch(API_URL, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
key: process.env.EZ_API_KEY,
|
||||
},
|
||||
body: form,
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(
|
||||
`Upload failed with status ${res.status}: ${await res.text()}`
|
||||
);
|
||||
}
|
||||
|
||||
const json = await res.json();
|
||||
|
||||
if (!json.imageUrl) {
|
||||
console.warn(json);
|
||||
throw new Error("Malformed response from e-z.gg");
|
||||
}
|
||||
|
||||
return json.url;
|
||||
}
|
||||
Reference in New Issue
Block a user