import { readFile } from 'fs/promises' import { i18n } from '../i18n' import { sdk } from '../sdk' import { dataDir } from '../utils' const { InputSpec, Value } = sdk // Where to drop the uploaded PDF on the main volume. nacho (and any other // daemon that mounts /data) can read it from this path. const SUPPORT_PDF_PATH = `${dataDir}/support.pdf` const inputSpec = InputSpec.of({ // `required: true` so the form refuses to submit without a file selected — // works around the StartOS quirk where an empty file picker submits `{}` // (which Value.file's nullable parser rejects). pdf: Value.file({ name: i18n('Workshop PDF'), description: i18n( 'PDF to write to /data/support.pdf (overwrites any existing file).', ), warning: null, extensions: ['.pdf'], required: true, }), }) export const uploadSupportPdf = sdk.Action.withInput( // id 'upload-support-pdf', // metadata async ({ effects }) => ({ name: i18n('Upload Support PDF'), description: i18n( 'Upload a PDF that replaces /data/support.pdf on the main volume (overwrites if it exists). Used by nacho as the workshop PDF.', ), warning: null, allowedStatuses: 'any', group: null, visibility: 'enabled', }), // input inputSpec, // prefill — file pickers can't be prefilled async ({ effects }) => {}, // run async ({ effects, input }) => { try { const pdfBuffer = await readFile(input.pdf.path) const res = await sdk.SubContainer.withTemp( effects, { imageId: 'spaces' }, sdk.Mounts.of().mountVolume({ volumeId: 'main', subpath: null, mountpoint: dataDir, readonly: false, }), 'upload-support-pdf', (subc) => subc.exec( [ 'sh', '-c', `cat > ${SUPPORT_PDF_PATH} && chmod 644 ${SUPPORT_PDF_PATH}`, ], { input: pdfBuffer, user: 'root' }, ), ) if (res.exitCode !== 0) { return { version: '1', title: i18n('Failure'), message: i18n('Could not write support PDF: ${error}', { error: ((res.stderr ?? '').toString() || `exit ${res.exitCode}`).slice(0, 300), }), result: null, } } return { version: '1', title: i18n('Success'), message: i18n( 'Workshop PDF written to /data/support.pdf (${size} bytes).', { size: String(input.pdf.commitment.size) }, ), result: null, } } catch (e) { return { version: '1', title: i18n('Failure'), message: i18n('Could not write support PDF: ${error}', { error: (e as Error)?.message ?? String(e), }), result: null, } } }, )