Page MenuHomeSealhub

file.ts
No OneTemporary

import { Context } from "koa";
import { FilePointer } from "@sealcode/file-manager";
import { FlatTemplatable, tempstream } from "tempstream";
import { FileContainer } from "../fields/file.js";
import { FormDataValue } from "../form-types.js";
import { FormControlContext } from "./form-control.js";
import { SimpleInput } from "./simple-input.js";
import { htmlEscape } from "escape-goat";
export class File extends SimpleInput<FileContainer> {
getType() {
return "file";
}
async renderFilePreview(
_ctx: Context,
file: FilePointer | null
): Promise<FlatTemplatable> {
const class_name = "file_preview";
const empty_container = `<div class="${class_name}"></div>`;
if (!file) {
return empty_container;
}
return tempstream/* HTML */ `<div class="${class_name}">
${file.getOriginalFilename()}
</div>`;
}
async preInput(
fctx: FormControlContext,
data: Record<string, FormDataValue>
) {
const { parsed } = await this.field.getValue(fctx.ctx, data);
return tempstream/* HTML */ `${this.renderFilePreview(
fctx.ctx,
parsed.old
)}
<input
type="hidden"
name="${this.field.name}[old]"
value="${htmlEscape(
parsed.old?.token || (await parsed.old?.save(false)) || ""
)}"
form="${fctx.form_id}"
/> `;
}
async getInputAttributes(
fctx: FormControlContext
): Promise<Record<string, string | boolean>> {
const original = await super.getInputAttributes(fctx);
const { parsed } = await this.field.getValue(fctx.ctx, fctx.data);
return {
...original,
name: this.field.name + "[new]",
required: original.required && !parsed.new,
value: false,
};
}
}

File Metadata

Mime Type
text/x-java
Expires
Tue, Jun 17, 03:28 (1 d, 10 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
635970
Default Alt Text
file.ts (1 KB)

Event Timeline