Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F7188228
file.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
file.ts
View Options
import
{
Field
,
Context
,
File
}
from
"../../../main"
;
import
type
{
FileDBEntry
}
from
"../../../data-structures/file"
;
import
{
hasField
,
hasFieldOfType
,
is
,
predicates
,
}
from
"@sealcode/ts-predicates"
;
export
type
FileStorageFormat
=
FileDBEntry
;
export
abstract
class
FileStorage
extends
Field
{
handles_large_data
=
true
;
get_default_file
:
(
context
:
Context
)
=>
Promise
<
File
>
;
async
isProperValue
(
context
:
Context
,
value
:
File
|
[
File
])
{
if
(
typeof
value
===
"string"
)
{
return
Field
.
valid
();
}
if
(
Array
.
isArray
(
value
))
{
if
(
value
.
length
!==
1
)
{
throw
new
Error
(
"If you use array for this field, it can only have one file element"
);
}
value
=
value
[
0
]
as
File
;
}
if
(
value
===
null
||
value
instanceof
File
)
{
return
Field
.
valid
();
}
if
(
is
(
value
,
predicates
.
object
)
&&
hasFieldOfType
(
value
,
"id"
,
predicates
.
string
)
&&
hasFieldOfType
(
value
,
"filename"
,
predicates
.
string
)
&&
hasField
(
"data"
,
value
)
)
{
return
Field
.
valid
();
}
return
Field
.
invalid
(
context
.
app
.
i18n
(
"invalid_file_storage"
));
}
setParams
(
params
:
{
get_default_file
:
(
context
:
Context
)
=>
Promise
<
File
>
;
})
{
this
.
get_default_file
=
params
.
get_default_file
;
}
async
encode
(
_
:
Context
,
file
:
File
|
[
File
])
{
if
(
file
===
null
)
{
return
null
;
}
if
(
Array
.
isArray
(
file
))
{
if
(
file
.
length
!==
1
)
{
throw
new
Error
(
"If you use array for this field, it can only have one file element"
);
}
file
=
file
[
0
]
as
File
;
}
if
(
!
file
.
id
)
{
await
file
.
save
();
}
return
file
.
toDBEntry
();
}
}
/** Takes a {@File} instance as input, stores it in the FS and then decodes to a URL.
*
* **Params**:
* - `get_default_file` - ()=>Promise<{@link File}> - if no file is provided, then this file will be used in it's stead
*/
export
default
class
FileField
extends
FileStorage
{
typeName
=
"file"
;
async
decode
(
_
:
Context
,
db_value
:
FileStorageFormat
|
null
,
__
:
any
,
format
?:
"url"
|
"file"
)
{
if
(
db_value
===
null
)
{
return
null
;
}
const
file
=
await
File
.
fromID
(
this
.
app
,
db_value
.
id
);
if
(
format
==
undefined
)
{
return
{
id
:
file
.
id
,
filename
:
file
.
filename
};
}
else
if
(
format
==
"file"
)
{
return
file
;
}
else
{
return
file
.
getURL
();
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Tue, Jul 8, 07:59 (1 d, 9 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
802249
Default Alt Text
file.ts (2 KB)
Attached To
Mode
rS Sealious
Attached
Detach File
Event Timeline
Log In to Comment