This commit is contained in:
47
pages/repo.ts
Normal file
47
pages/repo.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { access, constants } from 'node:fs/promises';
|
||||
import { Readable } from 'stream';
|
||||
import { Client } from 'minio';
|
||||
|
||||
import Page from '../classes/Page';
|
||||
import { config } from '../config';
|
||||
|
||||
const client = new Client(config.minio.client);
|
||||
|
||||
async function fetchFile(file: string): Promise<Readable> {
|
||||
try {
|
||||
return await client.getObject(config.minio.bucket, file);
|
||||
} catch(err: unknown) {
|
||||
console.log(`Fail to fetch ${file}`);
|
||||
throw Error("Required resource doen't exist.");
|
||||
}
|
||||
}
|
||||
|
||||
class Repo extends Page{
|
||||
get path(){return `/${config.repo.name}/os/x86_64/*`;}
|
||||
async handleRequest(req: Request, res: Response){
|
||||
try {
|
||||
const file: string = req.path.split('/').pop() ?? '';
|
||||
console.log(`Request for file ${file}`);
|
||||
|
||||
const fileStream: Readable = await fetchFile(file);
|
||||
|
||||
res.set({
|
||||
'Content-Type': 'application/octet-stream'
|
||||
});
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
fileStream.pipe(res);
|
||||
fileStream.on('end', () => {
|
||||
console.log(`Response with ${file}`);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
} catch(err: unknown) {
|
||||
res.status(404).send(err as string);
|
||||
if (err instanceof Error) throw err;
|
||||
else throw Error(String(err));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const page = new Repo();
|
||||
Reference in New Issue
Block a user