Redirect when photo can't be retrieved
This commit is contained in:
parent
47c9374a5a
commit
7f14b69605
@ -2,6 +2,7 @@ import PhotoForm from '@/photo/PhotoForm';
|
|||||||
import { convertPhotoToFormData } from '@/photo/form';
|
import { convertPhotoToFormData } from '@/photo/form';
|
||||||
import AdminChildPage from '@/components/AdminChildPage';
|
import AdminChildPage from '@/components/AdminChildPage';
|
||||||
import { getPhoto } from '@/services/postgres';
|
import { getPhoto } from '@/services/postgres';
|
||||||
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
export const runtime = 'edge';
|
export const runtime = 'edge';
|
||||||
|
|
||||||
@ -12,6 +13,8 @@ interface Props {
|
|||||||
export default async function PhotoPageEdit({ params: { photoId } }: Props) {
|
export default async function PhotoPageEdit({ params: { photoId } }: Props) {
|
||||||
const photo = await getPhoto(photoId);
|
const photo = await getPhoto(photoId);
|
||||||
|
|
||||||
|
if (!photo) { redirect('/admin'); }
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AdminChildPage>
|
<AdminChildPage>
|
||||||
<PhotoForm
|
<PhotoForm
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import { cc } from '@/utility/css';
|
|||||||
import { Metadata } from 'next';
|
import { Metadata } from 'next';
|
||||||
import { BASE_URL } from '@/site/config';
|
import { BASE_URL } from '@/site/config';
|
||||||
import { getPhoto, getPhotos } from '@/services/postgres';
|
import { getPhoto, getPhotos } from '@/services/postgres';
|
||||||
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
export const runtime = 'edge';
|
export const runtime = 'edge';
|
||||||
|
|
||||||
@ -20,9 +21,13 @@ export async function generateMetadata(
|
|||||||
{ params: { photoId } }: Props
|
{ params: { photoId } }: Props
|
||||||
): Promise<Metadata> {
|
): Promise<Metadata> {
|
||||||
const photo = await getPhoto(photoId);
|
const photo = await getPhoto(photoId);
|
||||||
|
|
||||||
|
if (!photo) { return {}; }
|
||||||
|
|
||||||
const title = photo.title;
|
const title = photo.title;
|
||||||
const description = ogImageDescriptionForPhoto(photo);
|
const description = ogImageDescriptionForPhoto(photo);
|
||||||
const images = ogImageUrlForPhoto(photo);
|
const images = ogImageUrlForPhoto(photo);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
title,
|
title,
|
||||||
description,
|
description,
|
||||||
@ -46,6 +51,9 @@ export default async function PhotoPage({
|
|||||||
children,
|
children,
|
||||||
}: Props) {
|
}: Props) {
|
||||||
const photo = await getPhoto(photoId);
|
const photo = await getPhoto(photoId);
|
||||||
|
|
||||||
|
if (!photo) { redirect('/'); }
|
||||||
|
|
||||||
const photos = await getPhotos();
|
const photos = await getPhotos();
|
||||||
|
|
||||||
return <>
|
return <>
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import PhotoModal from '@/photo/PhotoModal';
|
import PhotoModal from '@/photo/PhotoModal';
|
||||||
import { getPhoto } from '@/services/postgres';
|
import { getPhoto } from '@/services/postgres';
|
||||||
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
export const runtime = 'edge';
|
export const runtime = 'edge';
|
||||||
|
|
||||||
@ -9,5 +10,8 @@ interface Props {
|
|||||||
|
|
||||||
export default async function Share({ params: { photoId }}: Props) {
|
export default async function Share({ params: { photoId }}: Props) {
|
||||||
const photo = await getPhoto(photoId);
|
const photo = await getPhoto(photoId);
|
||||||
|
|
||||||
|
if (!photo) { return redirect('/'); }
|
||||||
|
|
||||||
return <PhotoModal photo={photo} />;
|
return <PhotoModal photo={photo} />;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,9 @@ import {
|
|||||||
PhotoDbInsert,
|
PhotoDbInsert,
|
||||||
translatePhotoId,
|
translatePhotoId,
|
||||||
parsePhotoFromDb,
|
parsePhotoFromDb,
|
||||||
|
Photo,
|
||||||
} from '@/photo';
|
} from '@/photo';
|
||||||
|
import { isValidUUID } from '@/utility/string';
|
||||||
|
|
||||||
const PHOTO_DEFAULT_LIMIT = 100;
|
const PHOTO_DEFAULT_LIMIT = 100;
|
||||||
|
|
||||||
@ -184,10 +186,12 @@ export const getPhotos = async (
|
|||||||
return photos;
|
return photos;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getPhoto = (id: string) =>
|
export const getPhoto = async (id: string): Promise<Photo | undefined> => {
|
||||||
sqlGetPhotoFromDb(
|
// Check for photo id forwarding
|
||||||
// Check for photo id forwarding
|
// and convert short ids to uuids
|
||||||
// and convert short ids to uuids
|
const photoId = translatePhotoId(id);
|
||||||
translatePhotoId(id)
|
return isValidUUID(photoId)
|
||||||
)
|
? sqlGetPhotoFromDb(photoId)
|
||||||
.then(photos => photos[0]);
|
.then(photos => photos.length > 0 ? photos[0] : undefined)
|
||||||
|
: Promise.resolve(undefined);
|
||||||
|
};
|
||||||
|
|||||||
2
src/utility/string.ts
Normal file
2
src/utility/string.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export const isValidUUID = (id: string): boolean =>
|
||||||
|
/^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$/i.test(id);
|
||||||
Loading…
Reference in New Issue
Block a user