feat: fix uploading
This commit is contained in:
parent
35a9b1c4cf
commit
cc27280699
|
|
@ -9,7 +9,6 @@ import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
|
||||||
import { timer } from '@gitroom/helpers/utils/timer';
|
import { timer } from '@gitroom/helpers/utils/timer';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import { SocialAbstract } from '@gitroom/nestjs-libraries/integrations/social.abstract';
|
import { SocialAbstract } from '@gitroom/nestjs-libraries/integrations/social.abstract';
|
||||||
import { string } from 'yup';
|
|
||||||
|
|
||||||
export class InstagramProvider
|
export class InstagramProvider
|
||||||
extends SocialAbstract
|
extends SocialAbstract
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,9 @@ import {
|
||||||
} from '@aws-sdk/client-s3';
|
} from '@aws-sdk/client-s3';
|
||||||
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
||||||
import { Request, Response } from 'express';
|
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 } =
|
const { CLOUDFLARE_ACCOUNT_ID, CLOUDFLARE_ACCESS_KEY, CLOUDFLARE_SECRET_ACCESS_KEY, CLOUDFLARE_BUCKETNAME, CLOUDFLARE_BUCKET_URL } =
|
||||||
process.env;
|
process.env;
|
||||||
|
|
@ -16,12 +19,16 @@ const R2 = new S3Client({
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Function to generate a random string
|
||||||
|
function generateRandomString() {
|
||||||
|
return makeId(20);
|
||||||
|
}
|
||||||
|
|
||||||
export default async function handleR2Upload(
|
export default async function handleR2Upload(
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
req: Request,
|
req: Request,
|
||||||
res: Response
|
res: Response
|
||||||
) {
|
) {
|
||||||
|
|
||||||
switch (endpoint) {
|
switch (endpoint) {
|
||||||
case 'create-multipart-upload':
|
case 'create-multipart-upload':
|
||||||
return createMultipartUpload(req, res);
|
return createMultipartUpload(req, res);
|
||||||
|
|
@ -39,10 +46,13 @@ export default async function handleR2Upload(
|
||||||
return res.status(404).end();
|
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 = {
|
const params = {
|
||||||
Bucket: CLOUDFLARE_BUCKETNAME,
|
Bucket: CLOUDFLARE_BUCKETNAME,
|
||||||
Key: key,
|
Key: randomFilename,
|
||||||
Body: data,
|
Body: data,
|
||||||
ContentType: contentType,
|
ContentType: contentType,
|
||||||
};
|
};
|
||||||
|
|
@ -50,7 +60,7 @@ export async function simpleUpload(data: Buffer, key: string, contentType: strin
|
||||||
const command = new PutObjectCommand({ ...params });
|
const command = new PutObjectCommand({ ...params });
|
||||||
await R2.send(command);
|
await R2.send(command);
|
||||||
|
|
||||||
return CLOUDFLARE_BUCKET_URL + '/' + key;
|
return CLOUDFLARE_BUCKET_URL + '/' + randomFilename;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createMultipartUpload(
|
export async function createMultipartUpload(
|
||||||
|
|
@ -58,11 +68,13 @@ export async function createMultipartUpload(
|
||||||
res: Response
|
res: Response
|
||||||
) {
|
) {
|
||||||
const { file, fileHash, contentType } = req.body;
|
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 {
|
try {
|
||||||
const params = {
|
const params = {
|
||||||
Bucket: CLOUDFLARE_BUCKETNAME,
|
Bucket: CLOUDFLARE_BUCKETNAME,
|
||||||
Key: `resources/${fileHash}/${filename}`,
|
Key: `${randomFilename}`,
|
||||||
ContentType: contentType,
|
ContentType: contentType,
|
||||||
Metadata: {
|
Metadata: {
|
||||||
'x-amz-meta-file-hash': fileHash,
|
'x-amz-meta-file-hash': fileHash,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue