import {HttpRequest} from "@aws-sdk/protocol-http"; import {S3RequestPresigner} from "@aws-sdk/s3-request-presigner"; import {parseUrl} from "@aws-sdk/url-parser"; import {Hash} from "@aws-sdk/hash-node"; import {fromEnv} from "@aws-sdk/credential-provider-env"; import {formatUrl} from "@aws-sdk/util-format-url"; // Minio set-up variables const minioSchema = process.env.MINIO_SCHEMA; const minioHostname = process.env.MINIO_HOSTNAME; const minioPort = process.env.MINIO_PORT; const minioBucket = process.env.MINIO_BUCKET; const signer = async function (asset: string) { const baseUrl = minioSchema + '://' + minioHostname + ((minioPort === '443' || minioPort === '80') ? '' : `:${minioPort}`) + '/' + minioBucket + '/' const s3ObjectUrl = parseUrl(baseUrl + asset); const presigner = new S3RequestPresigner({ credentials: fromEnv(), // AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are read here. region: 'us-east-1', sha256: Hash.bind(null, "sha256"), // In Node.js }); // Create a GET request from S3 url. return await presigner.presign(new HttpRequest(s3ObjectUrl)); } const shots = 5 const reduceScreenshots = function (screenshots: Array) { if (screenshots.length <= shots) return screenshots else var sc = [] for (let i = 0; i < shots; i++) { sc.push(screenshots[i * Math.floor((screenshots.length - 1) / (shots - 1))]) } return sc } const getScreenshots = async function (resp: object, path: string): Promise { const screenshots = resp.screenshots ? resp.screenshots : [] if (path === '/details/') { return screenshots.map(async (x) => { return { thumb: formatUrl(await signer(`thumb/${x}`)), orig: formatUrl(await signer(`${x}`)) } }); } else if (path === '/initial/' || path === '/query/' || path === '/streaming/') { return reduceScreenshots(screenshots).map(async (x) => { return { thumb: formatUrl(await signer(`thumb/${x}`)), } }); } } export default { port: 3000, async fetch(request: Request) { await new Promise((r) => setTimeout(r, 1)); // https://github.com/oven-sh/bun/issues/1600 let reqUrl = await parseUrl(request.url) let req = await request.json(); return Promise.all(await getScreenshots(req, reqUrl.path)).then((r) => { let resp = Object.assign({}, req) resp.screenshots = r return resp }).then((resp) => { return new Response(JSON.stringify(resp)) }); }, };