feat: rehost book images

This commit is contained in:
Cory Dransfeldt 2024-05-11 15:35:24 -07:00
parent 2ecea9939b
commit 04aabd4b81
No known key found for this signature in database
5 changed files with 2304 additions and 458 deletions

5
.env
View file

@ -4,4 +4,7 @@ API_KEY_TRAKT=
API_KEY_MOVIEDB=
ACCOUNT_ID_PLEX=
SUPABASE_URL=
SUPABASE_KEY=
SUPABASE_KEY=
B2_ACCESS_KEY_ID=
B2_SECRET_ACCESS_KEY=
B2_BUCKET_NAME=

1804
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
{
"name": "coryd.dev",
"version": "14.1.3",
"version": "14.2.0",
"description": "The source for my personal site. Built using 11ty.",
"type": "module",
"scripts": {
@ -8,7 +8,8 @@
"start:quick": "eleventy --serve --incremental --ignore-initial",
"build": "ELEVENTY_PRODUCTION=true eleventy",
"build:ogi": "node ./scripts/og-images/index.js",
"deps:upgrade": "npm upgrade && ncu",
"update:books": "cd ./scripts/books && node index.js && cd ../../",
"update:deps": "npm upgrade && ncu",
"debug": "DEBUG=Eleventy* npx @11ty/eleventy --serve"
},
"keywords": [
@ -36,6 +37,7 @@
"@11ty/eleventy-plugin-rss": "^1.2.0",
"@11ty/eleventy-plugin-syntaxhighlight": "^5.0.0",
"@11tyrocks/eleventy-plugin-lightningcss": "^1.4.0",
"@aws-sdk/client-s3": "^3.574.0",
"@cdransf/eleventy-plugin-tabler-icons": "^1.3.0",
"@supabase/supabase-js": "^2.43.1",
"dotenv-flow": "^4.1.0",

51
scripts/books/index.js Normal file
View file

@ -0,0 +1,51 @@
import fs from 'fs'
import fetch from 'node-fetch'
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3'
import dotenv from 'dotenv';
dotenv.config({ path: new URL('../../.env.local', import.meta.url).pathname });
const s3Client = new S3Client({
credentials: {
accessKeyId: process.env.B2_ACCESS_KEY_ID,
secretAccessKey: process.env.B2_SECRET_ACCESS_KEY
},
endpoint: {
url: 'https://s3.us-west-001.backblazeb2.com',
},
region: 'us-west-1',
});
const booksData = fs.readFileSync('../../src/_data/json/read.json', 'utf8');
const books = JSON.parse(booksData);
async function processBooks() {
for (const book of books) {
if (book.thumbnail.startsWith('https://coryd.dev/media/books/')) continue
const cleanedThumbnailURL = book.thumbnail.replace('&edge=curl', '');
const fileName = `${book.isbn}-${book.title.replace(/\s+/g, '-').replace(/[^\w\-]+/g, '').replace(/\-{2,}/g, '-').replace(/^-+|-+$/g, '') .toLowerCase().replace(/\s+/g, '-')}.jpg`;
const fileKey = `books/${fileName}`
try {
const response = await fetch(cleanedThumbnailURL);
const buffer = await response.buffer();
const putObjectParams = {
Bucket: process.env.B2_BUCKET_NAME,
Key: fileKey,
Body: buffer,
ContentType: 'image/jpeg'
};
await s3Client.send(new PutObjectCommand(putObjectParams));
book.thumbnail = `https://coryd.dev/media/books/${fileName}`;
console.log(`Uploaded and updated ${book.title}`);
} catch (error) {
console.error(`Failed to process ${book.title}: ${error}`);
}
}
fs.writeFileSync('../../src/_data/json/read.json', JSON.stringify(books, null, 2));
}
processBooks();

File diff suppressed because it is too large Load diff