This commit is contained in:
Dries Van Damme
2022-06-06 01:34:58 +02:00
parent bd519db14f
commit 63f88a3d1c
6 changed files with 4655 additions and 3001 deletions

View File

@ -7,7 +7,6 @@ import authConfig from './auth.config';
import databaseConfig from './database.config';
import googleConfig from './google.config';
import sendgridConfig from './sendgrid.config';
import storageConfig from './storage.config';
const validationSchema = Joi.object({
// App
@ -41,20 +40,12 @@ const validationSchema = Joi.object({
SENDGRID_FORGOT_PASSWORD_TEMPLATE_ID: Joi.string().allow(''),
SENDGRID_FROM_NAME: Joi.string().allow(''),
SENDGRID_FROM_EMAIL: Joi.string().allow(''),
// Storage
STORAGE_BUCKET: Joi.string().allow(''),
STORAGE_REGION: Joi.string().allow(''),
STORAGE_ENDPOINT: Joi.string().allow(''),
STORAGE_URL_PREFIX: Joi.string().allow(''),
STORAGE_ACCESS_KEY: Joi.string().allow(''),
STORAGE_SECRET_KEY: Joi.string().allow(''),
});
@Module({
imports: [
NestConfigModule.forRoot({
load: [appConfig, authConfig, databaseConfig, googleConfig, sendgridConfig, storageConfig],
load: [appConfig, authConfig, databaseConfig, googleConfig, sendgridConfig],
validationSchema: validationSchema,
}),
],

View File

@ -35,8 +35,6 @@ export class ResumeController {
@UseGuards(JwtAuthGuard)
@Get()
async findAllByUser(@User('id') userId: number) {
console.log('findAllByUser', userId);
return this.resumeService.findAllByUser(userId);
}

View File

@ -1,4 +1,3 @@
import { DeleteObjectCommand, PutObjectCommand, S3, S3Client } from '@aws-sdk/client-s3';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { InjectRepository } from '@nestjs/typeorm';
@ -22,22 +21,11 @@ export const SHORT_ID_LENGTH = 8;
@Injectable()
export class ResumeService {
private s3Client: S3Client;
constructor(
@InjectRepository(Resume) private resumeRepository: Repository<Resume>,
private configService: ConfigService,
private usersService: UsersService
) {
this.s3Client = new S3({
endpoint: configService.get<string>('storage.endpoint'),
region: configService.get<string>('storage.region'),
credentials: {
accessKeyId: configService.get<string>('storage.accessKey'),
secretAccessKey: configService.get<string>('storage.secretKey'),
},
});
}
) {}
async create(createResumeDto: CreateResumeDto, userId: number) {
try {
@ -230,40 +218,29 @@ export class ResumeService {
async uploadPhoto(id: number, userId: number, file: Express.Multer.File) {
const resume = await this.findOne(id, userId);
const urlPrefix = this.configService.get<string>('storage.urlPrefix');
const filename = new Date().getTime() + extname(file.originalname);
const filename = id + '--' + new Date().getTime() + extname(file.originalname);
const key = `uploads/${userId}/${id}/${filename}`;
await this.s3Client.send(
new PutObjectCommand({
Bucket: this.configService.get<string>('storage.bucket'),
Key: key,
Body: file.buffer,
ACL: 'public-read',
})
);
const publicUrl = urlPrefix + key;
const updatedResume = set(resume, 'basics.photo.url', publicUrl);
const fs = require('fs');
fs.writeFile(__dirname + '/../assets/' + key, file.buffer, (err) => {
if (err) {
console.log(err);
}
});
const updatedResume = set(resume, 'basics.photo.url', '/api/assets/' + key);
return this.resumeRepository.save<Resume>(updatedResume);
}
async deletePhoto(id: number, userId: number) {
const resume = await this.findOne(id, userId);
const urlPrefix = this.configService.get<string>('storage.urlPrefix');
const publicUrl = resume.basics.photo.url;
const key = publicUrl.replace(urlPrefix, '');
await this.s3Client.send(
new DeleteObjectCommand({
Bucket: this.configService.get<string>('storage.bucket'),
Key: key,
})
);
const glob = require('glob');
const fs = require('fs');
fs.unlink(__dirname + '/../' + resume.basics.photo.url.replace('/api/', ''), (err) => {
if (err) {
console.log(err);
}
});
const updatedResume = set(resume, 'basics.photo.url', '');
return this.resumeRepository.save<Resume>(updatedResume);