import "@johnlindquist/kit";
import cloudinary from "cloudinary";
import trash from "trash";
const DIR = "screenshots";
const NOTIFY_SOUND_FILE_PATH = false;
const CUSTOM_DOMAIN = false;
let filePath = await arg();
let event = await arg();
const options = {
public_id: `${DIR}/${Date.now()}`,
unique_filename: true,
use_filename: true,
overwrite: true,
filename_override: true,
};
cloudinary.v2.config({
cloud_name: await env("CLOUDINARY_CLOUD_NAME"),
api_key: await env("CLOUDINARY_API_KEY"),
api_secret: await env("CLOUDINARY_API_SECRET"),
});
if (event === "add") {
await appendFile(home(`Desktop/${DIR}/download.log`), filePath + "\n");
const isVideoFile = filePath.endsWith(".mov");
await cloudinary.v2.uploader.upload(
filePath,
{ ...options, resource_type: isVideoFile ? "video" : "image" },
async (error, result) => {
if (error) {
console.error("Error uploading file:", error);
} else {
if (result) {
await copy(
CUSTOM_DOMAIN
? `${CUSTOM_DOMAIN}${result.public_id.replace(`${DIR}/`, "")}`
: isVideoFile
? result.url.replace(".mov", ".mp4")
: result.url
);
notify("✓ Uploaded to Cloudinary");
NOTIFY_SOUND_FILE_PATH &&
(await playAudioFile(NOTIFY_SOUND_FILE_PATH));
await trash([filePath]);
}
}
}
);
}