@reliverse/rse-sdk
Version:
@reliverse/rse-sdk without cli. @reliverse/rse-sdk allows you to create new plugins for @reliverse/rse CLI, interact with reliverse.org, and even extend your own CLI functionality (you may also try @reliverse/dler-sdk for this case).
41 lines (40 loc) • 1.36 kB
JavaScript
import path from "@reliverse/pathkit";
import fs from "node:fs/promises";
import { uploadToUploadcare } from "./uploadcare.js";
import { uploadToUploadthing } from "./uploadthing.js";
export async function uploadToProvider(files, provider) {
const defaultProvider = process.env.DEFAULT_UPLOAD_PROVIDER || "uploadthing";
const chosen = provider || defaultProvider;
switch (chosen) {
case "uploadcare":
return uploadToUploadcare(files);
case "uploadthing":
return uploadToUploadthing(files);
default:
throw new Error(`Unsupported provider: ${chosen}`);
}
}
export function getMimeType(filePath) {
const extension = path.extname(filePath).toLowerCase();
const mimeTypes = {
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".png": "image/png",
".gif": "image/gif",
".pdf": "application/pdf",
".doc": "application/msword",
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
".txt": "text/plain"
};
return mimeTypes[extension] || "application/octet-stream";
}
export async function readFilesFromPaths(filePaths) {
return Promise.all(
filePaths.map(async (filePath) => {
const data = await fs.readFile(filePath);
const name = path.basename(filePath);
const type = getMimeType(filePath);
return { name, data, type };
})
);
}