Skip to content

Commit

Permalink
Merge pull request #544 from mfts/fix/download
Browse files Browse the repository at this point in the history
fix(download)
  • Loading branch information
mfts authored Jul 30, 2024
2 parents 842cd76 + 39241aa commit cb5ce8c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 72 deletions.
2 changes: 0 additions & 2 deletions components/view/access-form/email-section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ export default function EmailSection({
setData({ ...data, email: newEmail });
};

console.log("brand", brand);

return (
<div className="pb-5">
<div className="relative space-y-2 rounded-md shadow-sm">
Expand Down
1 change: 0 additions & 1 deletion components/view/access-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export default function AccessForm({
}
}, [email]);

console.log("brand", brand);
return (
<div
className="flex h-dvh flex-col justify-between pb-4 pt-12"
Expand Down
19 changes: 19 additions & 0 deletions lib/files/aws-client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LambdaClient } from "@aws-sdk/client-lambda";
import { S3Client } from "@aws-sdk/client-s3";

export const getS3Client = () => {
Expand All @@ -24,3 +25,21 @@ export const getS3Client = () => {
: undefined,
});
};

export const getLambdaClient = () => {
const NEXT_PUBLIC_UPLOAD_TRANSPORT = process.env.NEXT_PUBLIC_UPLOAD_TRANSPORT;

if (NEXT_PUBLIC_UPLOAD_TRANSPORT !== "s3") {
throw new Error("Invalid upload transport");
}

return new LambdaClient({
region: process.env.NEXT_PRIVATE_UPLOAD_REGION || "eu-central-1",
credentials: {
accessKeyId: String(process.env.NEXT_PRIVATE_UPLOAD_ACCESS_KEY_ID),
secretAccessKey: String(
process.env.NEXT_PRIVATE_UPLOAD_SECRET_ACCESS_KEY,
),
},
});
};
73 changes: 4 additions & 69 deletions pages/api/links/download/bulk.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,15 @@
import { NextApiRequest, NextApiResponse } from "next";

import {
InvocationType,
InvokeCommand,
LambdaClient,
} from "@aws-sdk/client-lambda";
import { InvocationType, InvokeCommand } from "@aws-sdk/client-lambda";
import { ViewType } from "@prisma/client";
import archiver from "archiver";
import mime from "mime-types";

import { getS3Client } from "@/lib/files/aws-client";
import { S3DownloadService } from "@/lib/files/bulk-download";
import { getFile } from "@/lib/files/get-file";
import { getLambdaClient } from "@/lib/files/aws-client";
import prisma from "@/lib/prisma";
import {
getExtensionFromSupportedType,
getMimeTypeFromSupportedType,
} from "@/lib/utils/get-content-type";

export const config = {
maxDuration: 180,
};

const s3Client = getS3Client();
const s3Service = new S3DownloadService(s3Client);

const finalizeArchiveSafely = (archive: archiver.Archiver): Promise<void> => {
return new Promise((resolve, reject) => {
archive.on("error", reject);
archive.finalize().then(resolve).catch(reject);
});
};

const sanitizeFileName = (name: string, mimeType: string): string => {
// Replace newlines and other potentially problematic characters
let sanitized = name.replace(/[\n\r\\\/]/g, "_");

// Add correct extension if it doesn't exist
const extension = mime.extension(getMimeTypeFromSupportedType(mimeType)!);
if (extension && !sanitized.endsWith(`.${extension}`)) {
sanitized += `.${extension}`;
}

return sanitized;
};

const generateUniqueFileName = (
name: string,
existingNames: Set<string>,
): string => {
let uniqueName = name;
let counter = 1;

while (existingNames.has(uniqueName)) {
const nameParts = name.split(".");
if (nameParts.length > 1) {
const ext = nameParts.pop();
uniqueName = `${nameParts.join(".")} (${counter}).${ext}`;
} else {
uniqueName = `${name}_${counter}`;
}
counter++;
}

existingNames.add(uniqueName);
return uniqueName;
};

export default async function handle(
req: NextApiRequest,
res: NextApiResponse,
Expand Down Expand Up @@ -158,20 +101,12 @@ export default async function handle(

const fileKeysOnly = view.dataroom.documents
.filter((doc) => doc.document.versions[0].type !== "notion")
.filter((doc) => doc.document.versions[0].storageType === "VERCEL_BLOB")
.filter((doc) => doc.document.versions[0].storageType !== "VERCEL_BLOB")
.map((doc) => {
return doc.document.versions[0].file;
});

const client = new LambdaClient({
region: process.env.NEXT_PRIVATE_UPLOAD_REGION || "eu-central-1",
credentials: {
accessKeyId: String(process.env.NEXT_PRIVATE_UPLOAD_ACCESS_KEY_ID),
secretAccessKey: String(
process.env.NEXT_PRIVATE_UPLOAD_SECRET_ACCESS_KEY,
),
},
});
const client = getLambdaClient();

const params = {
FunctionName: "bulk-download-zip-creator-prod", // Use the name you gave your Lambda function
Expand Down

0 comments on commit cb5ce8c

Please sign in to comment.