Skip to content

Commit

Permalink
stable images
Browse files Browse the repository at this point in the history
  • Loading branch information
davay42 committed Oct 3, 2023
1 parent 3cb28da commit b2eac15
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 35 deletions.
3 changes: 2 additions & 1 deletion components/SynthCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import SynthFav from './SynthFav.vue';
const props = defineProps({
off: { type: Boolean, default: false },
title: { type: String, default: '' },
slug: { type: String, default: '' },
description: { type: String, default: '' },
cover: { type: String, default: '' },
url: { type: String, default: '' },
Expand Down Expand Up @@ -50,7 +51,7 @@ button.flex.flex-col.text-left.relative.min-h-50.card.p-0.bg-light-300.shadow-lg
:key="title"
height="200"
width="1000"
:src="`/cover/${title.toLowerCase().split(' ').join('-')}.webp`"
:src="`/cover/${slug}.webp`"
:alt="`${title} illustration`")
.flex-1
.p-4.flex.items-center.justify-between.w-full
Expand Down
2 changes: 1 addition & 1 deletion components/SynthList.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup>
import { computed, onMounted, ref } from 'vue';
import { useForm } from "../composables/useForm.js";
import { data } from "../synths.data";
import { data } from "../db/synths.data";
import SynthCard from "./SynthCard.vue";
import { SlickList, SlickItem } from "vue-slicksort";
Expand Down
38 changes: 38 additions & 0 deletions db/downloadCovers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import download from 'image-downloader'

import path from "node:path";
import { fileURLToPath } from "node:url";
import fs from 'node:fs'


export async function downloadCovers(records = [], {
field = 'cover',
folder = '',
query = 'quality=70&width=1000&format=webp'
} = options) {

const dirname = path.dirname(fileURLToPath(import.meta.url));
let dest = path.resolve(dirname, '../public/', folder)
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}
const urls = []

for (let r of records) {
if (!r?.[field]) continue
let filePath = path.resolve(dest, `${r.slug}.webp`)
if (fs.existsSync(filePath)) continue
let url = `https://db.chromatone.center/assets/${r[field]}?${query}&download`
urls.push({ url, slug: r.slug, dest: filePath })
}

const chunkSize = 5;
for (let i = 0; i < urls.length; i += chunkSize) {
const chunk = urls.slice(i, i + chunkSize);
await Promise.all(chunk.map(rec => {
console.log('downloading file:', rec.slug + '.webp')
return download.image(rec)
}));
}

}
17 changes: 17 additions & 0 deletions db/synths.data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useItems } from '../composables/useItems.js';
import { downloadCovers } from './downloadCovers.js';

export default {
async load() {

const records = await useItems('synths', {
sort: ['sort', 'id'], limit: -1, filter: { status: { '_eq': 'published' } }
});

downloadCovers(records, {
folder: 'cover'
})

return records
}
}
Empty file added public/.nojekyll
Empty file.
33 changes: 0 additions & 33 deletions synths.data.js

This file was deleted.

0 comments on commit b2eac15

Please sign in to comment.