-
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.
feat: bucket-file stream route handler & updated test suite
- Loading branch information
Showing
7 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
|
||
export * from './delete'; | ||
export * from './read'; | ||
export * from './stream'; |
72 changes: 72 additions & 0 deletions
72
packages/server-storage/src/http/controllers/bucket-file/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,72 @@ | ||
/* | ||
* 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 { | ||
BucketFileEntity, | ||
} from '../../../../domains'; | ||
|
||
async function packFile( | ||
pack: Pack, | ||
file: BucketFileEntity, | ||
) : Promise<void> { | ||
const minio = useMinio(); | ||
return new Promise<void>((resolve, reject) => { | ||
minio.getObject(file.bucket.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)); | ||
}); | ||
} | ||
|
||
export async function executeBucketFileRouteStreamHandler(req: Request, res: Response) : Promise<any> { | ||
const id = useRequestParam(req, 'id'); | ||
|
||
const dataSource = await useDataSource(); | ||
const repository = dataSource.getRepository(BucketFileEntity); | ||
const entity = await repository.findOne({ | ||
where: { | ||
id, | ||
}, | ||
relations: ['bucket'], | ||
}); | ||
|
||
if (!entity) { | ||
throw new NotFoundError(); | ||
} | ||
|
||
res.writeHead(200, { | ||
'Content-Type': 'application/x-tar', | ||
'Transfer-Encoding': 'chunked', | ||
}); | ||
|
||
const pack = tar.pack(); | ||
pack.pipe(res); | ||
|
||
await packFile(pack, entity); | ||
|
||
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