@appsemble/node-utils
Version:
NodeJS utilities used by Appsemble internally.
35 lines • 1.5 kB
JavaScript
import { extension } from 'mime-types';
import { assertKoaCondition } from '../../../../../koa.js';
export function createGetAppAssetByIdController({ getApp, getAppAsset }) {
return async (ctx) => {
const { pathParams: { appId, assetId }, } = ctx;
const app = await getApp({ context: ctx, query: { attributes: ['id'], where: { id: appId } } });
assertKoaCondition(app != null, ctx, 404, 'App not found');
const asset = await getAppAsset({ app, context: ctx, id: assetId });
if (assetId !== asset.id) {
// Redirect to asset using current asset ID
ctx.status = 302;
ctx.set('location', `/api/apps/${app.id}/assets/${asset.id}`);
// @ts-expect-error 2322 null is not assignable to type (strictNullChecks)
ctx.type = null;
return;
}
let { filename, mime } = asset;
if (!filename) {
filename = asset.id;
if (mime) {
const ext = extension(mime);
if (ext) {
filename += `.${ext}`;
}
}
}
ctx.set('content-type', mime || 'application/octet-stream');
if (filename) {
ctx.set('content-disposition', `attachment; filename=${JSON.stringify(filename)}`);
}
ctx.set('Cache-Control', 'max-age=31536000,immutable');
ctx.body = asset.stream;
};
}
//# sourceMappingURL=createGetAppAssetByIdController.js.map