2 lines
3.9 KiB
JavaScript
2 lines
3.9 KiB
JavaScript
function n(h){const t=h.split("/").filter(Boolean),s=[];for(const e of t)e!=="."&&(e===".."?s.pop():s.push(e));return"/"+s.join("/")}class c{token=null;baseUrl;constructor(){this.baseUrl=`${window.location.origin}/app/filebrowser`}get isAuthenticated(){return this.token!==null}async login(t="admin",s="admin"){try{const e=await fetch(`${this.baseUrl}/api/login`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({username:t,password:s})});if(!e.ok)return!1;const o=await e.text();return this.token=o.replace(/^"|"$/g,""),document.cookie=`auth=${this.token}; path=/app/filebrowser; SameSite=Strict`,!0}catch{return!1}}headers(){const t={};return this.token&&(t["X-Auth"]=this.token),t}async listDirectory(t){const s=n(t),e=await fetch(`${this.baseUrl}/api/resources${s}`,{headers:this.headers()});if(!e.ok)throw new Error(`Failed to list directory: ${e.status}`);return((await e.json()).items||[]).map(a=>({...a,extension:a.name.includes(".")?a.name.split(".").pop().toLowerCase():""}))}downloadUrl(t){const s=n(t);return`${this.baseUrl}/api/raw${s}`}async fetchBlobUrl(t){const s=n(t),e=await fetch(`${this.baseUrl}/api/raw${s}`,{headers:this.headers()});if(!e.ok)throw new Error(`Failed to fetch file: ${e.status}`);const o=await e.blob();return URL.createObjectURL(o)}async downloadFile(t){const s=await this.fetchBlobUrl(t),e=t.split("/").pop()||"download",o=document.createElement("a");o.href=s,o.download=e,document.body.appendChild(o),o.click(),document.body.removeChild(o),URL.revokeObjectURL(s)}async upload(t,s){const e=n(t),o=e.endsWith("/")?e:`${e}/`,a=encodeURIComponent(s.name),i=await fetch(`${this.baseUrl}/api/resources${o}${a}?override=true`,{method:"POST",headers:this.headers(),body:s});if(!i.ok){const r=await i.text().catch(()=>"");throw new Error(`Upload failed (${i.status}): ${r}`)}}async createFolder(t,s){const e=n(t),o=e.endsWith("/")?e:`${e}/`,a=s.replace(/\.\./g,"").replace(/\//g,""),i=await fetch(`${this.baseUrl}/api/resources${o}${a}/`,{method:"POST",headers:this.headers()});if(!i.ok)throw new Error(`Create folder failed: ${i.status}`)}async deleteItem(t){const s=n(t),e=await fetch(`${this.baseUrl}/api/resources${s}`,{method:"DELETE",headers:this.headers()});if(!e.ok)throw new Error(`Delete failed: ${e.status}`)}async getUsage(){if(!this.isAuthenticated&&!await this.login())return{totalSize:0,folderCount:0,fileCount:0};const t=await fetch(`${this.baseUrl}/api/resources/`,{headers:this.headers()});if(!t.ok)return{totalSize:0,folderCount:0,fileCount:0};const e=(await t.json()).items||[],o=e.filter(r=>r.isDir).length,a=e.filter(r=>!r.isDir).length;return{totalSize:e.reduce((r,l)=>r+(l.size||0),0),folderCount:o,fileCount:a}}static TEXT_EXTENSIONS=new Set(["txt","md","json","csv","log","conf","yaml","yml","toml","xml","html","css","js","ts","py","sh","bash","env","ini","cfg","sql","rs","go","java","c","h","cpp","hpp","rb","php","dockerfile","makefile","gitignore","editorconfig"]);isTextFile(t){const s=t.includes(".")?t.split(".").pop().toLowerCase():"",e=t.split("/").pop()?.toLowerCase()||"";return c.TEXT_EXTENSIONS.has(s)||c.TEXT_EXTENSIONS.has(e)}async readFileAsText(t,s=102400){if(!this.isAuthenticated&&!await this.login())throw new Error("FileBrowser authentication failed");if(!this.isTextFile(t))throw new Error(`Cannot read binary file: ${t}`);const e=n(t),o=await fetch(`${this.baseUrl}/api/raw${e}`,{headers:this.headers()});if(!o.ok)throw new Error(`Failed to read file: ${o.status}`);const a=await o.blob(),i=a.size,r=i>s;return{content:await(r?a.slice(0,s):a).text(),truncated:r,size:i}}async rename(t,s){const e=n(t),o=e.substring(0,e.lastIndexOf("/")+1),a=s.replace(/\.\./g,"").replace(/\//g,""),i=await fetch(`${this.baseUrl}/api/resources${e}`,{method:"PATCH",headers:{...this.headers(),"Content-Type":"application/json"},body:JSON.stringify({destination:`${o}${a}`})});if(!i.ok)throw new Error(`Rename failed: ${i.status}`)}}const u=new c;export{u as f};
|