UNPKG

@appsemble/node-utils

Version:

NodeJS utilities used by Appsemble internally.

45 lines 1.57 kB
import { createReadStream } from 'node:fs'; import { stat } from 'node:fs/promises'; import { logger } from './logger.js'; import { uploadS3File } from './s3.js'; import { removeUploads } from './uploads.js'; export function setAssetHeaders(ctx, mime, filename, stats) { ctx.set('content-type', mime || 'application/octet-stream'); if (filename) { ctx.set('content-disposition', `${mime?.startsWith('image') ? 'inline' : 'attachment'}; filename=${JSON.stringify(filename)}`); } if (stats) { ctx.set('Content-Length', String(stats.size)); ctx.set('ETag', stats.etag); ctx.set('Last-Modified', stats.lastModified.toUTCString()); } ctx.set('Access-Control-Expose-Headers', 'Content-Disposition'); ctx.set('Cache-Control', 'max-age=31536000,immutable'); } export function getCompressedFileMeta({ filename, mime }) { return { filename, mime }; } export async function uploadAsset(appId, asset) { const { id, path } = asset; try { const stats = await stat(path); const stream = createReadStream(path); await uploadS3File(`app-${appId}`, id, stream, stats.size); } catch (error) { logger.error(error); throw error; } } export async function uploadAssets(appId, assets) { const filesToUnlink = [...new Set(assets.map(({ path }) => path))]; try { for (const asset of assets) { await uploadAsset(appId, asset); } } finally { await removeUploads(filesToUnlink); } } //# sourceMappingURL=assets.js.map