Files
ephemere/src/s3/upload.ts
T
naomi 6fe566b3f6
Node.js CI / CI (push) Successful in 25s
Security Scan and Upload / Security & DefectDojo Upload (push) Successful in 59s
feat: turn mimetype into proper util to dedupe this all
2026-01-08 23:29:50 -08:00

69 lines
2.2 KiB
TypeScript

/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import { input } from "@inquirer/prompts";
import { getMimeType } from "../utils/mimeType.js";
const accessKeyId = process.env.AWS_ACCESS_KEY_ID;
const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
if (accessKeyId === undefined || secretAccessKey === undefined) {
throw new Error("AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY is not set");
}
const fileName = await input({
message:
// eslint-disable-next-line stylistic/max-len -- Big boi string.
"Enter the name of the file to upload. Your file MUST be in the `data` directory in this repository. WITHOUT leading slash. Example: 'naomi.png' or 'img/naomi.png'",
});
if (fileName === "") {
throw new Error("File name is not set");
}
const file = await readFile(
join(import.meta.dirname, "..", "..", "data", fileName),
);
const uploadPath = await input({
message:
// eslint-disable-next-line stylistic/max-len -- Big boi string.
"Enter the PATH to upload the file to, WITHOUT leading slash. Example: 'img/naomi.png' or 'naomi.png':",
});
if (uploadPath === "") {
throw new Error("Upload path is not set");
}
const s3 = new S3Client({
credentials: { accessKeyId, secretAccessKey },
endpoint: "https://hel1.your-objectstorage.com",
region: "hel1",
});
const contentType = getMimeType(uploadPath);
if (contentType === undefined) {
console.warn(
`Warning: Unknown file type for ${uploadPath}. Content type will not be set.`,
);
}
const command = new PutObjectCommand({
// eslint-disable-next-line @typescript-eslint/naming-convention -- AWS SDK
Body: file,
// eslint-disable-next-line @typescript-eslint/naming-convention -- AWS SDK
Bucket: "nhcarrigan",
// eslint-disable-next-line @typescript-eslint/naming-convention -- AWS SDK
ContentType: contentType,
// eslint-disable-next-line @typescript-eslint/naming-convention -- AWS SDK
Key: uploadPath,
});
await s3.send(command);
console.log(`Uploaded ${fileName} to ${uploadPath}`);