persistent-image-url
Version:
A lightweight Node.js library for persisting image URLs using the [imgbb.com](https://imgbb.com/) or [sm.ms](https://sm.ms/) API. This library allows you to upload images from temporary URLs and get back persistent URLs that can be used to access the imag
41 lines (34 loc) • 1.04 kB
text/typescript
import axios from "axios";
import { uploadImageToImgbb } from "./services/imgbb-service";
import { uploadImageToSmms } from "./services/smms-service";
type Uploader = "imgbb" | "smms";
export type UploaderConfig = {
uploader?: Uploader;
token: string;
};
export async function persistImage(
tempUrl: string,
config: UploaderConfig
): Promise<string> {
try {
// default to imgbb
if (!config.uploader) {
config.uploader = "imgbb";
}
const imageStream = await axios.get(tempUrl, {
responseType: "arraybuffer",
});
const imageBase64 = Buffer.from(imageStream.data, "binary").toString(
"base64"
);
if (config.uploader === "imgbb") {
return uploadImageToImgbb(imageBase64, config.token);
} else if (config.uploader === "smms") {
return uploadImageToSmms(imageBase64, config.token);
} else {
throw new Error("Invalid uploader specified");
}
} catch (error) {
throw new Error("Unable to persist image");
}
}