Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ out/
next-env.d.ts
test-results/
playwright-report/
public/uploads/
1 change: 1 addition & 0 deletions app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ export default function AdminPage() {
{showRelForm && (
<div className="bg-white/5 border border-white/10 rounded-xl p-6">
<AdminRelForm
isAdmin
onSave={handleRelSave}
onCancel={() => setShowRelForm(false)}
/>
Expand Down
50 changes: 28 additions & 22 deletions app/api/upload/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NextRequest, NextResponse } from 'next/server';
import { Storage } from '@google-cloud/storage';
import sharp from 'sharp';
import { getCurrentUser } from '@/lib/auth';
import path from 'path';
import fs from 'fs/promises';

const ALLOWED_TYPES = new Set([
'image/jpeg',
Expand All @@ -11,21 +12,33 @@ const ALLOWED_TYPES = new Set([
]);
const MAX_SIZE = 5 * 1024 * 1024; // 5 MB

const storage = new Storage({
projectId: process.env.GCS_PROJECT_ID,
});
const useGCS = !!process.env.GCS_BUCKET_NAME;

function getBucket() {
const bucketName = process.env.GCS_BUCKET_NAME;
if (!bucketName) {
throw new Error('GCS_BUCKET_NAME environment variable is not set');
}
return storage.bucket(bucketName);
async function uploadToGCS(resized: Buffer, filename: string): Promise<string> {
const { Storage } = await import('@google-cloud/storage');
const storage = new Storage({ projectId: process.env.GCS_PROJECT_ID });
const bucket = storage.bucket(process.env.GCS_BUCKET_NAME!);
const blob = bucket.file(filename);
await blob.save(resized, {
contentType: 'image/webp',
metadata: {
cacheControl: 'public, max-age=31536000, immutable',
},
});
return `https://storage.googleapis.com/${bucket.name}/${filename}`;
}

async function uploadToLocal(resized: Buffer, filename: string): Promise<string> {
const uploadDir = path.join(process.cwd(), 'public', 'uploads', 'entities');
await fs.mkdir(uploadDir, { recursive: true });
const filePath = path.join(uploadDir, path.basename(filename));
await fs.writeFile(filePath, resized);
return `/uploads/entities/${path.basename(filename)}`;
}

export async function POST(request: NextRequest) {
const user = await getCurrentUser();
if (!user?.is_admin) {
if (!user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}

Expand Down Expand Up @@ -68,18 +81,11 @@ export async function POST(request: NextRequest) {
const random = Math.random().toString(36).slice(2, 10);
const filename = `entities/${timestamp}-${random}.webp`;

// Upload to GCS
const bucket = getBucket();
const blob = bucket.file(filename);
await blob.save(resized, {
contentType: 'image/webp',
metadata: {
cacheControl: 'public, max-age=31536000, immutable',
},
});
// Upload to GCS in production, local filesystem in development
const url = useGCS
? await uploadToGCS(resized, filename)
: await uploadToLocal(resized, filename);

// Public access is managed at the bucket level (uniform bucket-level access)
const url = `https://storage.googleapis.com/${bucket.name}/${filename}`;
return NextResponse.json({ url });
} catch (err: unknown) {
console.error('Upload error:', err);
Expand Down
4 changes: 4 additions & 0 deletions db/migrations/012_add_created_rel_type.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Add 'created' relationship type: person/org/project → project
INSERT INTO rel_types (key, forward_label, reverse_label, source_types, target_types)
VALUES ('created', 'Created', 'Created by', '{person,organization,project}', '{project}')
ON CONFLICT (key) DO NOTHING;
17 changes: 12 additions & 5 deletions db/seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ INSERT INTO rel_types (key, forward_label, reverse_label, source_types, target_t
('contributes_to', 'Contributes to', 'Contributor', '{person}', '{project}'),
('hosted_at', 'Hosted at', 'Hosts', '{event}', '{venue}'),
('tagged_with', 'Tagged with', 'Tags', '{person,organization,event,venue,project}', '{topic}'),
('spoke_at', 'Spoke at', 'Speaker', '{person}', '{event}');
('spoke_at', 'Spoke at', 'Speaker', '{person}', '{event}'),
('created', 'Created', 'Created by', '{person,organization,project}', '{project}');

DO $$
DECLARE
Expand Down Expand Up @@ -376,10 +377,16 @@ BEGIN

-- Project contributions
INSERT INTO relationships (source_id, target_id, rel_type, label) VALUES
(v_scott, v_jira, 'contributes_to', 'Creator'),
(v_mike, v_jira, 'contributes_to', 'Creator'),
(v_melanie, v_canva_tool, 'contributes_to', 'Creator'),
(v_nicholas, v_envato_market, 'contributes_to', 'Creator');
(v_scott, v_jira, 'contributes_to', null),
(v_mike, v_jira, 'contributes_to', null),
(v_melanie, v_canva_tool, 'contributes_to', null),
(v_nicholas, v_envato_market, 'contributes_to', null);

-- Project creation (org created project)
INSERT INTO relationships (source_id, target_id, rel_type, label) VALUES
(v_atlassian, v_jira, 'created', null),
(v_canva, v_canva_tool, 'created', null),
(v_envato, v_envato_market, 'created', null);

-- Topic tags
INSERT INTO relationships (source_id, target_id, rel_type, label) VALUES
Expand Down
2 changes: 1 addition & 1 deletion lib/config/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const ADMIN_ONLY_ENTITY_TYPES: EntityType[] = ['person', 'topic'];
// creation via AUTO_RELATIONSHIP_MAP in schema.ts — that's safe because
// the user just created the entity.

export const EDIT_GRANTING_REL_TYPES: string[] = ['founded', 'organized', 'manages'];
export const EDIT_GRANTING_REL_TYPES: string[] = ['founded', 'organized', 'manages', 'created'];

// Relationship types that only admins can create via the API.
// Currently identical to EDIT_GRANTING_REL_TYPES. Split into a separate
Expand Down
1 change: 1 addition & 0 deletions lib/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const REL_TYPE_DEFINITIONS: RelTypeDefinition[] = [
{ key: 'hosted_at', forwardLabel: 'Hosted at', reverseLabel: 'Hosts', sourceTypes: ['event'], targetTypes: ['venue'] },
{ key: 'tagged_with', forwardLabel: 'Tagged with', reverseLabel: 'Tags', sourceTypes: ['person', 'organization', 'event', 'venue', 'project'], targetTypes: ['topic'] },
{ key: 'spoke_at', forwardLabel: 'Spoke at', reverseLabel: 'Speaker', sourceTypes: ['person'], targetTypes: ['event'] },
{ key: 'created', forwardLabel: 'Created', reverseLabel: 'Created by', sourceTypes: ['person', 'organization', 'project'], targetTypes: ['project'] },
];

/** All valid relationship type keys */
Expand Down