Skip to content

Commit

Permalink
Merge pull request #431 from gitroomhq/feat/fix-upload
Browse files Browse the repository at this point in the history
fix upload
  • Loading branch information
nevo-david authored Nov 13, 2024
2 parents 35a9b1c + cc27280 commit 313f1f3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
import { timer } from '@gitroom/helpers/utils/timer';
import dayjs from 'dayjs';
import { SocialAbstract } from '@gitroom/nestjs-libraries/integrations/social.abstract';
import { string } from 'yup';

export class InstagramProvider
extends SocialAbstract
Expand Down
24 changes: 18 additions & 6 deletions libraries/nestjs-libraries/src/upload/r2.uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { Request, Response } from 'express';
import crypto from 'crypto';
import path from 'path';
import { makeId } from '@gitroom/nestjs-libraries/services/make.is';

const { CLOUDFLARE_ACCOUNT_ID, CLOUDFLARE_ACCESS_KEY, CLOUDFLARE_SECRET_ACCESS_KEY, CLOUDFLARE_BUCKETNAME, CLOUDFLARE_BUCKET_URL } =
process.env;
Expand All @@ -16,12 +19,16 @@ const R2 = new S3Client({
},
});

// Function to generate a random string
function generateRandomString() {
return makeId(20);
}

export default async function handleR2Upload(
endpoint: string,
req: Request,
res: Response
) {

switch (endpoint) {
case 'create-multipart-upload':
return createMultipartUpload(req, res);
Expand All @@ -39,30 +46,35 @@ export default async function handleR2Upload(
return res.status(404).end();
}

export async function simpleUpload(data: Buffer, key: string, contentType: string) {
export async function simpleUpload(data: Buffer, originalFilename: string, contentType: string) {
const fileExtension = path.extname(originalFilename); // Extract extension
const randomFilename = generateRandomString() + fileExtension; // Append extension

const params = {
Bucket: CLOUDFLARE_BUCKETNAME,
Key: key,
Key: randomFilename,
Body: data,
ContentType: contentType,
};

const command = new PutObjectCommand({ ...params });
await R2.send(command);

return CLOUDFLARE_BUCKET_URL + '/' + key;
return CLOUDFLARE_BUCKET_URL + '/' + randomFilename;
}

export async function createMultipartUpload(
req: Request,
res: Response
) {
const { file, fileHash, contentType } = req.body;
const filename = file.name;
const fileExtension = path.extname(file.name); // Extract extension
const randomFilename = generateRandomString() + fileExtension; // Append extension

try {
const params = {
Bucket: CLOUDFLARE_BUCKETNAME,
Key: `resources/${fileHash}/${filename}`,
Key: `${randomFilename}`,
ContentType: contentType,
Metadata: {
'x-amz-meta-file-hash': fileHash,
Expand Down

0 comments on commit 313f1f3

Please sign in to comment.