Move to drizzle
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { shake } from "radash";
|
||||
import PhotoDataSource from "@/data-source";
|
||||
import { Photo } from "@/entity/photo";
|
||||
import db from "@/db/db";
|
||||
import { photosTable } from "@/db/schema/photo";
|
||||
|
||||
export type ImageData = {
|
||||
width: number,
|
||||
@@ -29,9 +29,7 @@ export type GetPhotos = {
|
||||
}
|
||||
|
||||
export async function GET(): Promise<Response> {
|
||||
const dataSource = await PhotoDataSource.dataSource;
|
||||
const photoRepository = dataSource.getRepository(Photo);
|
||||
const currentSources = await photoRepository.find();
|
||||
const currentSources = await db.select().from(photosTable);
|
||||
const images = currentSources.map((photo) => {
|
||||
return {
|
||||
width: photo.width,
|
||||
|
||||
@@ -4,8 +4,8 @@ import { NextResponse } from "next/server";
|
||||
import { diff, sift } from "radash";
|
||||
import sharp from "sharp";
|
||||
|
||||
import PhotoDataSource from "@/data-source";
|
||||
import { Photo } from "@/entity/photo";
|
||||
import db from "@/db/db";
|
||||
import { photosTable } from "@/db/schema/photo";
|
||||
import { auth } from "@/lib/auth";
|
||||
|
||||
export type GetPhotosUpdate = {
|
||||
@@ -17,16 +17,13 @@ export const GET = auth(async function GET(req): Promise<Response> {
|
||||
if (!req.auth) {
|
||||
return NextResponse.json({ message: "Not authenticated" }, { status: 401 });
|
||||
}
|
||||
const photos = await db.select().from(photosTable);
|
||||
const currentSources = photos.map((photo) => photo.src);
|
||||
|
||||
const dataSource = await PhotoDataSource.dataSource;
|
||||
const photoRepository = dataSource.getRepository(Photo);
|
||||
const currentSources = (await photoRepository.find({
|
||||
select: {
|
||||
src: true
|
||||
}
|
||||
})).map((photo) => photo.src);
|
||||
|
||||
const s3Client = new S3Client();
|
||||
const s3Client = new S3Client({
|
||||
region: "auto",
|
||||
endpoint: `https://fly.storage.tigris.dev`,
|
||||
});
|
||||
|
||||
const listObjCmd = new ListObjectsV2Command({
|
||||
Bucket: "joemonk-photos"
|
||||
@@ -47,6 +44,10 @@ export const GET = auth(async function GET(req): Promise<Response> {
|
||||
|
||||
const newPhotos = diff(s3Photos, currentSources);
|
||||
|
||||
if (newPhotos.length === 0) {
|
||||
return NextResponse.json<GetPhotosUpdate>({ status: 200, s3Photos: newPhotos });
|
||||
}
|
||||
|
||||
const imageData = newPhotos.map(async (fileName: string) => {
|
||||
const getImageCmd = new GetObjectCommand({
|
||||
Bucket: "joemonk-photos",
|
||||
@@ -61,26 +62,27 @@ export const GET = auth(async function GET(req): Promise<Response> {
|
||||
.toBuffer();
|
||||
const exifData = exif ? exifReader(exif) : undefined;
|
||||
|
||||
const photo = new Photo();
|
||||
photo.src = fileName;
|
||||
photo.width = width ?? 10;
|
||||
photo.height = height ?? 10;
|
||||
photo.blur = `data:image/jpeg;base64,${blur.toString('base64')}` as `data:image/${string}`;
|
||||
photo.camera = exifData?.Image?.Model ?? null;
|
||||
const photo: typeof photosTable.$inferInsert = {
|
||||
src: fileName,
|
||||
width: width ?? 10,
|
||||
height: height ?? 10,
|
||||
blur: `data:image/jpeg;base64,${blur.toString('base64')}` as `data:image/${string}`,
|
||||
camera: exifData?.Image?.Model ?? null,
|
||||
|
||||
photo.exposureBiasValue = exifData?.Photo?.ExposureBiasValue ?? null;
|
||||
photo.fNumber = exifData?.Photo?.FNumber ?? null;
|
||||
photo.isoSpeedRatings = exifData?.Photo?.ISOSpeedRatings ?? null;
|
||||
photo.focalLength = exifData?.Photo?.FocalLength ?? null;
|
||||
photo.dateTimeOriginal = exifData?.Photo?.DateTimeOriginal ?? null;
|
||||
photo.lensModel = exifData?.Photo?.LensModel ?? null;
|
||||
exposureBiasValue: exifData?.Photo?.ExposureBiasValue ?? null,
|
||||
fNumber: exifData?.Photo?.FNumber ?? null,
|
||||
isoSpeedRatings: exifData?.Photo?.ISOSpeedRatings ?? null,
|
||||
focalLength: exifData?.Photo?.FocalLength ?? null,
|
||||
dateTimeOriginal: exifData?.Photo?.DateTimeOriginal ?? null,
|
||||
lensModel: exifData?.Photo?.LensModel ?? null,
|
||||
};
|
||||
|
||||
return photo;
|
||||
});
|
||||
|
||||
const images = await Promise.all(imageData);
|
||||
|
||||
await photoRepository.save(images);
|
||||
await db.insert(photosTable).values(images);
|
||||
|
||||
return NextResponse.json<GetPhotosUpdate>({ status: 200, s3Photos: newPhotos });
|
||||
});
|
||||
Reference in New Issue
Block a user