Skip to content

Commit

Permalink
update link routes and add shared_access post api to get the user det…
Browse files Browse the repository at this point in the history
…ails from shared linkl
  • Loading branch information
parwatcodes committed Dec 23, 2024
1 parent d050394 commit cb1feec
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
26 changes: 22 additions & 4 deletions Client/src/app/api/links/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
return createErrorResponse('link Id is required.', 400);
}

const signedUrl = await LinkService.getFileFromLink(linkIdFromParams);
return NextResponse.json({ message: 'Link URL generated', data: { signedUrl } }, { status: 200 });
const link = await prisma.link.findUnique({
where: {
linkId: linkIdFromParams,
},
});

if (!link?.hasSharingOptions && link?.isPublic) {
const signedUrl = await LinkService.getFileFromLink(linkIdFromParams);
return NextResponse.json({ message: 'Link URL generated', data: { signedUrl } }, { status: 200 });
} else {
return NextResponse.json({ message: 'Link has sharing options enabled', data: { link } }, { status: 200 });
}
} catch (error) {
return createErrorResponse('Server error.', 500, error);
}
Expand All @@ -24,21 +34,29 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
export async function POST(req: NextRequest): Promise<NextResponse> {
try {
const userId = await authenticate(req);
const { documentId, friendlyName, isPublic, emailRequired, passwordRequired, password, linkUrl, expirationTime } = await req.json();
const { documentId, friendlyName, isPublic, password, expirationTime, hasSharingOptions } = await req.json();

if (!documentId) {
return createErrorResponse('Document ID is required.', 400);
}

const { linkUrl, linkId } = LinkService.generateLinkDetails();

if (expirationTime && new Date(expirationTime) < new Date()) {
return createErrorResponse('Expiration time cannot be in the past.', 400);
}

const newLink = await prisma.link.create({
data: {
userId,
linkId,
linkUrl,
documentId,
isPublic: isPublic,
password: password || null,
password: password || null, //TODO: hash password
friendlyName: friendlyName || "",
expirationTime: expirationTime || null,
hasSharingOptions: hasSharingOptions || false,
},
});

Expand Down
54 changes: 54 additions & 0 deletions Client/src/app/api/links/shared_access.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { NextRequest, NextResponse } from 'next/server';
import LinkService from '@/services/linkService';
import prisma from '@lib/prisma';

export async function POST(req: NextRequest): Promise<NextResponse> {
try {
const { linkId, first_name, last_name, email, password } = await req.json();

if (!linkId || !first_name || !last_name || !email || !password) {
return createErrorResponse('Link ID, firstName, lastName, email and password are required.', 400);
}

const link = await prisma.link.findUnique({
where: { id: linkId },
});

if (!link) {
return createErrorResponse('Link not found.', 404);
}

if (!link.hasSharingOptions) {
return createErrorResponse('This link does not require sharing options.', 400);
}

if (password !== link.password) {
return createErrorResponse('Password invalid.', 403);
}

const linkRecipient = await prisma.linkRecipients.create({
data: {
linkId,
first_name,
last_name,
email
}
});

const signedUrl = await LinkService.getFileFromLink(linkId);
return NextResponse.json({ message: 'Link URL generated', data: { signedUrl } }, { status: 200 });
} catch (error) {
return createErrorResponse('Server error.', 500, error);
}
}

function createErrorResponse(message: string, status: number, details?: any) {
if (Array.isArray(details) && details.length === 2) {
const [errorCode, errorMessage] = details;
console.error(`[${new Date().toISOString()}] ${errorMessage}`, details);
return NextResponse.json({ error: errorMessage, details }, { status: errorCode });
} else {
console.error(`[${new Date().toISOString()}] ${message}`, details);
return NextResponse.json({ error: message, details }, { status });
}
}
8 changes: 6 additions & 2 deletions Client/src/services/linkService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import { SupabaseProvider } from '@/providers/storage/supabase/supabaseProvider'

export default class LinkService {

static generateLinkUrl(host: string): string {
static generateLinkDetails(): { linkUrl: string, linkId: string; } {
const uniqueId = uuidv4();
const HOST = process.env.HOST || 'http://localhost:3000';

return `${host}/${uniqueId}`;
return {
linkId: uniqueId,
linkUrl: `${HOST}/api/links/${uniqueId}`
};
}

static async deleteLink(linkId: string): Promise<void> {
Expand Down

0 comments on commit cb1feec

Please sign in to comment.