Page MenuHomeSealhub

file.ts
No OneTemporary

import {
hasField,
hasFieldOfType,
hasShape,
is,
predicates,
} from "@sealcode/ts-predicates";
import { Context } from "koa";
import { File as SealiousFile } from "sealious";
import { FormDataValue } from "../form";
import { FieldParseResult, FormField } from "./field";
export type FileContainer = {
old: SealiousFile | null;
new: SealiousFile | null;
};
export class File<Required extends boolean> extends FormField<
Required,
FileContainer
> {
async parse(
ctx: Context,
raw_value: FormDataValue
): Promise<FieldParseResult<FileContainer>> {
let result;
if (
raw_value instanceof SealiousFile ||
(is(raw_value, predicates.object) &&
hasShape(
{
filename: predicates.string,
id: predicates.string,
},
raw_value
) &&
raw_value.id)
) {
const file = await SealiousFile.fromID(
ctx.$app,
raw_value.id as string
);
result = {
parsed_value: { old: file, new: null },
error: null,
parsable: true as true,
};
} 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 false,
};
} else {
const old_file =
hasFieldOfType(raw_value, "old", predicates.object) &&
hasShape(
{ id: predicates.string, filename: predicates.string },
raw_value.old
) &&
raw_value.old.id
? await SealiousFile.fromID(ctx.$app, raw_value.old.id)
: null;
const new_file =
hasFieldOfType(
raw_value,
"new",
predicates.array(predicates.object)
) && raw_value.new[0] instanceof SealiousFile
? raw_value.new[0]
: null;
result = {
parsable: true as true,
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, May 27, 23:47 (1 d, 17 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
661968
Default Alt Text
file.ts (2 KB)

Event Timeline