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
45 changes: 36 additions & 9 deletions src/app/api/files/[fileId]/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,24 @@ export async function HEAD(request, { params } = {}) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}

const userId = user.id;
const { fileId } = await resolveRouteParams(params);
const normalizedFileId = normalizeFileId(fileId);
if (!normalizedFileId) {
return missingFileIdResponse();
}

const { data: internalUser, error: userError } = await supabase
.from('users')
.select('id')
.eq('auth_user_id', user.id)
.single();

if (userError || !internalUser) {
return NextResponse.json({ error: 'User profile not found' }, { status: 404 });
}

const userId = internalUser.id;

// Get file metadata from database
const { data: fileData, error: fileError } = await supabase
.from('encrypted_files')
Expand All @@ -189,13 +200,15 @@ export async function HEAD(request, { params } = {}) {
created_at,
messages!inner(
conversation_id,
conversation_participants!inner(
user_id
conversations!inner(
conversation_participants!inner(
user_id
)
)
)
`)
.eq('id', normalizedFileId)
.eq('messages.conversation_participants.user_id', userId)
.eq('messages.conversations.conversation_participants.user_id', userId)
.single();

if (fileError || !fileData) {
Expand Down Expand Up @@ -231,13 +244,25 @@ export async function POST(request, { params } = {}) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}

const userId = user.id;
const { fileId } = await resolveRouteParams(params);
const normalizedFileId = normalizeFileId(fileId);
if (!normalizedFileId) {
return missingFileIdResponse();
}

const authUserId = user.id;
const { data: internalUser, error: userError } = await supabase
.from('users')
.select('id')
.eq('auth_user_id', authUserId)
.single();

if (userError || !internalUser) {
return NextResponse.json({ error: 'User profile not found' }, { status: 404 });
}

const userId = internalUser.id;

console.log(`📁 [FILE-INFO] Info request from user: ${userId} for file: ${fileId}`);

// Get file metadata from database
Expand All @@ -252,13 +277,15 @@ export async function POST(request, { params } = {}) {
messages!inner(
id,
conversation_id,
conversation_participants!inner(
user_id
conversations!inner(
conversation_participants!inner(
user_id
)
)
)
`)
.eq('id', normalizedFileId)
.eq('messages.conversation_participants.user_id', userId)
.eq('messages.conversations.conversation_participants.user_id', userId)
.single();

if (fileError || !fileData) {
Expand All @@ -274,7 +301,7 @@ export async function POST(request, { params } = {}) {
encryptedMetadata: fileData.encrypted_metadata, // Client will decrypt
createdAt: fileData.created_at,
createdBy: fileData.created_by,
isOwner: fileData.created_by === userId
isOwner: fileData.created_by === authUserId || fileData.created_by === userId
});

} catch (err) {
Expand Down
23 changes: 22 additions & 1 deletion src/app/api/files/[fileId]/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,29 @@ function createFileQuery() {
return query;
}

function createUserQuery() {
const query = {
select: vi.fn(() => query),
eq: vi.fn(() => query),
single: vi.fn().mockResolvedValue({
data: { id: 'user-1' },
error: null
})
};
return query;
}

describe('/api/files/[fileId]', () => {
beforeEach(() => {
vi.resetModules();
vi.clearAllMocks();

mocks.authGetUser.mockResolvedValue({
data: { user: { id: 'user-1' } },
data: { user: { id: 'auth-user-1' } },
error: null
});
mocks.from.mockImplementation((table) => {
if (table === 'users') return createUserQuery();
if (table === 'encrypted_files') return createFileQuery();
throw new Error(`Unexpected table: ${table}`);
});
Expand Down Expand Up @@ -79,6 +92,10 @@ describe('/api/files/[fileId]', () => {
expect(response.status).toBe(200);
expect(response.headers.get('Content-Type')).toBe('application/octet-stream');
expect(mocks.eq).toHaveBeenCalledWith('id', 'file-1');
expect(mocks.eq).toHaveBeenCalledWith(
'messages.conversations.conversation_participants.user_id',
'user-1'
);
});

it('resolves async route params for POST metadata requests', async () => {
Expand All @@ -92,5 +109,9 @@ describe('/api/files/[fileId]', () => {
expect(body.id).toBe('file-1');
expect(body.conversationId).toBe('conversation-1');
expect(mocks.eq).toHaveBeenCalledWith('id', 'file-1');
expect(mocks.eq).toHaveBeenCalledWith(
'messages.conversations.conversation_participants.user_id',
'user-1'
);
});
});
Loading