Skip to content

Commit

Permalink
fix: adjusted flow to streaming bucket-files
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Jul 2, 2024
1 parent 205b93e commit 67e06c1
Showing 2 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
*/

import { NotFoundError } from '@ebec/http';
import { useLogger } from '@privateaim/server-kit';
import type { Request, Response } from 'routup';
import { useRequestParam } from 'routup';
import { useDataSource } from 'typeorm-extension';
@@ -19,11 +20,8 @@ export async function executeBucketFileRouteStreamHandler(req: Request, res: Res

const dataSource = await useDataSource();
const repository = dataSource.getRepository(BucketFileEntity);
const entity = await repository.findOne({
where: {
id,
},
relations: ['bucket'],
const entity = await repository.findOneBy({
id,
});

if (!entity) {
@@ -37,8 +35,12 @@ export async function executeBucketFileRouteStreamHandler(req: Request, res: Res
// todo: this should work
// setResponseHeaderAttachment(res, entity.path);

const bucketName = toBucketName(entity.bucket_id);

useLogger().debug(`Streaming file ${entity.hash} (${id}) of ${bucketName}`);

const minio = useMinio();
const stream = await minio.getObject(toBucketName(entity.bucket.id), entity.hash);
const stream = await minio.getObject(bucketName, entity.hash);

stream.pipe(res);
}
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@

import { isUUID } from '@authup/kit';
import { NotFoundError } from '@ebec/http';
import { useLogger } from '@privateaim/server-kit';
import type { Request, Response } from 'routup';
import { useRequestParam } from 'routup';
import type { Pack } from 'tar-stream';
@@ -44,18 +45,30 @@ async function packFile(
});
}

async function packFiles(
pack: Pack,
async function streamFiles(
res: Response,
name: string,
files: BucketFileEntity[],
) {
const promises : Promise<void>[] = [];
return new Promise<void>((resolve, reject) => {
const pack = tar.pack();
pack.pipe(res);
pack.on('error', (err) => {
reject(err);
});

for (let i = 0; i < files.length; i++) {
promises.push(packFile(pack, name, files[i]));
}
const promises : Promise<void>[] = [];

await Promise.all(promises);
for (let i = 0; i < files.length; i++) {
promises.push(packFile(pack, name, files[i]));
}

Promise.resolve()
.then(() => Promise.all(promises))
.then(() => pack.finalize())
.then(() => resolve())
.catch((e) => reject(e));
});
}

export async function executeBucketRouteStreamHandler(req: Request, res: Response) : Promise<any> {
@@ -84,10 +97,9 @@ export async function executeBucketRouteStreamHandler(req: Request, res: Respons
'Transfer-Encoding': 'chunked',
});

const pack = tar.pack();
pack.pipe(res);
const bucketName = toBucketName(entity.id);

await packFiles(pack, toBucketName(entity.id), files);
useLogger().debug(`Streaming files of ${bucketName}`);

pack.finalize();
await streamFiles(res, bucketName, files);
}

0 comments on commit 67e06c1

Please sign in to comment.