2022-12-12 20:56:14 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2022-12-13 12:05:08 +00:00
|
|
|
const getScreenshots = async function (resp: object, path: string): Promise<any> {
|
2022-12-12 20:56:14 +00:00
|
|
|
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 screenshots.splice(0, 5).map(async (x) => {
|
|
|
|
return {
|
|
|
|
thumb: formatUrl(await signer(`thumb/${x}`)),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
port: 3000,
|
|
|
|
async fetch(request: Request) {
|
2022-12-13 14:59:29 +00:00
|
|
|
await new Promise((r) => setTimeout(r, 1)); // https://github.com/oven-sh/bun/issues/1600
|
2022-12-12 20:56:14 +00:00
|
|
|
let reqUrl = await parseUrl(request.url)
|
|
|
|
let req = await request.json();
|
2022-12-13 14:59:29 +00:00
|
|
|
|
|
|
|
return Promise.all(await getScreenshots(req, reqUrl.path)).then((r) => {
|
2022-12-12 20:56:14 +00:00
|
|
|
let resp = Object.assign({}, req)
|
|
|
|
resp.screenshots = r
|
|
|
|
return resp
|
|
|
|
}).then((resp) => {
|
|
|
|
return new Response(JSON.stringify(resp))
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|