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

@ -53,7 +53,6 @@
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.17.10", "@babel/core": "^7.17.10",
"@reactive-resume/schema": "workspace:*",
"@tailwindcss/typography": "^0.5.2", "@tailwindcss/typography": "^0.5.2",
"@types/downloadjs": "^1.4.3", "@types/downloadjs": "^1.4.3",
"@types/lodash": "^4.14.182", "@types/lodash": "^4.14.182",

7585
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -10,7 +10,6 @@
"lint": "eslint --fix --ext .ts ./src" "lint": "eslint --fix --ext .ts ./src"
}, },
"dependencies": { "dependencies": {
"@aws-sdk/client-s3": "^3.81.0",
"@nestjs/axios": "^0.0.7", "@nestjs/axios": "^0.0.7",
"@nestjs/common": "^8.4.4", "@nestjs/common": "^8.4.4",
"@nestjs/config": "^2.0.0", "@nestjs/config": "^2.0.0",
@ -53,7 +52,6 @@
"devDependencies": { "devDependencies": {
"@nestjs/cli": "^8.2.5", "@nestjs/cli": "^8.2.5",
"@nestjs/schematics": "^8.0.10", "@nestjs/schematics": "^8.0.10",
"@reactive-resume/schema": "workspace:*",
"@types/bcrypt": "^5.0.0", "@types/bcrypt": "^5.0.0",
"@types/cookie-parser": "^1.4.3", "@types/cookie-parser": "^1.4.3",
"@types/express": "^4.17.13", "@types/express": "^4.17.13",

View File

@ -7,7 +7,6 @@ import authConfig from './auth.config';
import databaseConfig from './database.config'; import databaseConfig from './database.config';
import googleConfig from './google.config'; import googleConfig from './google.config';
import sendgridConfig from './sendgrid.config'; import sendgridConfig from './sendgrid.config';
import storageConfig from './storage.config';
const validationSchema = Joi.object({ const validationSchema = Joi.object({
// App // App
@ -41,20 +40,12 @@ const validationSchema = Joi.object({
SENDGRID_FORGOT_PASSWORD_TEMPLATE_ID: Joi.string().allow(''), SENDGRID_FORGOT_PASSWORD_TEMPLATE_ID: Joi.string().allow(''),
SENDGRID_FROM_NAME: Joi.string().allow(''), SENDGRID_FROM_NAME: Joi.string().allow(''),
SENDGRID_FROM_EMAIL: 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({ @Module({
imports: [ imports: [
NestConfigModule.forRoot({ NestConfigModule.forRoot({
load: [appConfig, authConfig, databaseConfig, googleConfig, sendgridConfig, storageConfig], load: [appConfig, authConfig, databaseConfig, googleConfig, sendgridConfig],
validationSchema: validationSchema, validationSchema: validationSchema,
}), }),
], ],

View File

@ -35,8 +35,6 @@ export class ResumeController {
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get() @Get()
async findAllByUser(@User('id') userId: number) { async findAllByUser(@User('id') userId: number) {
console.log('findAllByUser', userId);
return this.resumeService.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 { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
@ -22,22 +21,11 @@ export const SHORT_ID_LENGTH = 8;
@Injectable() @Injectable()
export class ResumeService { export class ResumeService {
private s3Client: S3Client;
constructor( constructor(
@InjectRepository(Resume) private resumeRepository: Repository<Resume>, @InjectRepository(Resume) private resumeRepository: Repository<Resume>,
private configService: ConfigService, private configService: ConfigService,
private usersService: UsersService 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) { async create(createResumeDto: CreateResumeDto, userId: number) {
try { try {
@ -230,40 +218,29 @@ export class ResumeService {
async uploadPhoto(id: number, userId: number, file: Express.Multer.File) { async uploadPhoto(id: number, userId: number, file: Express.Multer.File) {
const resume = await this.findOne(id, userId); const resume = await this.findOne(id, userId);
const urlPrefix = this.configService.get<string>('storage.urlPrefix'); const filename = id + '--' + new Date().getTime() + extname(file.originalname);
const filename = new Date().getTime() + extname(file.originalname);
const key = `uploads/${userId}/${id}/${filename}`; const key = `uploads/${userId}/${id}/${filename}`;
const fs = require('fs');
await this.s3Client.send( fs.writeFile(__dirname + '/../assets/' + key, file.buffer, (err) => {
new PutObjectCommand({ if (err) {
Bucket: this.configService.get<string>('storage.bucket'), console.log(err);
Key: key, }
Body: file.buffer, });
ACL: 'public-read', const updatedResume = set(resume, 'basics.photo.url', '/api/assets/' + key);
})
);
const publicUrl = urlPrefix + key;
const updatedResume = set(resume, 'basics.photo.url', publicUrl);
return this.resumeRepository.save<Resume>(updatedResume); return this.resumeRepository.save<Resume>(updatedResume);
} }
async deletePhoto(id: number, userId: number) { async deletePhoto(id: number, userId: number) {
const resume = await this.findOne(id, userId); const resume = await this.findOne(id, userId);
const urlPrefix = this.configService.get<string>('storage.urlPrefix');
const publicUrl = resume.basics.photo.url; 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', ''); const updatedResume = set(resume, 'basics.photo.url', '');
return this.resumeRepository.save<Resume>(updatedResume); return this.resumeRepository.save<Resume>(updatedResume);