Page MenuHomeSealhub

file.ts
No OneTemporary

import { hasFieldOfType, is, predicates } from "@sealcode/ts-predicates";
import { Context } from "koa";
import { FileManager, FilePointer } from "@sealcode/file-manager";
import { FormDataValue } from "../form-types.js";
import { FieldParseResult, FormField } from "./field.js";
export type FileContainer = {
old: FilePointer | null;
new: FilePointer | null;
};
export class File<Required extends boolean> extends FormField<
Required,
FileContainer
> {
constructor(
public readonly required: Required,
public file_manager: FileManager
) {
super(required);
}
async parse(
_ctx: Context,
raw_value: FormDataValue
): Promise<FieldParseResult<FileContainer>> {
let result;
if (is(raw_value, predicates.string)) {
const file = await this.file_manager.fromToken(raw_value);
result = {
parsed_value: { old: file, new: null },
error: null,
parsable: true as const,
};
} else if (!is(raw_value, predicates.object)) {
result = {
parsed_value: null,
error: "Expected a field with old.id:string, old.filename:string and new?:File",
parsable: false as const,
};
} else {
const old_file = hasFieldOfType(
raw_value,
"old",
predicates.or(predicates.string, predicates.object)
)
? await this.file_manager.toPointer(
raw_value.old as string | FilePointer
)
: null;
const new_file =
hasFieldOfType(
raw_value,
"new",
predicates.array(predicates.object)
) && raw_value.new[0] instanceof FilePointer
? raw_value.new[0]
: null;
result = {
parsable: true as const,
error: null,
parsed_value: { old: old_file, new: new_file },
};
}
return result;
}
async getDatabaseValue(ctx: Context, data: Record<string, FormDataValue>) {
const { parsed } = await this.getValue(ctx, data);
return parsed.new || undefined;
}
getEmptyValue() {
return { old: null, new: null };
}
}

File Metadata

Mime Type
text/x-java
Expires
Tue, Dec 24, 14:02 (17 h, 38 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557232
Default Alt Text
file.ts (1 KB)

Event Timeline