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 });
|
||||
});
|
||||
@@ -1,32 +0,0 @@
|
||||
import { DataSource } from "typeorm";
|
||||
import { Photo } from "./entity/photo";
|
||||
|
||||
const dataSource = new DataSource({
|
||||
type: "better-sqlite3",
|
||||
database: `${process.cwd()}/db.sql`,
|
||||
entities: [Photo],
|
||||
migrations: ["./migrations"],
|
||||
});
|
||||
|
||||
export default class PhotoDataSource {
|
||||
private static _dataSource: DataSource | null = null;
|
||||
|
||||
static get dataSource(): Promise<DataSource> {
|
||||
if (PhotoDataSource._dataSource === null) {
|
||||
return PhotoDataSource.initDataSource();
|
||||
} else {
|
||||
return Promise.resolve(PhotoDataSource._dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
static async initDataSource(): Promise<DataSource> {
|
||||
if (!PhotoDataSource._dataSource || !PhotoDataSource._dataSource.isInitialized) {
|
||||
const ds = await dataSource.initialize();
|
||||
console.log('Photo data source initialized');
|
||||
PhotoDataSource._dataSource = ds;
|
||||
}
|
||||
return PhotoDataSource._dataSource;
|
||||
}
|
||||
}
|
||||
|
||||
await PhotoDataSource.initDataSource();
|
||||
3
src/db/db.ts
Normal file
3
src/db/db.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { drizzle } from 'drizzle-orm/better-sqlite3';
|
||||
|
||||
export default drizzle(`${process.cwd()}/db.sql`);
|
||||
22
src/db/schema/photo.ts
Normal file
22
src/db/schema/photo.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { int, sqliteTable, text, blob, real } from "drizzle-orm/sqlite-core";
|
||||
|
||||
export const photosTable = sqliteTable(
|
||||
"photo",
|
||||
{
|
||||
id: int().primaryKey({ autoIncrement: true }),
|
||||
src: text().notNull().unique(),
|
||||
width: int().notNull(),
|
||||
height: int().notNull(),
|
||||
blur: blob().notNull(),
|
||||
|
||||
camera: text(),
|
||||
title: text(),
|
||||
description: text(),
|
||||
exposureBiasValue: int(),
|
||||
fNumber: real(),
|
||||
isoSpeedRatings: int(),
|
||||
focalLength: int(),
|
||||
dateTimeOriginal: int({ mode: 'timestamp' }),
|
||||
lensModel: text(),
|
||||
}
|
||||
);
|
||||
@@ -1,48 +0,0 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
||||
|
||||
@Entity()
|
||||
export class Photo {
|
||||
@PrimaryGeneratedColumn()
|
||||
id!: number;
|
||||
|
||||
@Column("text", { unique: true })
|
||||
src!: string;
|
||||
|
||||
@Column()
|
||||
width!: number;
|
||||
|
||||
@Column()
|
||||
height!: number;
|
||||
|
||||
@Column("blob")
|
||||
blur!: string;
|
||||
|
||||
@Column("text", { nullable: true })
|
||||
camera: string | null = null;
|
||||
|
||||
// Manually input data
|
||||
@Column("text", { nullable: true })
|
||||
title: string | null = null;
|
||||
|
||||
@Column("text", { nullable: true })
|
||||
description: string | null = null;
|
||||
|
||||
// Exif data
|
||||
@Column("int", { nullable: true })
|
||||
exposureBiasValue: number | null = null;
|
||||
|
||||
@Column("float", { nullable: true })
|
||||
fNumber: number | null = null;
|
||||
|
||||
@Column("int", { nullable: true })
|
||||
isoSpeedRatings: number | null = null;
|
||||
|
||||
@Column("int", { nullable: true })
|
||||
focalLength: number | null = null;
|
||||
|
||||
@Column("date", { nullable: true })
|
||||
dateTimeOriginal: Date | null = null;
|
||||
|
||||
@Column("text", { nullable: true })
|
||||
lensModel: string | null = null;
|
||||
}
|
||||
Reference in New Issue
Block a user