feat:add Finds feature with media upload and R2 support
This commit is contained in:
131
src/routes/api/finds/+server.ts
Normal file
131
src/routes/api/finds/+server.ts
Normal file
@@ -0,0 +1,131 @@
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { db } from '$lib/server/db';
|
||||
import { find, findMedia, user } from '$lib/server/db/schema';
|
||||
import { eq, and, sql } from 'drizzle-orm';
|
||||
import { encodeBase64url } from '@oslojs/encoding';
|
||||
|
||||
function generateFindId(): string {
|
||||
const bytes = crypto.getRandomValues(new Uint8Array(15));
|
||||
return encodeBase64url(bytes);
|
||||
}
|
||||
|
||||
export const GET: RequestHandler = async ({ url, locals }) => {
|
||||
if (!locals.user) {
|
||||
throw error(401, 'Unauthorized');
|
||||
}
|
||||
|
||||
const lat = url.searchParams.get('lat');
|
||||
const lng = url.searchParams.get('lng');
|
||||
const radius = url.searchParams.get('radius') || '10';
|
||||
|
||||
if (!lat || !lng) {
|
||||
throw error(400, 'Latitude and longitude are required');
|
||||
}
|
||||
|
||||
// Query finds within radius (simplified - in production use PostGIS)
|
||||
// For MVP, using a simple bounding box approach
|
||||
const latNum = parseFloat(lat);
|
||||
const lngNum = parseFloat(lng);
|
||||
const radiusNum = parseFloat(radius);
|
||||
|
||||
// Rough conversion: 1 degree ≈ 111km
|
||||
const latDelta = radiusNum / 111;
|
||||
const lngDelta = radiusNum / (111 * Math.cos((latNum * Math.PI) / 180));
|
||||
|
||||
const finds = await db
|
||||
.select({
|
||||
find: find,
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username
|
||||
}
|
||||
})
|
||||
.from(find)
|
||||
.innerJoin(user, eq(find.userId, user.id))
|
||||
.where(
|
||||
and(
|
||||
eq(find.isPublic, 1),
|
||||
// Simple bounding box filter
|
||||
sql`CAST(${find.latitude} AS NUMERIC) BETWEEN ${latNum - latDelta} AND ${latNum + latDelta}`,
|
||||
sql`CAST(${find.longitude} AS NUMERIC) BETWEEN ${lngNum - lngDelta} AND ${lngNum + lngDelta}`
|
||||
)
|
||||
)
|
||||
.orderBy(find.createdAt);
|
||||
|
||||
// Get media for each find
|
||||
const findsWithMedia = await Promise.all(
|
||||
finds.map(async (item) => {
|
||||
const media = await db
|
||||
.select()
|
||||
.from(findMedia)
|
||||
.where(eq(findMedia.findId, item.find.id))
|
||||
.orderBy(findMedia.orderIndex);
|
||||
|
||||
return {
|
||||
...item.find,
|
||||
user: item.user,
|
||||
media: media
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return json(findsWithMedia);
|
||||
};
|
||||
|
||||
export const POST: RequestHandler = async ({ request, locals }) => {
|
||||
if (!locals.user) {
|
||||
throw error(401, 'Unauthorized');
|
||||
}
|
||||
|
||||
const data = await request.json();
|
||||
const { title, description, latitude, longitude, locationName, category, isPublic, media } = data;
|
||||
|
||||
if (!title || !latitude || !longitude) {
|
||||
throw error(400, 'Title, latitude, and longitude are required');
|
||||
}
|
||||
|
||||
if (title.length > 100) {
|
||||
throw error(400, 'Title must be 100 characters or less');
|
||||
}
|
||||
|
||||
if (description && description.length > 500) {
|
||||
throw error(400, 'Description must be 500 characters or less');
|
||||
}
|
||||
|
||||
const findId = generateFindId();
|
||||
|
||||
// Create find
|
||||
const newFind = await db
|
||||
.insert(find)
|
||||
.values({
|
||||
id: findId,
|
||||
userId: locals.user.id,
|
||||
title,
|
||||
description,
|
||||
latitude: latitude.toString(),
|
||||
longitude: longitude.toString(),
|
||||
locationName,
|
||||
category,
|
||||
isPublic: isPublic ? 1 : 0
|
||||
})
|
||||
.returning();
|
||||
|
||||
// Create media records if provided
|
||||
if (media && media.length > 0) {
|
||||
const mediaRecords = media.map(
|
||||
(item: { type: string; url: string; thumbnailUrl?: string }, index: number) => ({
|
||||
id: generateFindId(),
|
||||
findId,
|
||||
type: item.type,
|
||||
url: item.url,
|
||||
thumbnailUrl: item.thumbnailUrl,
|
||||
orderIndex: index
|
||||
})
|
||||
);
|
||||
|
||||
await db.insert(findMedia).values(mediaRecords);
|
||||
}
|
||||
|
||||
return json({ success: true, find: newFind[0] });
|
||||
};
|
||||
52
src/routes/api/finds/upload/+server.ts
Normal file
52
src/routes/api/finds/upload/+server.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { processAndUploadImage, processAndUploadVideo } from '$lib/server/media-processor';
|
||||
import { encodeBase64url } from '@oslojs/encoding';
|
||||
|
||||
const MAX_FILE_SIZE = 100 * 1024 * 1024; // 100MB
|
||||
const MAX_FILES = 5;
|
||||
const ALLOWED_IMAGE_TYPES = ['image/jpeg', 'image/png', 'image/webp'];
|
||||
const ALLOWED_VIDEO_TYPES = ['video/mp4', 'video/quicktime'];
|
||||
|
||||
function generateFindId(): string {
|
||||
const bytes = crypto.getRandomValues(new Uint8Array(15));
|
||||
return encodeBase64url(bytes);
|
||||
}
|
||||
|
||||
export const POST: RequestHandler = async ({ request, locals }) => {
|
||||
if (!locals.user) {
|
||||
throw error(401, 'Unauthorized');
|
||||
}
|
||||
|
||||
const formData = await request.formData();
|
||||
const files = formData.getAll('files') as File[];
|
||||
const findId = (formData.get('findId') as string) || generateFindId(); // Generate if creating new Find
|
||||
|
||||
if (files.length === 0 || files.length > MAX_FILES) {
|
||||
throw error(400, `Must upload between 1 and ${MAX_FILES} files`);
|
||||
}
|
||||
|
||||
const uploadedMedia: Array<{ type: string; url: string; thumbnailUrl: string }> = [];
|
||||
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
|
||||
// Validate file size
|
||||
if (file.size > MAX_FILE_SIZE) {
|
||||
throw error(400, `File ${file.name} exceeds maximum size of 100MB`);
|
||||
}
|
||||
|
||||
// Process based on type
|
||||
if (ALLOWED_IMAGE_TYPES.includes(file.type)) {
|
||||
const result = await processAndUploadImage(file, findId, i);
|
||||
uploadedMedia.push({ type: 'photo', ...result });
|
||||
} else if (ALLOWED_VIDEO_TYPES.includes(file.type)) {
|
||||
const result = await processAndUploadVideo(file, findId, i);
|
||||
uploadedMedia.push({ type: 'video', ...result });
|
||||
} else {
|
||||
throw error(400, `File type ${file.type} not allowed`);
|
||||
}
|
||||
}
|
||||
|
||||
return json({ findId, media: uploadedMedia });
|
||||
};
|
||||
Reference in New Issue
Block a user