Skip to content

Instantly share code, notes, and snippets.

@rishi-raj-jain
Created June 17, 2023 09:20
Show Gist options
  • Save rishi-raj-jain/d3cd86a43236d3846e7b0bcc7906b455 to your computer and use it in GitHub Desktop.
Save rishi-raj-jain/d3cd86a43236d3846e7b0bcc7906b455 to your computer and use it in GitHub Desktop.
const fs = require("fs");
const path = require("path");
const https = require("https");
var ImageKit = require("imagekit");
const projectId = "";
var imagekit = new ImageKit({
publicKey: "",
privateKey: "",
urlEndpoint: "",
});
imagekit.listFiles(
{
skip: 0,
limit: 999,
},
function (error, result) {
if (error) console.log(error);
else {
result = result.map((i) => i.url);
const baseFolderPath = "./images";
result.forEach((url) => {
// Parse the URL
const parsedUrl = new URL(url);
// Get the path after the fixed prefix
const pathAfterPrefix = parsedUrl.pathname.replace("/" + projectId + "/", "");
// Create the local folder path
const folderPath = path.join(
baseFolderPath,
path.dirname(pathAfterPrefix)
);
// Create the local folder if it doesn't exist
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });
}
// Extract the filename from the URL
const filename = path.basename(parsedUrl.pathname);
// Set the local file path
const localFilePath = path.join(folderPath, filename);
// Download the file and save it locally
const file = fs.createWriteStream(localFilePath);
https.get(url, (response) => {
response.pipe(file);
file.on("finish", () => {
file.close();
console.log(`File downloaded and saved to: ${localFilePath}`);
});
});
});
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment