-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
118 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages/server-storage/src/http/controllers/bucket/handlers/stream.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (c) 2022-2024. | ||
* Author Peter Placzek (tada5hi) | ||
* For the full copyright and license information, | ||
* view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
import { NotFoundError } from '@ebec/http'; | ||
import type { Request, Response } from 'routup'; | ||
import { useRequestParam } from 'routup'; | ||
import type { Pack } from 'tar-stream'; | ||
import tar from 'tar-stream'; | ||
import { useDataSource } from 'typeorm-extension'; | ||
import { streamToBuffer, useMinio } from '../../../../core'; | ||
import { | ||
BucketEntity, BucketFileEntity, | ||
} from '../../../../domains'; | ||
|
||
async function packFile( | ||
pack: Pack, | ||
name: string, | ||
file: BucketFileEntity, | ||
) : Promise<void> { | ||
const minio = useMinio(); | ||
return new Promise<void>((resolve, reject) => { | ||
minio.getObject(name, file.hash) | ||
.then((stream) => streamToBuffer(stream)) | ||
.then((data) => { | ||
pack.entry({ | ||
name: file.path, | ||
size: data.byteLength, | ||
}, data, (err) => { | ||
if (err) { | ||
reject(err); | ||
|
||
return; | ||
} | ||
|
||
resolve(); | ||
}); | ||
}) | ||
.catch((e) => reject(e)); | ||
}); | ||
} | ||
|
||
async function packFiles( | ||
pack: Pack, | ||
name: string, | ||
files: BucketFileEntity[], | ||
) { | ||
const promises : Promise<void>[] = []; | ||
|
||
for (let i = 0; i < files.length; i++) { | ||
promises.push(packFile(pack, name, files[i])); | ||
} | ||
|
||
await Promise.all(promises); | ||
} | ||
|
||
export async function executeBucketRouteStreamHandler(req: Request, res: Response) : Promise<any> { | ||
const id = useRequestParam(req, 'id'); | ||
|
||
const dataSource = await useDataSource(); | ||
const repository = dataSource.getRepository(BucketEntity); | ||
const entity = await repository.findOneBy({ id }); | ||
if (!entity) { | ||
throw new NotFoundError(); | ||
} | ||
|
||
const fileRepository = dataSource.getRepository(BucketFileEntity); | ||
const files = await fileRepository.findBy({ | ||
bucket_id: entity.id, | ||
}); | ||
|
||
const pack = tar.pack(); | ||
pack.pipe(res); | ||
|
||
await packFiles(pack, entity.name, files); | ||
|
||
pack.finalize(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters