@yongdall/file 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/index.mjs +23 -0
- package/assets/index.mjs.map +1 -0
- package/assets.yongdall.mjs +4 -0
- package/file-rQo6J1mo.mjs +110 -0
- package/file-rQo6J1mo.mjs.map +1 -0
- package/hooks.yongdall.mjs +12 -0
- package/hooks.yongdall.mjs.map +1 -0
- package/index.d.mts +18 -0
- package/index.mjs +3 -0
- package/package.json +26 -0
- package/routers.yongdall.mjs +128 -0
- package/routers.yongdall.mjs.map +1 -0
package/assets/index.mjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import{User as e,effect as t,pathRoots as n,request as r,selectFile as i,toFileLink as a,toFileURL as o}from"@yongdall/web";var s=Object.defineProperty,c=(e,t)=>{let n={};for(var r in e)s(n,r,{get:e[r],enumerable:!0});return t||s(n,Symbol.toStringTag,{value:`Module`}),n};function l(e,t,n){return e.replace(/\{([a-zA-Z]+)\}/g,(e,r)=>{switch(r){case`YYYY`:case`YYY`:case`YY`:return`${t.getFullYear()}`.slice(-r.length);case`MM`:case`M`:return`${t.getMonth()+1}`.padStart(r.length,`0`);case`DD`:case`D`:return`${t.getDate()}`.padStart(r.length,`0`);case`hh`:case`h`:return`${t.getHours()}`.padStart(r.length,`0`);case`mm`:case`m`:return`${t.getMinutes()}`.padStart(r.length,`0`);case`ss`:case`s`:return`${t.getSeconds()}`.padStart(r.length,`0`);default:if(Object.hasOwn(n,r))return n[r]}return e})}const u=r.clone().path(`fileCabinets`);async function d(e,t,n,{signal:r}={}){let i=u.clone().append`${t}/files/${n}`.put();r&&(i=i.signal(r));let a=await i.json(),{link:o,id:s}=a,c=fetch(o,{method:`put`,body:e,signal:r instanceof AbortSignal?r:null}),l=s?c.then(()=>u.clone().append`${t}/id/${s}`.put().json(),e=>u.clone().append`${t}/id/${s}`.delete().json().then(()=>Promise.reject(e))):c.then(()=>a);if(!(r instanceof AbortSignal))return l;let d=new Promise((e,t)=>{if(r.aborted){t(r.reason);return}r.addEventListener(`abort`,()=>t(r.reason),{once:!0})});return Promise.race([d,l])}function f(t,r){let i=r?.replace(/[\\\/#?{}~@:&^$()]/g,``)||``,a=i.lastIndexOf(`.`),o=a>=0?i.slice(a):``;if(!t||typeof t!=`string`)return null;let s=l(t,new Date,{user:e.current?.id||`_`,filename:i||`_`,extname:o});if(/^\+file:\/\//.test(s)){let{hostname:e,pathname:t,hash:r}=new URL(s.slice(1)),i=e||`_`,a=t.split(`/`).filter(Boolean).join(`/`);return a?{cabinetId:i,path:a,name:decodeURIComponent(r.slice(1)||a.split(`/`).pop()||`文件`),href:`${n.file}/${i}/files/${a}`}:{cabinetId:i,path:a,name:``,href:``}}return null}async function p(e,t){let n=t?.signal,r=await i(t);if(n?.aborted||!r)return null;let a=f(e,r.name),s=`_`,c;a&&({cabinetId:s,path:c}=a);let l=await d(r,s||`_`,c||r.name,{signal:n});if(!n?.aborted)return o(l,r.name)}document.head.appendChild(document.createElement(`style`)).textContent=`
|
|
2
|
+
.yongdall-file-form-field {
|
|
3
|
+
a {
|
|
4
|
+
flex: 1;
|
|
5
|
+
overflow: hidden;
|
|
6
|
+
text-overflow: ellipsis;
|
|
7
|
+
white-space: pre;
|
|
8
|
+
}
|
|
9
|
+
span {
|
|
10
|
+
flex: 1;
|
|
11
|
+
}
|
|
12
|
+
span:first-of-type::after {
|
|
13
|
+
content: '上传中...';
|
|
14
|
+
}
|
|
15
|
+
display: flex;
|
|
16
|
+
width: 100%;
|
|
17
|
+
&.disabled {
|
|
18
|
+
button, span {
|
|
19
|
+
display: none;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}`;var m=(e,n)=>{let r=n?.fileCabinet,i=null,o=document.createElement(`div`);o.className=`yongdall-file-form-field`,e.listen(`focus`,()=>o.focus()),e.listen(`scrollIntoView`,()=>o.scrollIntoView());let s=``,c=o.appendChild(document.createElement(`a`));c.target=`_blank`;let l=o.appendChild(document.createElement(`button`));l.appendChild(document.createTextNode(`移除`));let u=o.appendChild(document.createElement(`span`)),d=o.appendChild(document.createElement(`button`));d.appendChild(document.createTextNode(`取消`));let f=o.appendChild(document.createElement(`span`)),m=o.appendChild(document.createElement(`button`));m.appendChild(document.createTextNode(`上传`)),l.addEventListener(`click`,()=>{e.value=e.nullable?null:``}),d.addEventListener(`click`,()=>{i?.abort()}),m.addEventListener(`click`,async()=>{i?.abort();let t=i=new AbortController,{signal:n}=i;d.hidden=!1,u.hidden=!1,f.hidden=!0,m.hidden=!0,n.addEventListener(`abort`,()=>{d.hidden=!0,u.hidden=!0,f.hidden=!1,m.hidden=!1},{once:!0});try{let t=await p(r,{signal:n});if(typeof t!=`string`)return;e.value=t||(e.nullable?null:``)}finally{n.aborted||t.abort()}}),c.addEventListener(`click`,t=>{e.disabled&&(t.preventDefault(),t.stopPropagation())}),e.editable?t(()=>{e.disabled||e.readonly?o.classList.add(`disabled`):o.classList.remove(`disabled`)},e.signal):o.classList.add(`disabled`),l.hidden=!0,c.hidden=!0,u.hidden=!0,d.hidden=!0;function h(e){let t=e||``;if(t===s)return;s=t,i?.abort(),i=null;let n=a(t);if(!n?.href){c.href=``,c.innerText=``,l.hidden=!0,c.hidden=!0,f.hidden=!1,m.hidden=!1;return}c.href=n.href,c.innerText=n.name||n.href,c.download=n.name||`文件`,l.hidden=!1,c.hidden=!1,f.hidden=!0,m.hidden=!0}return t(()=>h(e.value),e.signal),o},h=c({formFields:()=>g});const g=[{type:`file`,input:m}];export{h as hooks,f as parseFileURL,p as upload};
|
|
23
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../../plugins/file/common/replacePath.mjs","../../../plugins/file/assets/services.mjs","../../../plugins/file/assets/web.mjs","../../../plugins/file/assets/input.mjs","../../../plugins/file/assets/hooks.mjs"],"sourcesContent":["\n/**\n * \n * @param {string} path \n * @param {Date} date \n * @param {Record<string, any>} data \n */\nexport default function replacePath(path, date, data) {\n\treturn path.replace(/\\{([a-zA-Z]+)\\}/g, (_, v) => {\n\t\tswitch (v) {\n\t\t\tcase 'YYYY':\n\t\t\tcase 'YYY':\n\t\t\tcase 'YY':\n\t\t\t\tconst year = date.getFullYear();\n\t\t\t\treturn `${year}`.slice(-v.length);\n\t\t\tcase 'MM':\n\t\t\tcase 'M':\n\t\t\t\tconst month = date.getMonth() + 1;\n\t\t\t\treturn `${month}`.padStart(v.length, '0');\n\t\t\tcase 'DD':\n\t\t\tcase 'D':\n\t\t\t\tconst day = date.getDate();\n\t\t\t\treturn `${day}`.padStart(v.length, '0');\n\t\t\tcase 'hh':\n\t\t\tcase 'h':\n\t\t\t\tconst hour = date.getHours();\n\t\t\t\treturn `${hour}`.padStart(v.length, '0');\n\t\t\tcase 'mm':\n\t\t\tcase 'm':\n\t\t\t\tconst minute = date.getMinutes();\n\t\t\t\treturn `${minute}`.padStart(v.length, '0');\n\t\t\tcase 'ss':\n\t\t\tcase 's':\n\t\t\t\tconst second = date.getSeconds();\n\t\t\t\treturn `${second}`.padStart(v.length, '0');\n\t\t\tdefault: {\n\t\t\t\tif (Object.hasOwn(data, v)) {\n\t\t\t\t\treturn data[v];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn _;\n\t});\n}\n","/** @import { DotRequest } from '@yongdall/web' */\nimport { request } from '@yongdall/web';\nexport const fileCabinetApi = request.clone().path('fileCabinets');\n\n/**\n * \n * @param {File} file \n * @param {string} cabinetId\n * @param {string} path\n * @param {object} [options] \n * @param {DotRequest.Signal} [options.signal] \n * @returns {Promise<{domain: string; path: string; id: string}>} \n */\nexport async function uploadFile(file, cabinetId, path, { signal } = {}) {\n\tlet r = fileCabinetApi.clone().append`${cabinetId}/files/${path}`.put();\n\tif (signal) { r = r.signal(signal); }\n\tconst target = await r.json();\n\tconst { link, id } = target;\n\tconst promise1 = fetch(link, {\n\t\tmethod: 'put',\n\t\tbody: file,\n\t\tsignal: signal instanceof AbortSignal ? signal : null,\n\t});\n\n\tconst promise = id\n\t\t? promise1.then(\n\t\t\t() => fileCabinetApi.clone().append`${cabinetId}/id/${id}`.put().json(),\n\t\t\te => fileCabinetApi.clone().append`${cabinetId}/id/${id}`.delete().json().then(() => Promise.reject(e)))\n\t\t: promise1.then(() => target);\n\tif (!(signal instanceof AbortSignal)) { return promise; }\n\t/** @type {Promise<never>} */\n\tconst signalPromise = new Promise((_, reject) => {\n\t\tif (signal.aborted) {\n\t\t\treject(signal.reason);\n\t\t\treturn;\n\t\t}\n\t\tsignal.addEventListener('abort', () => reject(signal.reason), {once: true});\n\t});\n\treturn Promise.race([signalPromise, promise]);\n}\n","import { pathRoots, selectFile, toFileURL, User } from '@yongdall/web';\nimport replacePath from './common/replacePath.mjs';\nimport { uploadFile } from './services.mjs';\n\n/**\n * \n * @param {string} [path] \n * @param {string} [name] \n * @returns \n */\nexport function parseFileURL(path, name) {\n\tconst filename = name?.replace(/[\\\\\\/#?{}~@:&^$()]/g, '') || '';\n\tconst index = filename.lastIndexOf('.');\n\tconst extname = index >= 0 ? filename.slice(index) : '';\n\tif (!path || typeof path !== 'string') { return null; }\n\tconst date = new Date();\n\tconst url = replacePath(path, date, {\n\t\tuser: User.current?.id || '_', filename: filename || '_', extname,\n\t});\n\tif (/^\\+file:\\/\\//.test(url)) {\n\t\tconst { hostname, pathname, hash } = new URL(url.slice(1));\n\t\tconst cabinetId = hostname || '_';\n\t\tconst path = pathname.split('/').filter(Boolean).join('/');\n\t\tif (!path) { return { cabinetId, path, name: '', href: '' }; }\n\t\tconst name = decodeURIComponent(hash.slice(1) || path.split('/').pop() || '文件');\n\t\tconst href = `${pathRoots.file}/${cabinetId}/files/${path}`;\n\t\treturn { cabinetId, path, name, href };\n\t}\n\treturn null;\n}\n/**\n * \n * @param {string} fileCabinetUrl \n * @param {object} [options]\n * @param {FileSystemHandleQueryOptions['types']} [options.types]\n * @param {boolean} [options.excludeAcceptAllOption]\n * @param {AbortSignal} [options.signal] \n * @returns \n */\nexport async function upload(fileCabinetUrl, options) {\n\tconst signal = options?.signal;\n\tconst file = await selectFile(options);\n\tif (signal?.aborted || !file) { return null; }\n\tconst u = parseFileURL(fileCabinetUrl, file.name);\n\tlet cabinetId = '_', path;\n\tif (u) { ({ cabinetId, path } = u); }\n\tconst s = await uploadFile(file, cabinetId || '_', path || file.name, { signal });\n\tif (signal?.aborted) { return; }\n\treturn toFileURL(s, file.name);\n\n}\n","import { effect, toFileLink } from '@yongdall/web';\nimport { upload } from './web.mjs';\n/** @import { FieldComponent } from '@yongdall/web' */\n\n\nconst style = `\n.yongdall-file-form-field {\n\ta {\n\t\tflex: 1;\n\t\toverflow: hidden;\n\t\ttext-overflow: ellipsis;\n\t\twhite-space: pre;\n\t}\n\tspan {\n\t\tflex: 1;\n\t}\n\tspan:first-of-type::after {\n\t\tcontent: '上传中...';\n\t}\n\tdisplay: flex;\n\twidth: 100%;\n\t&.disabled {\n\t\tbutton, span {\n\t\t\tdisplay: none;\n\t\t}\n\t}\n}`\ndocument.head.appendChild(document.createElement('style')).textContent = style;\n\n/** @type {FieldComponent} */\nconst Component = (ctx, options) => {\n\tconst fileCabinetUrl = options?.fileCabinet;\n\t/** @type {AbortController?} */\n\tlet ac = null;\n\n\tconst root = document.createElement('div');\n\troot.className = 'yongdall-file-form-field';\n\tctx.listen('focus', () => root.focus());\n\tctx.listen('scrollIntoView', () => root.scrollIntoView());\n\tlet value = '';\n\n\tconst input = root.appendChild(document.createElement('a'));\n\tinput.target = '_blank';\n\tconst removeButton = root.appendChild(document.createElement('button'));\n\tremoveButton.appendChild(document.createTextNode('移除'));\n\tconst uploadTip = root.appendChild(document.createElement('span'));\n\tconst cancelButton = root.appendChild(document.createElement('button'));\n\tcancelButton.appendChild(document.createTextNode('取消'));\n\tconst uploadStart = root.appendChild(document.createElement('span'));\n\tconst uploadButton = root.appendChild(document.createElement('button'));\n\tuploadButton.appendChild(document.createTextNode('上传'));\n\n\tremoveButton.addEventListener('click', () => { ctx.value = ctx.nullable ? null : ''; });\n\tcancelButton.addEventListener('click', () => { ac?.abort(); });\n\n\n\n\tuploadButton.addEventListener('click', async () => {\n\t\tac?.abort();\n\t\tconst tac = ac = new AbortController();\n\t\tconst { signal } = ac;\n\t\tcancelButton.hidden = false;\n\t\tuploadTip.hidden = false;\n\t\tuploadStart.hidden = true;\n\t\tuploadButton.hidden = true;\n\t\tsignal.addEventListener('abort', () => {\n\t\t\tcancelButton.hidden = true;\n\t\t\tuploadTip.hidden = true;\n\t\t\tuploadStart.hidden = false;\n\t\t\tuploadButton.hidden = false;\n\t\t}, {once: true});\n\t\ttry {\n\t\t\tconst url = await upload(fileCabinetUrl, {signal});\n\t\t\tif (typeof url !== 'string') { return; }\n\t\t\tctx.value = url || (ctx.nullable ? null : '');\n\t\t} finally {\n\t\t\tif (!signal.aborted) { tac.abort(); }\n\t\t}\n\t})\n\n\tinput.addEventListener('click', (event) => {\n\t\tif (!ctx.disabled) { return }\n\t\tevent.preventDefault();\n\t\tevent.stopPropagation();\n\t})\n\n\tif (!ctx.editable) {\n\t\troot.classList.add('disabled');\n\t} else {\n\t\teffect(() => {\n\t\t\tif (ctx.disabled || ctx.readonly) {\n\t\t\t\troot.classList.add('disabled');\n\t\t\t} else {\n\t\t\t\troot.classList.remove('disabled');\n\t\t\t}\n\t\t}, ctx.signal);\n\t}\n\n\n\tremoveButton.hidden = true;\n\tinput.hidden = true;\n\tuploadTip.hidden = true;\n\tcancelButton.hidden = true;\n\n\t/**\n\t * \n\t * @param {*} v \n\t * @returns \n\t */\n\tfunction update(v) {\n\t\tconst val = v || '';\n\t\tif (val === value) { return; }\n\t\tvalue = val;\n\t\tac?.abort();\n\t\tac = null;\n\t\tconst url = toFileLink(val);\n\t\tif (!url?.href) {\n\t\t\tinput.href = '';\n\t\t\tinput.innerText = '';\n\t\t\tremoveButton.hidden = true;\n\t\t\tinput.hidden = true;\n\t\t\tuploadStart.hidden = false;\n\t\t\tuploadButton.hidden = false;\n\t\t\treturn;\n\t\t}\n\t\tinput.href = url.href;\n\t\tinput.innerText = url.name || url.href;\n\t\tinput.download = url.name || '文件';\n\t\tremoveButton.hidden = false;\n\t\tinput.hidden = false;\n\t\tuploadStart.hidden = true;\n\t\tuploadButton.hidden = true;\n\t}\n\teffect(() => update(ctx.value), ctx.signal);\n\treturn root;\n};\n\nexport default Component;\n","/** @import { Hooks } from '@yongdall/web' */\n\nimport input from './input.mjs';\n\n\n\n/** @type {Hooks.Define['formFields']} */\nexport const formFields = [\n\t{type: 'file', input },\n]\n"],"mappings":"gRAOA,SAAwB,EAAY,EAAM,EAAM,EAAM,CACrD,OAAO,EAAK,QAAQ,oBAAqB,EAAG,IAAM,CACjD,OAAQ,EAAR,CACC,IAAK,OACL,IAAK,MACL,IAAK,KAEJ,MAAO,GADM,EAAK,aAAa,GACd,MAAM,CAAC,EAAE,OAAO,CAClC,IAAK,KACL,IAAK,IAEJ,MAAO,GADO,EAAK,UAAU,CAAG,IACd,SAAS,EAAE,OAAQ,IAAI,CAC1C,IAAK,KACL,IAAK,IAEJ,MAAO,GADK,EAAK,SAAS,GACV,SAAS,EAAE,OAAQ,IAAI,CACxC,IAAK,KACL,IAAK,IAEJ,MAAO,GADM,EAAK,UAAU,GACX,SAAS,EAAE,OAAQ,IAAI,CACzC,IAAK,KACL,IAAK,IAEJ,MAAO,GADQ,EAAK,YAAY,GACb,SAAS,EAAE,OAAQ,IAAI,CAC3C,IAAK,KACL,IAAK,IAEJ,MAAO,GADQ,EAAK,YAAY,GACb,SAAS,EAAE,OAAQ,IAAI,CAC3C,QACC,GAAI,OAAO,OAAO,EAAM,EAAE,CACzB,OAAO,EAAK,GAIf,OAAO,GACN,CCxCH,MAAa,EAAiB,EAAQ,OAAO,CAAC,KAAK,eAAe,CAWlE,eAAsB,EAAW,EAAM,EAAW,EAAM,CAAE,UAAW,EAAE,CAAE,CACxE,IAAI,EAAI,EAAe,OAAO,CAAC,MAAM,GAAG,EAAU,SAAS,IA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M,KAAO,GACb,EAAM,UAAY,GAClB,EAAa,OAAS,GACtB,EAAM,OAAS,GACf,EAAY,OAAS,GACrB,EAAa,OAAS,GACtB,OAED,EAAM,KAAO,EAAI,KACjB,EAAM,UAAY,EAAI,MAAQ,EAAI,KAClC,EAAM,SAAW,EAAI,MAAQ,KAC7B,EAAa,OAAS,GACtB,EAAM,OAAS,GACf,EAAY,OAAS,GACrB,EAAa,OAAS,GAGvB,OADA,MAAa,EAAO,EAAI,MAAM,CAAE,EAAI,OAAO,CACpC,2BC/HR,MAAa,EAAa,CACzB,CAAC,KAAM,OAAQ,MAAA,EAAO,CACtB"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { createField, createModel, now, uuid } from "@yongdall/model";
|
|
2
|
+
|
|
3
|
+
//#region plugins/file/models/FileCabinet.mjs
|
|
4
|
+
var FileCabinet_default = createModel("fileCabinet", {
|
|
5
|
+
label: createField("string", {
|
|
6
|
+
nullable: false,
|
|
7
|
+
default: "",
|
|
8
|
+
no: 1,
|
|
9
|
+
label: "名称",
|
|
10
|
+
layout: { colSpan: 12 }
|
|
11
|
+
}),
|
|
12
|
+
mode: createField("string", {
|
|
13
|
+
default: "map",
|
|
14
|
+
nullable: false,
|
|
15
|
+
immutable: true,
|
|
16
|
+
no: 2,
|
|
17
|
+
label: "模式",
|
|
18
|
+
renderer: {
|
|
19
|
+
component: "select",
|
|
20
|
+
values: [{
|
|
21
|
+
value: "map",
|
|
22
|
+
label: "映射"
|
|
23
|
+
}]
|
|
24
|
+
}
|
|
25
|
+
}),
|
|
26
|
+
storeId: createField("string", {
|
|
27
|
+
nullable: false,
|
|
28
|
+
no: 3,
|
|
29
|
+
label: "存储单元",
|
|
30
|
+
renderer: {
|
|
31
|
+
component: "select",
|
|
32
|
+
type: "model",
|
|
33
|
+
name: "database",
|
|
34
|
+
value: "name",
|
|
35
|
+
filter: "type=rdb"
|
|
36
|
+
},
|
|
37
|
+
layout: { colSpan: 4 }
|
|
38
|
+
}),
|
|
39
|
+
storePath: createField("string", {
|
|
40
|
+
nullable: false,
|
|
41
|
+
default: "",
|
|
42
|
+
no: 4,
|
|
43
|
+
label: "根路径",
|
|
44
|
+
layout: { colSpan: 4 }
|
|
45
|
+
}),
|
|
46
|
+
uploadPath: createField("string", {
|
|
47
|
+
nullable: false,
|
|
48
|
+
default: "",
|
|
49
|
+
no: 5,
|
|
50
|
+
label: "上传路径",
|
|
51
|
+
layout: { colSpan: 4 }
|
|
52
|
+
}),
|
|
53
|
+
id: createField("string", {
|
|
54
|
+
nullable: false,
|
|
55
|
+
no: 7,
|
|
56
|
+
default: uuid,
|
|
57
|
+
label: "#",
|
|
58
|
+
renderer: "string",
|
|
59
|
+
layout: { colSpan: 12 },
|
|
60
|
+
primary: 1
|
|
61
|
+
}),
|
|
62
|
+
createdAt: createField("timestamp", {
|
|
63
|
+
nullable: false,
|
|
64
|
+
creating: now,
|
|
65
|
+
no: 8,
|
|
66
|
+
label: "创建时间"
|
|
67
|
+
}),
|
|
68
|
+
updatedAt: createField("timestamp", {
|
|
69
|
+
nullable: false,
|
|
70
|
+
updating: now,
|
|
71
|
+
no: 9,
|
|
72
|
+
label: "更新时间"
|
|
73
|
+
}),
|
|
74
|
+
public: createField("bool", {
|
|
75
|
+
nullable: false,
|
|
76
|
+
default: true,
|
|
77
|
+
no: 6,
|
|
78
|
+
label: "是否公开",
|
|
79
|
+
layout: { colSpan: 4 }
|
|
80
|
+
}),
|
|
81
|
+
description: createField("text", {
|
|
82
|
+
nullable: false,
|
|
83
|
+
default: "",
|
|
84
|
+
no: 10,
|
|
85
|
+
label: "描述"
|
|
86
|
+
})
|
|
87
|
+
}, {
|
|
88
|
+
label: "文件柜",
|
|
89
|
+
labelField: "label",
|
|
90
|
+
permissions: [{
|
|
91
|
+
fields: ["id", "label"],
|
|
92
|
+
authorizations: ["options"]
|
|
93
|
+
}, {
|
|
94
|
+
permission: "system:fileCabinet",
|
|
95
|
+
fields: "*",
|
|
96
|
+
authorizations: [
|
|
97
|
+
"query",
|
|
98
|
+
"read",
|
|
99
|
+
"create",
|
|
100
|
+
"update",
|
|
101
|
+
"destroy",
|
|
102
|
+
"add",
|
|
103
|
+
"remove"
|
|
104
|
+
]
|
|
105
|
+
}]
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
//#endregion
|
|
109
|
+
export { FileCabinet_default as t };
|
|
110
|
+
//# sourceMappingURL=file-rQo6J1mo.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-rQo6J1mo.mjs","names":[],"sources":["../../plugins/file/models/FileCabinet.mjs"],"sourcesContent":["import { createField, createModel, now, uuid } from '@yongdall/model';\n\nexport default createModel('fileCabinet', {\n\tlabel: createField('string', { nullable: false, default: '', no: 1, label: '名称', layout: { colSpan: 12 } }),\n\tmode: createField('string', {\n\t\tdefault: 'map', nullable: false, immutable: true, no: 2, label: '模式', renderer: {\n\t\t\tcomponent: 'select', values: [\n\t\t\t\t{ value: 'map', label: '映射' },\n\t\t\t\t// {value: 'archive', label: '归档'},\n\t\t\t]\n\t\t}\n\t}),\n\n\tstoreId: createField('string', { nullable: false, no: 3, label: '存储单元', renderer: { component: 'select', type: 'model', name: 'database', value: 'name', filter: 'type=rdb' }, layout: { colSpan: 4 } }),\n\tstorePath: createField('string', { nullable: false, default: '', no: 4, label: '根路径', layout: { colSpan: 4 } }),\n\tuploadPath: createField('string', { nullable: false, default: '', no: 5, label: '上传路径', layout: { colSpan: 4 } }),\n\n\tid: createField('string', { nullable: false, no: 7, default: uuid, label: '#', renderer: 'string', layout: { colSpan: 12 }, primary: 1 }),\n\n\n\n\tcreatedAt: createField('timestamp', { nullable: false, creating: now, no: 8, label: '创建时间' }),\n\tupdatedAt: createField('timestamp', { nullable: false, updating: now, no: 9, label: '更新时间' }),\n\n\n\n\tpublic: createField('bool', { nullable: false, default: true, no: 6, label: '是否公开', layout: { colSpan: 4 } }),\n\n\n\n\tdescription: createField('text', { nullable: false, default: '', no: 10, label: '描述' }),\n}, {\n\tlabel: '文件柜',\n\tlabelField: 'label',\n\tpermissions: [\n\t\t{ fields: ['id', 'label'], authorizations: ['options'] },\n\t\t{ permission: 'system:fileCabinet', fields: '*', authorizations: ['query', 'read', 'create', 'update', 'destroy', 'add', 'remove'] },\n\t],\n});\n"],"mappings":";;;AAEA,0BAAe,YAAY,eAAe;CACzC,OAAO,YAAY,UAAU;EAAE,UAAU;EAAO,SAAS;EAAI,IAAI;EAAG,OAAO;EAAM,QAAQ,EAAE,SAAS,IAAI;EAAE,CAAC;CAC3G,MAAM,YAAY,UAAU;EAC3B,SAAS;EAAO,UAAU;EAAO,WAAW;EAAM,IAAI;EAAG,OAAO;EAAM,UAAU;GAC/E,WAAW;GAAU,QAAQ,CAC5B;IAAE,OAAO;IAAO,OAAO;IAAM,CAE7B;GACD;EACD,CAAC;CAEF,SAAS,YAAY,UAAU;EAAE,UAAU;EAAO,IAAI;EAAG,OAAO;EAAQ,UAAU;GAAE,WAAW;GAAU,MAAM;GAAS,MAAM;GAAY,OAAO;GAAQ,QAAQ;GAAY;EAAE,QAAQ,EAAE,SAAS,GAAG;EAAE,CAAC;CACxM,WAAW,YAAY,UAAU;EAAE,UAAU;EAAO,SAAS;EAAI,IAAI;EAAG,OAAO;EAAO,QAAQ,EAAE,SAAS,GAAG;EAAE,CAAC;CAC/G,YAAY,YAAY,UAAU;EAAE,UAAU;EAAO,SAAS;EAAI,IAAI;EAAG,OAAO;EAAQ,QAAQ,EAAE,SAAS,GAAG;EAAE,CAAC;CAEjH,IAAI,YAAY,UAAU;EAAE,UAAU;EAAO,IAAI;EAAG,SAAS;EAAM,OAAO;EAAK,UAAU;EAAU,QAAQ,EAAE,SAAS,IAAI;EAAE,SAAS;EAAG,CAAC;CAIzI,WAAW,YAAY,aAAa;EAAE,UAAU;EAAO,UAAU;EAAK,IAAI;EAAG,OAAO;EAAQ,CAAC;CAC7F,WAAW,YAAY,aAAa;EAAE,UAAU;EAAO,UAAU;EAAK,IAAI;EAAG,OAAO;EAAQ,CAAC;CAI7F,QAAQ,YAAY,QAAQ;EAAE,UAAU;EAAO,SAAS;EAAM,IAAI;EAAG,OAAO;EAAQ,QAAQ,EAAE,SAAS,GAAG;EAAE,CAAC;CAI7G,aAAa,YAAY,QAAQ;EAAE,UAAU;EAAO,SAAS;EAAI,IAAI;EAAI,OAAO;EAAM,CAAC;CACvF,EAAE;CACF,OAAO;CACP,YAAY;CACZ,aAAa,CACZ;EAAE,QAAQ,CAAC,MAAM,QAAQ;EAAE,gBAAgB,CAAC,UAAU;EAAE,EACxD;EAAE,YAAY;EAAsB,QAAQ;EAAK,gBAAgB;GAAC;GAAS;GAAQ;GAAU;GAAU;GAAW;GAAO;GAAS;EAAE,CACpI;CACD,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { t as FileCabinet_default } from "./file-rQo6J1mo.mjs";
|
|
2
|
+
|
|
3
|
+
//#region plugins/file/hooks.yongdall.mjs
|
|
4
|
+
/** @import { Hooks } from '@yongdall/core' */
|
|
5
|
+
/** @type {Hooks.Define['models']} */
|
|
6
|
+
const models = { fileCabinet: FileCabinet_default };
|
|
7
|
+
/** @type {Hooks.Define['migrationModels']} */
|
|
8
|
+
const migrationModels = [FileCabinet_default];
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
export { migrationModels, models };
|
|
12
|
+
//# sourceMappingURL=hooks.yongdall.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.yongdall.mjs","names":["FileCabinet"],"sources":["../../plugins/file/hooks.yongdall.mjs"],"sourcesContent":["import { FileCabinet } from './index.mjs';\n/** @import { Hooks } from '@yongdall/core' */\n\n/** @type {Hooks.Define['models']} */\nexport const models = {\n\tfileCabinet: FileCabinet,\n};\n\n\n/** @type {Hooks.Define['migrationModels']} */\nexport const migrationModels = [\n\tFileCabinet,\n]\n"],"mappings":";;;;;AAIA,MAAa,SAAS,EACrB,aAAaA,qBACb;;AAID,MAAa,kBAAkB,CAC9BA,oBACA"}
|
package/index.d.mts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as imodel0 from "imodel";
|
|
2
|
+
import * as _yongdall_model0 from "@yongdall/model";
|
|
3
|
+
|
|
4
|
+
//#region plugins/file/models/FileCabinet.d.mts
|
|
5
|
+
declare const _default: _yongdall_model0.ModelTable<{
|
|
6
|
+
label: imodel0.FieldDefine<"string", false, false>;
|
|
7
|
+
mode: imodel0.FieldDefine<"string", false, false>;
|
|
8
|
+
storeId: imodel0.FieldDefine<"string", false, false>;
|
|
9
|
+
storePath: imodel0.FieldDefine<"string", false, false>;
|
|
10
|
+
uploadPath: imodel0.FieldDefine<"string", false, false>;
|
|
11
|
+
id: imodel0.FieldDefine<"string", false, false>;
|
|
12
|
+
createdAt: imodel0.FieldDefine<"timestamp", false, false>;
|
|
13
|
+
updatedAt: imodel0.FieldDefine<"timestamp", false, false>;
|
|
14
|
+
public: imodel0.FieldDefine<"bool", false, false>;
|
|
15
|
+
description: imodel0.FieldDefine<"text", false, false>;
|
|
16
|
+
}, "fileCabinet">;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { _default as FileCabinet };
|
package/index.mjs
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yongdall/file",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"main": "./index.mjs",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./index.mjs",
|
|
7
|
+
"./assets": "./assets/index.mjs"
|
|
8
|
+
},
|
|
9
|
+
"version": "0.1.0",
|
|
10
|
+
"description": "",
|
|
11
|
+
"keywords": [],
|
|
12
|
+
"author": "",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@yongdall/core": "^0.1.0",
|
|
16
|
+
"@yongdall/connection": "^0.1.0",
|
|
17
|
+
"@yongdall/model": "^0.1.0",
|
|
18
|
+
"@yongdall/http": "^0.1.0"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@yongdall/web": "^0.1.0"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"@yongdall/web": "^0.1.0"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { Query } from "@yongdall/model";
|
|
2
|
+
import { ApiRouter, Param, e404 } from "@yongdall/http";
|
|
3
|
+
import { getModel, getUser } from "@yongdall/core";
|
|
4
|
+
import { useConnection, useDatabase } from "@yongdall/connection";
|
|
5
|
+
|
|
6
|
+
//#region plugins/file/getCabinet.mjs
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param {string} cabinetId
|
|
10
|
+
* @returns {Promise<{mode: string; storeId: string; storePath: string; uploadPath: string; public: boolean}?>}
|
|
11
|
+
*/
|
|
12
|
+
async function getCabinet(cabinetId) {
|
|
13
|
+
const pageModel = await getModel("fileCabinet");
|
|
14
|
+
if (!pageModel) return null;
|
|
15
|
+
const sql = new Query(pageModel, true).where("id", cabinetId);
|
|
16
|
+
return await useDatabase(pageModel.databaseId).first(sql) || null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region plugins/file/common/replacePath.mjs
|
|
21
|
+
/**
|
|
22
|
+
*
|
|
23
|
+
* @param {string} path
|
|
24
|
+
* @param {Date} date
|
|
25
|
+
* @param {Record<string, any>} data
|
|
26
|
+
*/
|
|
27
|
+
function replacePath(path, date, data) {
|
|
28
|
+
return path.replace(/\{([a-zA-Z]+)\}/g, (_, v) => {
|
|
29
|
+
switch (v) {
|
|
30
|
+
case "YYYY":
|
|
31
|
+
case "YYY":
|
|
32
|
+
case "YY": return `${date.getFullYear()}`.slice(-v.length);
|
|
33
|
+
case "MM":
|
|
34
|
+
case "M": return `${date.getMonth() + 1}`.padStart(v.length, "0");
|
|
35
|
+
case "DD":
|
|
36
|
+
case "D": return `${date.getDate()}`.padStart(v.length, "0");
|
|
37
|
+
case "hh":
|
|
38
|
+
case "h": return `${date.getHours()}`.padStart(v.length, "0");
|
|
39
|
+
case "mm":
|
|
40
|
+
case "m": return `${date.getMinutes()}`.padStart(v.length, "0");
|
|
41
|
+
case "ss":
|
|
42
|
+
case "s": return `${date.getSeconds()}`.padStart(v.length, "0");
|
|
43
|
+
default: if (Object.hasOwn(data, v)) return data[v];
|
|
44
|
+
}
|
|
45
|
+
return _;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region plugins/file/routers/api.mjs
|
|
51
|
+
const fileCabinetId$1 = new Param();
|
|
52
|
+
const filePathParam$1 = new Param();
|
|
53
|
+
const apiRouter = new ApiRouter();
|
|
54
|
+
apiRouter.put`${fileCabinetId$1}/files/${filePathParam$1}+`(async (ctx) => {
|
|
55
|
+
const cabinetId = fileCabinetId$1.param(ctx) || "_";
|
|
56
|
+
const filePath = filePathParam$1.params(ctx);
|
|
57
|
+
if (!filePath?.length) return e404(ctx);
|
|
58
|
+
const paths = filePath;
|
|
59
|
+
const path = filePath.join("/");
|
|
60
|
+
const fileName = paths.pop()?.replace(/[\\\/#?{}~@:&^$()]/g, "") || "";
|
|
61
|
+
if (!fileName) return e404(ctx);
|
|
62
|
+
const dirname = paths.join("/");
|
|
63
|
+
const index = fileName.lastIndexOf(".");
|
|
64
|
+
const filename = fileName.slice(0, index) || "_";
|
|
65
|
+
const extname = index >= 0 ? fileName.slice(index) : "";
|
|
66
|
+
const cabinet = await getCabinet(cabinetId);
|
|
67
|
+
if (!cabinet) return e404(ctx);
|
|
68
|
+
const uploadPath = cabinet.uploadPath;
|
|
69
|
+
const subPath = uploadPath ? replacePath(uploadPath, /* @__PURE__ */ new Date(), {
|
|
70
|
+
user: await getUser() || "_",
|
|
71
|
+
filename,
|
|
72
|
+
extname,
|
|
73
|
+
dirname
|
|
74
|
+
}) : path;
|
|
75
|
+
/** @type {{id?: number, link: string; domain: string; path: string}?} */
|
|
76
|
+
let link = null;
|
|
77
|
+
switch (cabinet.mode) {
|
|
78
|
+
case "map": {
|
|
79
|
+
const store = useConnection("fs", cabinet.storeId);
|
|
80
|
+
const path = [...cabinet.storePath?.split("/")?.filter(Boolean) || [], subPath].join("/");
|
|
81
|
+
link = {
|
|
82
|
+
link: await store.urlWrite(path),
|
|
83
|
+
path: subPath,
|
|
84
|
+
domain: cabinetId
|
|
85
|
+
};
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (!link) return e404(ctx);
|
|
90
|
+
ctx.responseHeaders.set("Location", link.link);
|
|
91
|
+
return link;
|
|
92
|
+
});
|
|
93
|
+
var api_default = apiRouter;
|
|
94
|
+
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region plugins/file/routers/download.mjs
|
|
97
|
+
const fileCabinetId = new Param();
|
|
98
|
+
const filePathParam = new Param();
|
|
99
|
+
const downloadRouter = new ApiRouter();
|
|
100
|
+
downloadRouter.get`${fileCabinetId}/files/${filePathParam}+`(async (ctx) => {
|
|
101
|
+
const cabinetId = fileCabinetId.param(ctx) || "_";
|
|
102
|
+
const filePath = filePathParam.params(ctx);
|
|
103
|
+
if (!filePath?.length) return e404(ctx);
|
|
104
|
+
const cabinet = await getCabinet(cabinetId);
|
|
105
|
+
if (!cabinet) return e404(ctx);
|
|
106
|
+
let link = "";
|
|
107
|
+
switch (cabinet.mode) {
|
|
108
|
+
case "map": {
|
|
109
|
+
const store = useConnection("fs", cabinet.storeId);
|
|
110
|
+
const path = [...cabinet.storePath?.split("/")?.filter(Boolean) || [], ...filePath].join("/");
|
|
111
|
+
link = await store.urlRead(path);
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (!link) return e404(ctx);
|
|
116
|
+
ctx.responseHeaders.set("Location", link);
|
|
117
|
+
ctx.status = 302;
|
|
118
|
+
});
|
|
119
|
+
var download_default = downloadRouter;
|
|
120
|
+
|
|
121
|
+
//#endregion
|
|
122
|
+
//#region plugins/file/routers/index.mjs
|
|
123
|
+
const api = { fileCabinets: api_default };
|
|
124
|
+
const root = { fileCabinets: download_default };
|
|
125
|
+
|
|
126
|
+
//#endregion
|
|
127
|
+
export { api, root };
|
|
128
|
+
//# sourceMappingURL=routers.yongdall.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routers.yongdall.mjs","names":["fileCabinetId","filePathParam","apiRouter","download"],"sources":["../../plugins/file/getCabinet.mjs","../../plugins/file/common/replacePath.mjs","../../plugins/file/routers/api.mjs","../../plugins/file/routers/download.mjs","../../plugins/file/routers/index.mjs"],"sourcesContent":["import { getModel } from '@yongdall/core';\nimport { Query } from '@yongdall/model';\nimport { useDatabase } from '@yongdall/connection';\n\n\n\n/**\n * \n * @param {string} cabinetId \n * @returns {Promise<{mode: string; storeId: string; storePath: string; uploadPath: string; public: boolean}?>}\n */\nexport default async function getCabinet(cabinetId) {\n\n\tconst pageModel = await getModel('fileCabinet');\n\tif (!pageModel) { return null; }\n\n\tconst sql = new Query(pageModel, true).where('id', cabinetId);\n\tconst database = useDatabase(pageModel.databaseId);\n\treturn await database.first(sql) || null;\n}\n","\n/**\n * \n * @param {string} path \n * @param {Date} date \n * @param {Record<string, any>} data \n */\nexport default function replacePath(path, date, data) {\n\treturn path.replace(/\\{([a-zA-Z]+)\\}/g, (_, v) => {\n\t\tswitch (v) {\n\t\t\tcase 'YYYY':\n\t\t\tcase 'YYY':\n\t\t\tcase 'YY':\n\t\t\t\tconst year = date.getFullYear();\n\t\t\t\treturn `${year}`.slice(-v.length);\n\t\t\tcase 'MM':\n\t\t\tcase 'M':\n\t\t\t\tconst month = date.getMonth() + 1;\n\t\t\t\treturn `${month}`.padStart(v.length, '0');\n\t\t\tcase 'DD':\n\t\t\tcase 'D':\n\t\t\t\tconst day = date.getDate();\n\t\t\t\treturn `${day}`.padStart(v.length, '0');\n\t\t\tcase 'hh':\n\t\t\tcase 'h':\n\t\t\t\tconst hour = date.getHours();\n\t\t\t\treturn `${hour}`.padStart(v.length, '0');\n\t\t\tcase 'mm':\n\t\t\tcase 'm':\n\t\t\t\tconst minute = date.getMinutes();\n\t\t\t\treturn `${minute}`.padStart(v.length, '0');\n\t\t\tcase 'ss':\n\t\t\tcase 's':\n\t\t\t\tconst second = date.getSeconds();\n\t\t\t\treturn `${second}`.padStart(v.length, '0');\n\t\t\tdefault: {\n\t\t\t\tif (Object.hasOwn(data, v)) {\n\t\t\t\t\treturn data[v];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn _;\n\t});\n}\n","import { ApiRouter, Param } from '@yongdall/http';\nimport { e404 } from '@yongdall/http';\nimport { getUser } from '@yongdall/core';\nimport { useConnection } from '@yongdall/connection';\nimport getCabinet from '../getCabinet.mjs';\nimport replacePath from '../common/replacePath.mjs';\n\n\n\n\nconst fileCabinetId = new Param();\nconst filePathParam = new Param();\n\n\n\nconst apiRouter = new ApiRouter();\n\napiRouter.put`${fileCabinetId}/files/${filePathParam}+`(async ctx => {\n\tconst cabinetId = fileCabinetId.param(ctx) || '_';\n\tconst filePath = filePathParam.params(ctx);\n\tif (!filePath?.length) { return e404(ctx); }\n\tconst paths = filePath;\n\tconst path = filePath.join('/');\n\tconst fileName = paths.pop()?.replace(/[\\\\\\/#?{}~@:&^$()]/g, '') || '';\n\tif (!fileName) { return e404(ctx); }\n\tconst dirname = paths.join('/');\n\n\tconst index = fileName.lastIndexOf('.');\n\tconst filename = fileName.slice(0, index) || '_';\n\tconst extname = index >= 0 ? fileName.slice(index) : '';\n\n\tconst cabinet = await getCabinet(cabinetId);\n\tif (!cabinet) { return e404(ctx); }\n\tconst uploadPath = cabinet.uploadPath;\n\tconst subPath = uploadPath ? replacePath(uploadPath, new Date, {\n\t\tuser: await getUser() || '_', filename, extname, dirname,\n\t}) : path;\n\t/** @type {{id?: number, link: string; domain: string; path: string}?} */\n\tlet link = null;\n\tswitch (cabinet.mode) {\n\t\tcase 'map': {\n\t\t\tconst store = useConnection('fs', cabinet.storeId);\n\t\t\tconst path = [\n\t\t\t\t...cabinet.storePath?.split('/')?.filter(Boolean) || [],\n\t\t\t\tsubPath,\n\t\t\t].join('/');\n\t\t\tlink = {\n\t\t\t\tlink: await store.urlWrite(path),\n\t\t\t\tpath: subPath, domain: cabinetId,\n\t\t\t};\n\t\t\tbreak;\n\t\t}\n\t}\n\tif (!link) { return e404(ctx); }\n\n\tctx.responseHeaders.set('Location', link.link);\n\treturn link;\n});\nexport default apiRouter;\n","import { ApiRouter, Param } from '@yongdall/http';\nimport { e404 } from '@yongdall/http';\nimport { useConnection } from '@yongdall/connection';\nimport getCabinet from '../getCabinet.mjs';\n\n\nconst fileCabinetId = new Param();\nconst filePathParam = new Param();\nconst downloadRouter = new ApiRouter();\n\ndownloadRouter.get`${fileCabinetId}/files/${filePathParam}+`(async ctx => {\n\tconst cabinetId = fileCabinetId.param(ctx) || '_';\n\tconst filePath = filePathParam.params(ctx);\n\tif (!filePath?.length) { return e404(ctx); }\n\tconst cabinet = await getCabinet(cabinetId);\n\tif (!cabinet) { return e404(ctx); }\n\n\tlet link = '';\n\tswitch (cabinet.mode) {\n\t\tcase 'map': {\n\t\t\tconst store = useConnection('fs', cabinet.storeId);\n\t\t\tconst path = [\n\t\t\t\t...cabinet.storePath?.split('/')?.filter(Boolean) || [],\n\t\t\t\t...filePath,\n\t\t\t].join('/');\n\t\t\tlink = await store.urlRead(path);\n\t\t\tbreak;\n\t\t}\n\t}\n\tif (!link) { return e404(ctx); }\n\tctx.responseHeaders.set('Location', link);\n\tctx.status = 302;\n});\nexport default downloadRouter;\n","import apiRouter from './api.mjs';\nimport download from './download.mjs';\n\nexport const api = {\n\tfileCabinets: apiRouter,\n}\nexport const root = {\n\tfileCabinets: download,\n}\n"],"mappings":";;;;;;;;;;;AAWA,eAA8B,WAAW,WAAW;CAEnD,MAAM,YAAY,MAAM,SAAS,cAAc;AAC/C,KAAI,CAAC,UAAa,QAAO;CAEzB,MAAM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