|
@@ -10,7 +10,7 @@ import {
|
|
|
} from '@nestjs/common';
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
import { createHash } from 'node:crypto';
|
|
|
-import { QueryFailedError, Repository } from 'typeorm';
|
|
|
+import { Repository } from 'typeorm';
|
|
|
import { AuthUserDto } from '../../decorators/auth-user.decorator';
|
|
|
import { AssetEntity, AssetType } from '@app/database/entities/asset.entity';
|
|
|
import { constants, createReadStream, ReadStream, stat } from 'fs';
|
|
@@ -53,31 +53,17 @@ export class AssetService {
|
|
|
createAssetDto: CreateAssetDto,
|
|
|
originalPath: string,
|
|
|
mimeType: string,
|
|
|
+ checksum: Buffer,
|
|
|
): Promise<AssetEntity> {
|
|
|
- const checksum = await this.calculateChecksum(originalPath);
|
|
|
-
|
|
|
- try {
|
|
|
- const assetEntity = await this._assetRepository.create(
|
|
|
- createAssetDto,
|
|
|
- authUser.id,
|
|
|
- originalPath,
|
|
|
- mimeType,
|
|
|
- checksum,
|
|
|
- );
|
|
|
-
|
|
|
- return assetEntity;
|
|
|
- } catch (err) {
|
|
|
- if (err instanceof QueryFailedError && (err as any).constraint === 'UQ_userid_checksum') {
|
|
|
- const [assetEntity, _] = await Promise.all([
|
|
|
- this._assetRepository.getAssetByChecksum(authUser.id, checksum),
|
|
|
- fs.unlink(originalPath)
|
|
|
- ]);
|
|
|
-
|
|
|
- return assetEntity;
|
|
|
- }
|
|
|
+ const assetEntity = await this._assetRepository.create(
|
|
|
+ createAssetDto,
|
|
|
+ authUser.id,
|
|
|
+ originalPath,
|
|
|
+ mimeType,
|
|
|
+ checksum,
|
|
|
+ );
|
|
|
|
|
|
- throw err;
|
|
|
- }
|
|
|
+ return assetEntity;
|
|
|
}
|
|
|
|
|
|
public async getUserAssetsByDeviceId(authUser: AuthUserDto, deviceId: string) {
|
|
@@ -478,7 +464,11 @@ export class AssetService {
|
|
|
return mapAssetCountByTimeBucket(result);
|
|
|
}
|
|
|
|
|
|
- private calculateChecksum(filePath: string): Promise<Buffer> {
|
|
|
+ getAssetByChecksum(userId: string, checksum: Buffer) {
|
|
|
+ return this._assetRepository.getAssetByChecksum(userId, checksum);
|
|
|
+ }
|
|
|
+
|
|
|
+ calculateChecksum(filePath: string): Promise<Buffer> {
|
|
|
const fileReadStream = createReadStream(filePath);
|
|
|
const sha1Hash = createHash('sha1');
|
|
|
const deferred = new Promise<Buffer>((resolve, reject) => {
|