add formdata controller and delete file service

This commit is contained in:
OxSaitama 2023-05-08 22:26:35 +02:00
parent fe879584f2
commit d893fe6906
11 changed files with 72 additions and 34 deletions

View File

@ -48,8 +48,9 @@
"cors": "^2.8.5",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.0",
"le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.37",
"le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.38",
"module-alias": "^2.2.2",
"multer": "^1.4.5-lts.1",
"next": "^13.1.5",
"node-cache": "^5.1.2",
"node-schedule": "^2.1.1",
@ -66,6 +67,7 @@
"@types/express": "^4.17.16",
"@types/jest": "^29.5.0",
"@types/jsonwebtoken": "^9.0.1",
"@types/multer": "^1.4.7",
"@types/node": "^18.11.18",
"@types/node-schedule": "^2.1.0",
"@types/uuid": "^9.0.0",

View File

@ -4,7 +4,6 @@ import ApiController from "@Common/system/controller-pattern/ApiController";
import { Service } from "typedi";
import FilesService from "@Services/private-services/FilesService/FilesService";
import { Files } from "@prisma/client";
import ObjectHydrate from "@Common/helpers/ObjectHydrate";
import { File } from "le-coffre-resources/dist/SuperAdmin";
import { validateOrReject } from "class-validator";
@ -46,14 +45,18 @@ export default class FilesController extends ApiController {
@Post("/api/v1/super-admin/files")
protected async post(req: Request, response: Response) {
try {
//get file
if(!req.file) throw new Error('No file provided')
//init File resource with request body values
const fileEntity = File.hydrate<File>(req.body);
const fileEntity = File.hydrate<File>(JSON.parse(req.body["q"]));
//validate File
await validateOrReject(fileEntity, { groups: ["createFile"] });
//call service to get prisma entity
const prismaEntityCreated = await this.filesService.create(fileEntity);
const prismaEntityCreated = await this.filesService.create(fileEntity, req.file);
//Hydrate ressource with prisma entity
const fileEntityCreated = File.hydrate<File>(prismaEntityCreated, {
@ -114,7 +117,7 @@ export default class FilesController extends ApiController {
const fileEntity: Files = await this.filesService.delete(uid);
//Hydrate ressource with prisma entity
const file = ObjectHydrate.hydrate<File>(new File(), fileEntity, { strategy: "excludeAll" });
const file = File.hydrate<File>(fileEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, file);
@ -138,7 +141,7 @@ export default class FilesController extends ApiController {
const fileEntity = await this.filesService.getByUid(uid);
//Hydrate ressource with prisma entity
const file = ObjectHydrate.hydrate<File>(new File(), fileEntity, { strategy: "excludeAll" });
const file = File.hydrate<File>(fileEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, file);

View File

@ -0,0 +1,22 @@
import { NextFunction, Request, Response } from "express";
import multer from "multer";
export default function fileHandler(req: Request, response: Response, next: NextFunction) {
const storage = multer.memoryStorage()
const upload = multer({storage:storage}).single('file');
// Here call the upload middleware of multer
upload(req, response, function (err) {
if (err instanceof multer.MulterError) {
// A Multer error occurred when uploading.
const err = new Error('Multer error');
return next(err)
} else if (err) {
// An unknown error occurred when uploading.
const err = new Error('Server Error')
return next(err)
}
next()
})
}

View File

@ -0,0 +1,8 @@
/*
Warnings:
- Added the required column `file_name` to the `files` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "files" ADD COLUMN "file_name" VARCHAR(255) NOT NULL;

View File

@ -203,6 +203,7 @@ model Files {
document Documents @relation(fields: [document_uid], references: [uid], onDelete: Cascade)
document_uid String @db.VarChar(255)
file_path String @unique @db.VarChar(255)
file_name String @db.VarChar(255)
iv String @db.VarChar(255)
created_at DateTime? @default(now())
updated_at DateTime? @updatedAt

View File

@ -379,6 +379,7 @@ import {
{
uid: uidFiles1,
document_uid: uidDocument1,
file_name: "fileName1",
file_path: "https://www.google1.com",
iv: "randomIv1",
created_at: new Date(),
@ -387,6 +388,7 @@ import {
{
uid: uidFiles2,
document_uid: uidDocument2,
file_name: "fileName2",
file_path: "https://www.google2.com",
iv: "randomIv2",
created_at: new Date(),

View File

@ -35,9 +35,11 @@ export default class FilesRepository extends BaseRepository {
uid: file.document!.uid,
},
},
file_name: file.file_name,
file_path: file.file_path,
iv: file.iv
},
include: { document: true }
});
}

View File

@ -8,6 +8,7 @@ import bodyParser from "body-parser";
// import TezosLink from "@Common/databases/TezosLink";
import errorHandler from "@App/middlewares/ErrorHandler";
import { BackendVariables } from "@Common/config/variables/Variables";
import fileHandler from "@App/middlewares/FileHandler";
(async () => {
try {
@ -22,7 +23,7 @@ import { BackendVariables } from "@Common/config/variables/Variables";
label,
port: parseInt(port),
rootUrl,
middlwares: [cors({ origin: "*" }), bodyParser.urlencoded({ extended: true }), bodyParser.json()],
middlwares: [cors({ origin: "*" }), fileHandler, bodyParser.urlencoded({ extended: true }), bodyParser.json()],
errorHandler,
});

View File

@ -5,11 +5,10 @@ import crypto from "crypto";
@Service()
export default class CryptoService extends BaseService {
private key: CryptoKey;
private jwkKey: any;
private jwkKey: JsonWebKey;
private subtle: SubtleCrypto = crypto.webcrypto.subtle
constructor(protected variables: BackendVariables) {
super();
this.key = new CryptoKey();
this.jwkKey = {
kty: "oct",
k: variables.KEY_DATA,
@ -19,24 +18,18 @@ export default class CryptoService extends BaseService {
}
private async getKey() {
if (!this.key) this.key = await crypto.subtle.importKey("jwk", this.jwkKey, {name: "AES-GCM"}, false, ["encrypt", "decrypt"]);
return this.key;
}
public getTextEncoderDecoder() {
return { encoder: new TextEncoder(), decoder: new TextDecoder("utf-8") }
return await this.subtle.importKey("jwk", this.jwkKey, {name: "AES-GCM"}, false, ["encrypt", "decrypt"]);
}
/**
* @description : encrypt data
* @throws {Error} If data cannot be encrypted
*/
public async encrypt(data: any) {
const { encoder, decoder } = this.getTextEncoderDecoder();
const encodedData = encoder.encode(data);
public async encrypt(data: string) {
const encodedData = Buffer.from(data);
const iv = crypto.getRandomValues(new Uint8Array(16));
const key = await this.getKey();
const cipherData = await crypto.subtle.encrypt(
const cipherData = await this.subtle.encrypt(
{
name: "AES-GCM",
iv,
@ -45,8 +38,8 @@ export default class CryptoService extends BaseService {
encodedData,
);
const cipherText = decoder.decode(cipherData);
const ivStringified = decoder.decode(iv);
const cipherText = Buffer.from(cipherData).toString('base64');
const ivStringified = Buffer.from(iv).toString('base64');
return { cipherText, ivStringified };
}
@ -56,11 +49,10 @@ export default class CryptoService extends BaseService {
* @throws {Error} If data cannot be decrypted
*/
public async decrypt(cipherText: string, ivStringified: string): Promise<string> {
const { encoder, decoder } = this.getTextEncoderDecoder();
const cipherData = encoder.encode(cipherText);
const iv = encoder.encode(ivStringified);
const cipherData = Buffer.from(cipherText, 'base64');
const iv = Buffer.from(ivStringified, 'base64');
const key = await this.getKey();
const decryptedData = await crypto.subtle.decrypt(
const decryptedData = await this.subtle.decrypt(
{
name: "AES-GCM",
iv,
@ -69,6 +61,6 @@ export default class CryptoService extends BaseService {
cipherData,
);
return decoder.decode(decryptedData);
return Buffer.from(decryptedData).toString('utf-8');
}
}

View File

@ -4,8 +4,9 @@ import { Service } from "typedi";
import { File } from "le-coffre-resources/dist/SuperAdmin"
import CryptoService from "../CryptoService/CryptoService";
import IpfsService from "../IpfsService/IpfsService";
import fs from "fs";
//import fs from "fs";
import { BackendVariables } from "@Common/config/variables/Variables";
import { Readable } from "stream";
@Service()
export default class FilesService extends BaseService {
@ -25,10 +26,10 @@ export default class FilesService extends BaseService {
* @description : Create a new file
* @throws {Error} If file cannot be created
*/
public async create(file: File) {
const stream = fs.createReadStream('./login.png');
const upload = await this.ipfsService.pinFile(stream, 'login.png');
public async create(file: File, fileData: Express.Multer.File) {
const upload = await this.ipfsService.pinFile(Readable.from(fileData.buffer), fileData.originalname);
const encryptedPath = await this.cryptoService.encrypt(this.variables.PINATA_GATEWAY.concat(upload.IpfsHash));
file.file_name = fileData.originalname;
file.file_path = encryptedPath.cipherText;
file.iv = encryptedPath.ivStringified;
return this.filesRepository.create(file);
@ -47,6 +48,10 @@ export default class FilesService extends BaseService {
* @throws {Error} If file cannot be deleted
*/
public async delete(uid: string) {
const fileToUnpin = await this.filesRepository.findOneByUid(uid);
const decryptedFilePath = await this.cryptoService.decrypt(fileToUnpin.file_path, fileToUnpin.iv);
const fileHash= decryptedFilePath.substring(this.variables.PINATA_GATEWAY.length);
await this.ipfsService.unpinFile(fileHash)
return this.filesRepository.delete(uid);
}

View File

@ -2,7 +2,7 @@ import BaseService from "@Services/BaseService";
import { Service } from "typedi";
import pinataSDK from "@pinata/sdk";
import { BackendVariables } from "@Common/config/variables/Variables";
import fs from "fs";
import { Readable } from "stream";
@Service()
export default class FilesService extends BaseService {
@ -16,7 +16,7 @@ export default class FilesService extends BaseService {
* @description : pin a file
* @throws {Error} If file cannot be pinned
*/
public async pinFile(stream: fs.ReadStream, fileName: string) {
public async pinFile(stream: Readable, fileName: string) {
return this.ipfsClient.pinFileToIPFS(stream, {pinataMetadata : {name: fileName}});
}