36 lines
1.1 KiB
JavaScript
36 lines
1.1 KiB
JavaScript
|
import { createDirectus, staticToken, rest, readFiles } from '@directus/sdk';
|
||
|
import fs from 'fs';
|
||
|
|
||
|
export async function crawlImages(routes) {
|
||
|
const client = createDirectus(process.env.DIRECTUS_URL)
|
||
|
.with(staticToken(process.env.DIRECTUS_API_TOKEN))
|
||
|
.with(rest());
|
||
|
|
||
|
const directusFiles = await client.request(
|
||
|
readFiles({
|
||
|
query: {
|
||
|
filter: {
|
||
|
type: {
|
||
|
_eq: 'image/',
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
);
|
||
|
|
||
|
for (let image of directusFiles) {
|
||
|
if (image.type != "image/heic") {
|
||
|
const fileExists = async (filePath) => !!(await fs.promises.access(filePath, fs.constants.F_OK).then(() => true).catch(() => false));
|
||
|
|
||
|
const filePath = `./public/api/assets/${image.id}.webp`;
|
||
|
fileExists(filePath)
|
||
|
.then(exists => {
|
||
|
if (!exists) {
|
||
|
routes.add(`/api/assets/${image.id}.webp`);
|
||
|
}
|
||
|
})
|
||
|
.catch(error => console.error('Error:', error));
|
||
|
}
|
||
|
}
|
||
|
}
|