Skip to content

Instantly share code, notes, and snippets.

@0PandaDEV
Last active February 13, 2024 23:29
Show Gist options
  • Save 0PandaDEV/218de4a0d4c956f684a0b24a979b7ffb to your computer and use it in GitHub Desktop.
Save 0PandaDEV/218de4a0d4c956f684a0b24a979b7ffb to your computer and use it in GitHub Desktop.
// Name: Transfer.sh Uploader
import "@johnlindquist/kit"
import fs from "fs";
import got from "got";
import path from "path";
import { pipeline, PassThrough } from "stream";
import { promisify } from "util";
import clipboard from "clipboardy";
const serverUrl = "https://transfer.sh"
try {
let fileInfo = await drop()
const jsonArray = fileInfo;
const filePath = jsonArray[0].path;
const fileName = path.basename(filePath);
const fullUrl = `${serverUrl}/${fileName}`;
const stats = fs.statSync(filePath);
const fileSizeInBytes = stats.size;
const fileStream = fs.createReadStream(filePath);
let uploadedBytes = 0;
div(`Uploading ${fileName}: 0%`, `text-white p-4`)
const pipelinePromise = promisify(pipeline);
const monitorStream = new PassThrough();
monitorStream.on("data", (chunk) => {
uploadedBytes += chunk.length;
const uploadPercentage = Math.round(
(uploadedBytes / fileSizeInBytes) * 100,
);
div(`Uploading ${fileName}: ${uploadPercentage}%`, `text-white p-4`)
});
const uploadStream = got.put(fullUrl, {
headers: {
"Content-Type": "application/octet-stream",
},
body: monitorStream,
});
await pipelinePromise(fileStream, monitorStream);
const response = await uploadStream;
clipboard.writeSync(response.body);
await div(`Copied URL to clipboard: ${fileName}`, `text-white p-4`)
} catch (error) {
await div(`Upload failed: ${error instanceof Error ? error.message : "An unknown error occurred"}`, `text-white p-4`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment