feat: upload profile picture in case of expiration

This commit is contained in:
Nevo David 2024-07-26 19:15:18 +07:00
parent d99d4eae4c
commit 22a25c1a8c
3 changed files with 31 additions and 9 deletions

View File

@ -4,6 +4,8 @@ import dayjs from 'dayjs';
import * as console from 'node:console';
import { Integration } from '@prisma/client';
import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
import { simpleUpload } from '@gitroom/nestjs-libraries/upload/r2.uploader';
import axios from 'axios';
@Injectable()
export class IntegrationRepository {
@ -12,7 +14,12 @@ export class IntegrationRepository {
private _posts: PrismaRepository<'post'>
) {}
updateIntegration(id: string, params: Partial<Integration>) {
async updateIntegration(id: string, params: Partial<Integration>) {
if (params.picture && params.picture.indexOf(process.env.CLOUDFLARE_BUCKET_URL!) === -1) {
const picture = await axios.get(params.picture, { responseType: 'arraybuffer' });
params.picture = await simpleUpload(picture.data, `${makeId(10)}.png`, 'image/png');
}
return this._integration.model.integration.update({
where: {
id,

View File

@ -7,6 +7,9 @@ import { SocialProvider } from '@gitroom/nestjs-libraries/integrations/social/so
import { Integration } from '@prisma/client';
import { NotificationService } from '@gitroom/nestjs-libraries/database/prisma/notifications/notification.service';
import { LinkedinPageProvider } from '@gitroom/nestjs-libraries/integrations/social/linkedin.page.provider';
import { simpleUpload } from '@gitroom/nestjs-libraries/upload/r2.uploader';
import axios from 'axios';
import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
@Injectable()
export class IntegrationService {
@ -15,7 +18,7 @@ export class IntegrationService {
private _integrationManager: IntegrationManager,
private _notificationService: NotificationService
) {}
createOrUpdateIntegration(
async createOrUpdateIntegration(
org: string,
name: string,
picture: string,
@ -29,10 +32,13 @@ export class IntegrationService {
isBetweenSteps = false,
refresh?: string
) {
const loadImage = await axios.get(picture, { responseType: 'arraybuffer' });
const uploadedPicture = await simpleUpload(loadImage.data, `${makeId(10)}.png`, 'image/png');
return this._integrationRepository.createOrUpdateIntegration(
org,
name,
picture,
uploadedPicture,
type,
internalId,
provider,

View File

@ -1,10 +1,5 @@
import {
UploadPartCommand,
S3Client,
ListPartsCommand,
CreateMultipartUploadCommand,
CompleteMultipartUploadCommand,
AbortMultipartUploadCommand,
UploadPartCommand, S3Client, ListPartsCommand, CreateMultipartUploadCommand, CompleteMultipartUploadCommand, AbortMultipartUploadCommand, PutObjectCommand
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { Request, Response } from 'express';
@ -44,6 +39,20 @@ export default async function handleR2Upload(
return res.status(404).end();
}
export async function simpleUpload(data: Buffer, key: string, contentType: string) {
const params = {
Bucket: CLOUDFLARE_BUCKETNAME,
Key: key,
Body: data,
ContentType: contentType,
};
const command = new PutObjectCommand({ ...params });
await R2.send(command);
return CLOUDFLARE_BUCKET_URL + '/' + key;
}
export async function createMultipartUpload(
req: Request,
res: Response