-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions for downloading all images and hashing filenames
- Loading branch information
Showing
7 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import axios from 'axios'; | ||
|
||
import { getImageFileName } from './getImageFileName.js'; | ||
import { IMAGE_FOLDER } from '../../constants.js'; | ||
|
||
const DELAY_MS = 1000; | ||
|
||
/** | ||
* Downloads all the images in the given set. | ||
* @param {Set} imageURLs - The set of image URLs to download. | ||
*/ | ||
async function downloadImages(imageURLs) { | ||
// Create directory | ||
if (!fs.existsSync(IMAGE_FOLDER)) { | ||
fs.mkdirSync(IMAGE_FOLDER); | ||
} | ||
let successful = 0; | ||
let failed = 0; | ||
for (const imageURL of imageURLs) { | ||
try { | ||
const fileName = getImageFileName(imageURL); | ||
await downloadImage(imageURL, IMAGE_FOLDER, fileName); | ||
await new Promise((resolve) => setTimeout(resolve, DELAY_MS)); | ||
successful++; | ||
} catch (error) { | ||
console.error(error); | ||
failed++; | ||
continue; | ||
} | ||
} | ||
console.log(`Successfully downloaded ${successful} images.`); | ||
console.log(`Failed to download ${failed} images.`); | ||
} | ||
|
||
/** | ||
* Downloads the image at the given URL and saves it to the given path. | ||
* @param {string} imageURL | ||
* @param {string} savePath | ||
* @param {string} fileName | ||
*/ | ||
async function downloadImage(imageURL, savePath, fileName) { | ||
// Check if path valid | ||
if (!fs.existsSync(savePath)) { | ||
throw new Error(`Invalid path: ${savePath}`); | ||
} | ||
// Check if valid URL | ||
try { | ||
new URL(imageURL); | ||
} catch (error) { | ||
console.error(`Invalid URL: ${imageURL}`); | ||
return; | ||
} | ||
|
||
const filePath = path.join(savePath, fileName); | ||
|
||
// Check if file already exists | ||
if (fs.existsSync(filePath)) { | ||
console.log(`File already exists: ${fileName}`); | ||
return; | ||
} | ||
// Download image | ||
console.log(`Downloading ${fileName}...`); | ||
const response = await axios.get(imageURL, { | ||
responseType: 'arraybuffer', | ||
}); | ||
const buffer = Buffer.from(response.data, 'binary'); | ||
|
||
// Save image | ||
fs.writeFileSync(filePath, buffer); | ||
console.log(`Saved ${fileName}`); | ||
} | ||
|
||
export { downloadImages }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createHash } from 'crypto'; | ||
|
||
/** | ||
* Hashes the image URL to get the image file name, preserving the file extension. | ||
* @param {string} imageURL | ||
*/ | ||
function getImageFileName(imageURL) { | ||
const hash = createHash('sha256'); | ||
hash.update(imageURL); | ||
const hashed = hash.digest('hex'); | ||
const extension = imageURL.split('.').pop() || ''; | ||
const allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'svg', 'webp']; | ||
if (!allowedExtensions.includes(extension)) { | ||
throw new Error(`Invalid extension: ${extension}`); | ||
} | ||
return `${hashed}.${extension}`; | ||
} | ||
|
||
export { getImageFileName }; |