51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
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 URIError("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) {
|
|
if (err instanceof URIError) {
|
|
res.status(404).send(String(err));
|
|
return;
|
|
}
|
|
if (err instanceof Error) throw err;
|
|
else throw Error(String(err));
|
|
}
|
|
}
|
|
};
|
|
|
|
export const page = new Repo();
|