Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F22398399
file.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None
file.ts
View Options
import
{
hasFieldOfType
,
is
,
predicates
}
from
"@sealcode/ts-predicates"
;
import
{
Context
}
from
"koa"
;
import
{
FilePointer
,
PathFilePointer
}
from
"@sealcode/file-manager"
;
import
{
FormDataValue
}
from
"../form-types.js"
;
import
{
FieldParseResult
,
FormField
}
from
"./field.js"
;
import
{
File
as
FileControl
}
from
"../controls/file.js"
;
import
{
FormControl
}
from
"../controls/form-control.js"
;
import
{
ImageValue
}
from
"sealious"
;
export
type
FileContainer
=
{
old
:
FilePointer
|
null
;
new
:
FilePointer
|
null
;
};
export
class
File
<
Required
extends
boolean
>
extends
FormField
<
Required
,
FileContainer
,
FormControl
>
{
constructor
(
public
readonly
required
:
Required
)
{
super
(
required
);
}
async
parse
(
ctx
:
Context
,
raw_value
:
FormDataValue
)
:
Promise
<
FieldParseResult
<
FileContainer
>>
{
let
result
;
if
(
is
(
raw_value
,
predicates
.
string
))
{
const
file
=
await
ctx
.
$app
.
fileManager
.
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
.
instanceOf
(
PathFilePointer
)
)
&&
raw_value
.
old
)
?
await
ctx
.
$app
.
fileManager
.
toPointer
(
raw_value
.
old
)
:
null
;
const
new_file
=
(
hasFieldOfType
(
raw_value
,
"new"
,
predicates
.
or
(
predicates
.
array
(
predicates
.
instanceOf
(
PathFilePointer
)
),
predicates
.
instanceOf
(
PathFilePointer
)
)
)
)
?
Array
.
isArray
(
raw_value
.
new
)
?
raw_value
.
new
[
0
]
:
raw_value
.
new
:
null
;
result
=
{
parsable
:
true
as
const
,
error
:
null
,
parsed_value
:
{
old
:
old_file
,
new
:
new_file
||
null
},
};
}
return
result
;
}
async
getDatabaseValue
(
ctx
:
Context
,
data
:
Record
<
string
,
FormDataValue
>
)
{
const
{
parsed
}
=
await
this
.
getParsedValue
(
ctx
,
data
);
return
parsed
?
.
new
||
undefined
;
}
getEmptyValue
()
{
return
{
old
:
null
,
new
:
null
};
}
getControl
()
:
FileControl
{
return
new
FileControl
(
this
,
{
label
:
this
.
name
,
autocomplete
:
false
});
}
async
parsedToSealious
(
value
:
FileContainer
)
:
Promise
<
FilePointer
|
undefined
>
{
const
pointer
=
value
?
.
new
||
value
?
.
old
||
undefined
;
if
(
this
.
required
&&
!
pointer
)
{
throw
new
Error
(
"File is required, but no value is provided"
);
}
return
pointer
;
}
async
sealiousValueToForm
(
ctx
:
Context
,
sealiousValue
:
FilePointer
|
ImageValue
|
string
|
null
)
:
Promise
<
{
old
:
FilePointer
|
undefined
}
|
Record
<
string
,
never
>>
{
if
(
!
sealiousValue
)
{
return
{};
}
if
(
sealiousValue
instanceof
ImageValue
)
{
return
{
old
:
sealiousValue
.
toFile
()
};
}
if
(
typeof
sealiousValue
==
"string"
)
{
sealiousValue
=
await
ctx
.
$app
.
fileManager
.
fromToken
(
sealiousValue
);
}
return
{
old
:
sealiousValue
,
// FilePointer
};
}
}
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Fri, Nov 27, 17:22 (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1202299
Default Alt Text
file.ts (3 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline