fix FileService to compute file hash in create

This commit is contained in:
Loïs Mansot 2023-09-21 11:39:12 +02:00
parent d6bd2315e8
commit 92f10dd407
No known key found for this signature in database
GPG Key ID: 8CF1F4150DDA726D
2 changed files with 7 additions and 0 deletions

View File

@ -15,6 +15,10 @@ export default class CryptoService extends BaseService {
return crypto.createHash("sha256").update(String(key)).digest("base64").slice(0, 32); return crypto.createHash("sha256").update(String(key)).digest("base64").slice(0, 32);
} }
public async getHash(buffer: Buffer): Promise<string> {
return crypto.createHash("sha256").update(buffer).digest("hex");
}
/** /**
* @description : encrypt data * @description : encrypt data
* @throws {Error} If data cannot be encrypted * @throws {Error} If data cannot be encrypted

View File

@ -71,12 +71,15 @@ export default class FilesService extends BaseService {
public async create(file: File, fileData: Express.Multer.File) { public async create(file: File, fileData: Express.Multer.File) {
const key = v4(); const key = v4();
const encryptedFile = await this.cryptoService.encrypt(fileData.buffer, key); const encryptedFile = await this.cryptoService.encrypt(fileData.buffer, key);
const hash = await this.cryptoService.getHash(fileData.buffer);
const upload = await this.ipfsService.pinFile(Readable.from(encryptedFile), fileData.originalname); const upload = await this.ipfsService.pinFile(Readable.from(encryptedFile), fileData.originalname);
let fileToCreate: File = file; let fileToCreate: File = file;
fileToCreate.file_name = fileData.originalname; fileToCreate.file_name = fileData.originalname;
fileToCreate.file_path = this.variables.PINATA_GATEWAY.concat(upload.IpfsHash); fileToCreate.file_path = this.variables.PINATA_GATEWAY.concat(upload.IpfsHash);
fileToCreate.mimetype = fileData.mimetype; fileToCreate.mimetype = fileData.mimetype;
fileToCreate.size = fileData.size; fileToCreate.size = fileData.size;
fileToCreate.hash = hash;
fileToCreate.archived_at = null; fileToCreate.archived_at = null;
return this.filesRepository.create(fileToCreate, key); return this.filesRepository.create(fileToCreate, key);