Add page routing and content fetchers
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { createDirectusConnection } from "@/lib/directus";
|
||||
import { print } from "graphql";
|
||||
import getAlbums from '@/graphql/photos/getAlbums.graphql';
|
||||
import getAlbumItem from '@/graphql/photos/getAlbum.graphql';
|
||||
import { formatDate } from "@/lib/dates";
|
||||
|
||||
export async function getAllAlbums(settings: GlobalSettings): Promise<PhotoAlbum[]> {
|
||||
@@ -21,6 +22,7 @@ export async function getAllAlbums(settings: GlobalSettings): Promise<PhotoAlbum
|
||||
];
|
||||
|
||||
const album: PhotoAlbum = {
|
||||
type: "PhotoAlbum",
|
||||
title: albumRecord["title"],
|
||||
description: albumRecord["description"],
|
||||
url: albumRecord["url"],
|
||||
@@ -78,3 +80,76 @@ export async function getAllAlbums(settings: GlobalSettings): Promise<PhotoAlbum
|
||||
|
||||
return albums;
|
||||
}
|
||||
|
||||
export async function getAlbum(settings: GlobalSettings, route: string): Promise<PhotoAlbum> {
|
||||
const client = await createDirectusConnection();
|
||||
const result = await client.query(print(getAlbumItem), {
|
||||
route: route
|
||||
});
|
||||
|
||||
const albumRecord = result["Photo_Albums"][0];
|
||||
|
||||
let dates: string[] = [
|
||||
settings.website.lastModified.toISOString(),
|
||||
settings.photo.lastModified.toISOString(),
|
||||
albumRecord["date_created"],
|
||||
albumRecord["date_updated"],
|
||||
albumRecord["thumbnail"]["created_on"],
|
||||
];
|
||||
|
||||
const album: PhotoAlbum = {
|
||||
type: "PhotoAlbum",
|
||||
title: albumRecord["title"],
|
||||
description: albumRecord["description"],
|
||||
url: albumRecord["url"],
|
||||
startDate: albumRecord["start_date"],
|
||||
endDate: albumRecord["end_date"],
|
||||
location: albumRecord["location"],
|
||||
category: {
|
||||
id: albumRecord["category"][0]["Photo_Categories_id"]["id"],
|
||||
title: albumRecord["category"][0]["Photo_Categories_id"]["title"],
|
||||
url: albumRecord["category"][0]["Photo_Categories_id"]["url"],
|
||||
thumbnail: {
|
||||
url: albumRecord["category"][0]["Photo_Categories_id"]["thumbnail"]["filename_disk"],
|
||||
height: albumRecord["category"][0]["Photo_Categories_id"]["thumbnail"]["height"],
|
||||
width: albumRecord["category"][0]["Photo_Categories_id"]["thumbnail"]["width"]
|
||||
}
|
||||
},
|
||||
thumbnail: {
|
||||
url: albumRecord["thumbnail"]["filename_download"],
|
||||
height: albumRecord["thumbnail"]["height"],
|
||||
width: albumRecord["thumbnail"]["width"]
|
||||
},
|
||||
photos: [],
|
||||
lastModified: new Date()
|
||||
};
|
||||
|
||||
albumRecord["photos"].forEach((photoRecord: any) => {
|
||||
album.photos.push({
|
||||
id: photoRecord["id"],
|
||||
photo: {
|
||||
url: photoRecord["photo"]["filename_disk"],
|
||||
width: photoRecord["photo"]["width"],
|
||||
height: photoRecord["photo"]["height"]
|
||||
},
|
||||
text: photoRecord["text"]
|
||||
});
|
||||
|
||||
dates.push(photoRecord["date_created"]);
|
||||
dates.push(photoRecord["date_updated"]);
|
||||
dates.push(photoRecord["photo"]["created_on"]);
|
||||
});
|
||||
|
||||
if (dates.filter(e => e !== null).length === 0) {
|
||||
album.lastModified = new Date();
|
||||
}
|
||||
else {
|
||||
const sortedDates: string[] = dates.sort((a: string, b: string) => {
|
||||
return new Date(b).getTime() - new Date(a).getTime();
|
||||
});
|
||||
|
||||
album.lastModified = new Date(sortedDates[0]);
|
||||
}
|
||||
|
||||
return album;
|
||||
}
|
||||
|
||||
@@ -22,4 +22,4 @@ export async function getAllCategories(settings: GlobalSettings): Promise<PhotoA
|
||||
});
|
||||
|
||||
return categories;
|
||||
}
|
||||
}
|
||||
|
||||
30
astro/src/content/photos/photos.ts
Normal file
30
astro/src/content/photos/photos.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { createDirectusConnection } from "@/lib/directus";
|
||||
import { print } from "graphql";
|
||||
import getPhotos from '@/graphql/photos/getPhotos.graphql';
|
||||
import md5 from "md5";
|
||||
|
||||
export async function getPhotoFromHash(albumUrl: string, hash: string): Promise<PhotoAlbumPhoto | null> {
|
||||
const client = await createDirectusConnection();
|
||||
const result = await client.query(print(getPhotos));
|
||||
|
||||
result["Photo_Albums"][0]["photos"].forEach((photo: any) => {
|
||||
const hashObject = md5(JSON.stringify({
|
||||
id: photo.id,
|
||||
url: photo.photo.url,
|
||||
width: photo.photo.width,
|
||||
height: photo.photo.height
|
||||
}));
|
||||
|
||||
if (hash.substring(hash.length - 10) === hash) {
|
||||
return {
|
||||
id: photo.id,
|
||||
text: photo.text,
|
||||
photo: {
|
||||
url: photo.photo.url,
|
||||
width: photo.photo.width,
|
||||
height: photo.photo.height
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user