Commit 5cef4cc5 authored by nanahira's avatar nanahira

hash thing

parent cf2e61b1
import { Connection, In, IsNull, Not, SelectQueryBuilder } from 'typeorm';
import { InjectConnection } from '@nestjs/typeorm';
import { ConsoleLogger, Injectable } from '@nestjs/common';
import { ConsoleLogger, forwardRef, Inject, Injectable } from '@nestjs/common';
import { AppsJson } from './utility/apps-json-type';
import { App } from './entities/App.entity';
import { BlankReturnMessageDto, ReturnMessageDto } from './dto/ReturnMessage.dto';
......@@ -19,6 +19,8 @@ export class AppService extends ConsoleLogger {
private readonly packageVersionTraceCount: number;
private readonly packageVersionPreserveCount: number;
constructor(
@Inject(forwardRef(() => AppService))
appService: AppService,
@InjectConnection('app')
private db: Connection,
private packager: PackagerService,
......@@ -299,4 +301,12 @@ export class AppService extends ConsoleLogger {
async deleteApp(id: string) {
return this.updateResult(() => this.db.getRepository(App).update({ id }, { isDeleted: true }));
}
async lookForExistingArchiveHash(path: string) {
const archive = await this.db.getRepository(Archive).findOne({ where: { path, hash: Not(IsNull()) }, select: ['hash'] });
if (archive) {
return archive.hash;
}
return null;
}
}
......@@ -21,6 +21,9 @@ export class Archive extends TimeBase {
@Column('varchar', { length: 128 })
path: string;
@Column('varchar', { length: 128, nullable: true })
hash: string;
@Column('int', { unsigned: true })
size: number;
......
......@@ -11,10 +11,11 @@ import { PackageResult } from '../dto/PackageResult.dto';
import { PackageS3Service } from '../package-s3/package-s3.service';
import readdirp from 'readdirp';
import { ConsoleLogger, Injectable } from '@nestjs/common';
import { createHash } from 'crypto';
import { ConsoleLogger, forwardRef, Inject, Injectable } from '@nestjs/common';
import { Archive, ArchiveType } from '../entities/Archive.entity';
import { Build } from '../entities/Build.entity';
import { AppService } from '../app.service';
import { createHash } from 'crypto';
export interface FileWithHash {
file: readdirp.EntryInfo;
......@@ -63,7 +64,11 @@ export class PackagerService extends ConsoleLogger {
bucket_enter = 1 * 1024 ** 2;
packagerWorkingDirectory: string;
constructor(private s3: PackageS3Service, config: ConfigService) {
constructor(
@Inject(forwardRef(() => AppService)) private readonly appService: AppService,
private s3: PackageS3Service,
config: ConfigService
) {
super('packager');
this.bucket_max = (parseInt(config.get('PACKAGE_BUCKET_MAX')) || 10) * 1024 ** 2;
this.bucket_enter = (parseInt(config.get('PACKAGE_BUCKET_ENTER')) || 1) * 1024 ** 2;
......@@ -173,11 +178,14 @@ export class PackagerService extends ConsoleLogger {
const existing = await this.s3.fileExists(archiveName);
if (existing) {
archive.size = existing.Size;
return archive;
const hash = await this.appService.lookForExistingArchiveHash(archiveTask.path);
if (hash) {
archive.hash = hash;
return archive;
}
}
const files = archiveTask.filePaths;
this.log(`Packaging archive ${archiveName} with ${files.length} files.`);
const stream = new Stream.Writable();
const child = child_process.spawn('tar', ['--zstd', '-cf', '-'].concat(files), {
cwd: root,
});
......@@ -193,10 +201,20 @@ export class PackagerService extends ConsoleLogger {
reject(error);
});
});
const hashObject = createHash('sha512');
child.stdout.on('data', (chunk) => {
hashObject.update(chunk);
});
if (existing) {
await childPromise;
archive.hash = hashObject.digest('hex');
return archive;
}
const uploadPromise = this.s3.uploadStream(archiveName, child.stdout, {
ContentType: 'application/tar+zstd',
});
const [, { object }] = await Promise.all([childPromise, uploadPromise]);
archive.hash = hashObject.digest('hex');
archive.size = object.Size;
return archive;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment