46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
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<PhotoAlbumItem | null> {
|
|
const client = await createDirectusConnection();
|
|
const result = await client.query(print(getPhotos), {
|
|
albumUrl: albumUrl
|
|
});
|
|
|
|
let object: PhotoAlbumItem | null = null;
|
|
|
|
result["Photo_Albums"][0]["photos"].forEach((photo: any) => {
|
|
/*
|
|
* I have decided to not put the getImageSize here, it can mess up the
|
|
* hashing, or anything else. It seems smarter to do this in the photo's and galleries.
|
|
*/
|
|
|
|
const hashObject = md5(JSON.stringify({
|
|
id: photo.id,
|
|
url: photo.photo.filename_disk,
|
|
width: photo.photo.width,
|
|
height: photo.photo.height
|
|
}));
|
|
|
|
if (hashObject.substring(hashObject.length - 10) === hash) {
|
|
object = {
|
|
id: photo.id,
|
|
text: photo.text,
|
|
photo: {
|
|
url: photo.photo.filename_disk,
|
|
width: photo.photo.width,
|
|
height: photo.photo.height
|
|
},
|
|
album: {
|
|
url: result["Photo_Albums"][0].url,
|
|
title: result["Photo_Albums"][0].title
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
return object;
|
|
}
|