91 lines
2.5 KiB
TypeScript
91 lines
2.5 KiB
TypeScript
import dayjs from 'dayjs';
|
|
export const getTips = (key: string) => {
|
|
return keysTips.find((item) => item.key === key)?.tips;
|
|
};
|
|
export const keysTips = [
|
|
{
|
|
key: 'share',
|
|
tips: `共享设置
|
|
1. 设置公共可以直接访问
|
|
2. 设置受保护需要登录后访问
|
|
3. 设置私有只有自己可以访问。\n
|
|
受保护可以设置密码,设置访问的用户名。切换共享状态后,需要重新设置密码和用户名。 不设置,默认是只能自己访问。`,
|
|
},
|
|
{
|
|
key: 'content-type',
|
|
tips: `内容类型,设置文件的内容类型。默认不要修改。`,
|
|
},
|
|
{
|
|
key: 'app-source',
|
|
tips: `应用来源,上传方式。默认不要修改。`,
|
|
},
|
|
{
|
|
key: 'cache-control',
|
|
tips: `缓存控制,设置文件的缓存控制。默认不要修改。`,
|
|
},
|
|
{
|
|
key: 'password',
|
|
tips: `密码,设置文件的密码。不设置默认是所有人都可以访问。`,
|
|
},
|
|
{
|
|
key: 'usernames',
|
|
tips: `用户名,设置文件的用户名。不设置默认是所有人都可以访问。`,
|
|
parse: (value: string) => {
|
|
if (!value) {
|
|
return [];
|
|
}
|
|
return value.split(',');
|
|
},
|
|
stringify: (value: string[]) => {
|
|
if (!value) {
|
|
return '';
|
|
}
|
|
return value.join(',');
|
|
},
|
|
},
|
|
{
|
|
key: 'expiration-time',
|
|
tips: `过期时间,设置文件的过期时间。不设置默认是永久。`,
|
|
parse: (value: Date) => {
|
|
if (!value) {
|
|
return null;
|
|
}
|
|
return dayjs(value);
|
|
},
|
|
stringify: (value?: dayjs.Dayjs) => {
|
|
if (!value) {
|
|
return '';
|
|
}
|
|
return value.toISOString();
|
|
},
|
|
},
|
|
];
|
|
export class KeyParse {
|
|
static parse(metadata: Record<string, any>) {
|
|
const keys = Object.keys(metadata);
|
|
const newMetadata = {};
|
|
keys.forEach((key) => {
|
|
const tip = keysTips.find((item) => item.key === key);
|
|
if (tip && tip.parse) {
|
|
newMetadata[key] = tip.parse(metadata[key]);
|
|
} else {
|
|
newMetadata[key] = metadata[key];
|
|
}
|
|
});
|
|
return newMetadata;
|
|
}
|
|
static stringify(metadata: Record<string, any>) {
|
|
const keys = Object.keys(metadata);
|
|
const newMetadata = {};
|
|
keys.forEach((key) => {
|
|
const tip = keysTips.find((item) => item.key === key);
|
|
if (tip && tip.stringify) {
|
|
newMetadata[key] = tip.stringify(metadata[key]);
|
|
} else {
|
|
newMetadata[key] = metadata[key];
|
|
}
|
|
});
|
|
return newMetadata;
|
|
}
|
|
}
|