Skip to content

Commit

Permalink
fs and del file
Browse files Browse the repository at this point in the history
  • Loading branch information
bramses committed Oct 19, 2023
1 parent 6301813 commit 3aa7ef7
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cloudflare-images.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from "axios";
// fs promises
import { promises as fsPromises } from "fs";
import fs from "fs";
const { readFile } = fsPromises;
import FormData from "form-data";
import convert from "heic-convert";
Expand All @@ -23,13 +24,25 @@ async function saveImgFromUrl(url) {
}
}

async function deleteFile(filename) {
try {
fs.unlinkSync(filename);
return "Deleted file: " + filename;
} catch (err) {
console.error(err);
}
}


export const dalleImgToCF = async (url = "") => {
try {
if (!url || url === "") {
throw new Error("URL is required");
}
const filename = await saveImgFromUrl(url);
const cfUrl = await uploadToCloudflare(filename);
// delete file
await deleteFile(filename);
return cfUrl;
} catch (err) {
console.error(err);
Expand Down

0 comments on commit 3aa7ef7

Please sign in to comment.