-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benefit: in the meantime, easier to process and optimise on the flight
- Loading branch information
Showing
10 changed files
with
4,584 additions
and
3,592 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
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,90 @@ | ||
import { parseArgs } from "node:util"; | ||
import { readdirSync } from "node:fs"; | ||
import { dirname, join } from "node:path"; | ||
import { fileURLToPath } from "node:url"; | ||
import cloudinary from "cloudinary"; | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
|
||
const { | ||
values: { key, secret }, | ||
} = parseArgs({ | ||
options: { | ||
key: { | ||
type: "string", | ||
}, | ||
secret: { | ||
type: "string", | ||
}, | ||
}, | ||
}); | ||
|
||
const CLOUD_NAME = "beraliv"; | ||
const CLOUDINARY_FOLDER = "beraliv_com"; | ||
const LOCAL_FOLDER = join(__dirname, "../src/components/images"); | ||
|
||
// Configure Cloudinary with your API credentials | ||
cloudinary.v2.config({ | ||
cloud_name: CLOUD_NAME, | ||
api_key: key, | ||
api_secret: secret, | ||
}); | ||
|
||
/** | ||
* @param {string} filename | ||
* @param {string} imageUrl | ||
*/ | ||
async function uploadToCloudinary(imageNameWithExtension) { | ||
const [imageName] = imageNameWithExtension.split("."); | ||
|
||
const imageUrl = `${LOCAL_FOLDER}/${imageNameWithExtension}`; | ||
|
||
try { | ||
if (imageName.includes(" ")) { | ||
console.error(`Invalid image name ❌: remove a space from ${imageName}`); | ||
return; | ||
} | ||
|
||
await cloudinary.v2.uploader.upload(imageUrl, { | ||
public_id: imageName, | ||
folder: CLOUDINARY_FOLDER, | ||
}); | ||
|
||
console.error(`Image is uploaded ✅: ${imageNameWithExtension}`); | ||
} catch (error) { | ||
console.error( | ||
`Error uploading image ❌:\n` + | ||
` Error: ${error.message}\n` + | ||
` Name: ${imageName}` | ||
); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
async function getImages() { | ||
try { | ||
const files = readdirSync(LOCAL_FOLDER); | ||
return files; | ||
} catch (error) { | ||
return []; | ||
} | ||
} | ||
|
||
async function main() { | ||
const images = await getImages(); | ||
const uploads = []; | ||
for (const image of images) { | ||
uploads.push(uploadToCloudinary(image)); | ||
} | ||
try { | ||
await Promise.all(uploads); | ||
|
||
console.error(`Images are uploaded ✅`); | ||
process.exit(0); | ||
} catch (error) { | ||
console.error(`Image are failed to upload ❌`); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
main(); |
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,12 @@ | ||
import { CLOUDINARY_FOLDER } from "../constants/CLOUDINARY_FOLDER"; | ||
|
||
interface ImageProps { | ||
alt: string; | ||
className?: string; | ||
name: string; | ||
} | ||
|
||
export const image = ({ alt, className, name }: ImageProps) => | ||
`<img src="https://res.cloudinary.com/beraliv/image/upload/v${msToSeconds( | ||
Date.now() | ||
)}/${CLOUDINARY_FOLDER}/${name}" class="${className}" alt="${alt}" />`; |
File renamed without changes
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 @@ | ||
export const CLOUDINARY_FOLDER = "beraliv_com"; |
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 @@ | ||
export const msToSeconds = (ms: number): number => Math.floor(ms / 1_000); |
Oops, something went wrong.