1import{ag as P}from"./main-Dck1T9BT.js";import"./react-vendor-CHQL15iE.js";import"./lucide-BFLOpGdB.js";import"./pdfjs-ji3DqnKJ.js";import"./markdown-W-rE_H7s.js";import"./katex-math-BxKHCrdI.js";class L extends P{constructor(){super(...arguments),this.lastProgressUpdate=0,this.PROGRESS_UPDATE_INTERVAL=100}async downloadFile(t){try{const e=this.buildUrl(t.url,t.params),s=new Headers(t.headers),o={method:t.method||"GET",headers:s,redirect:t.disableRedirects?"manual":"follow"},r=new AbortController,l=t.connectTimeout||6e4,i=setTimeout(()=>r.abort(),l);let n,a=0;try{const d=await fetch(e,{...o,signal:r.signal});if(!d.ok)throw new Error(`HTTP error: ${d.status}`);if(t.progress&&d.body){const h=d.body.getReader(),f=parseInt(d.headers.get("content-length")||"0",10),F=d.headers.get("content-type")||"";let m=0;const w=[];for(;;){const{done:u,value:g}=await h.read();if(u)break;g&&(w.push(g),m+=g.length,this.notifyProgress(t.url,m,f,f>0,"download"))}const y=new Uint8Array(m);let b=0;for(const u of w)y.set(u,b),b+=u.length;n=new Blob([y],{type:F}),a=m,t.progress&&this.notifyProgress(t.url,a,a,!0,"download",!0)}else n=await d.blob(),a=n.size}finally{clearTimeout(i)}if(await this.tryWriteToFilesystem(n,t.path))return{path:t.path,blob:n};const c=URL.createObjectURL(n),p=document.createElement("a");return p.href=c,p.download=this.extractFilename(t.path,t.url),document.body.appendChild(p),p.click(),document.body.removeChild(p),URL.revokeObjectURL(c),{blob:n,path:t.path}}catch(e){throw this.handleError(e,t.url,t.path)}}async uploadFile(t){try{let e;if(t.blob)e=t.blob;else{const s=await this.tryReadFromFilesystem(t.path);if(s)e=s;else throw new Error("File upload from path is not supported in web without @capacitor/filesystem plugin. Please provide a blob instead.")}return new Promise((s,o)=>{const r=new XMLHttpRequest,l=this.buildUrl(t.url,t.params);t.progress&&(r.upload.onprogress=a=>{this.notifyProgress(t.url,a.loaded,a.total,a.lengthComputable,"upload")}),r.onload=()=>{if(r.status>=200&&r.status<300){t.progress&&this.notifyProgress(t.url,e.size,e.size,!0,"upload",!0);const a={};r.getAllResponseHeaders().split(`\r 2`).forEach(c=>{const[p,d]=c.split(": ");p&&d&&(a[p]=d)}),s({bytesSent:e.size,responseCode:r.status.toString(),response:r.responseText,headers:a})}else o(new Error(`HTTP error: ${r.status}`))},r.onerror=()=>{o(new Error("Network error occurred"))},r.open(t.method||"POST",l,!0),t.headers&&Object.entries(t.headers).forEach(([a,c])=>{r.setRequestHeader(a,c)});const i=new FormData,n=t.fileKey||"file";i.append(n,e,t.path),t.params&&Object.entries(t.params).forEach(([a,c])=>{Array.isArray(c)?c.forEach(p=>i.append(a,p)):i.append(a,c)}),r.send(i)})}catch(e){throw this.handleError(e,t.path,t.url)}}buildUrl(t,e){if(!e||Object.keys(e).length===0)return t;const s=new URL(t);return Object.entries(e).forEach(([o,r])=>{Array.isArray(r)?r.forEach(l=>s.searchParams.append(o,l)):s.searchParams.append(o,r)}),s.toString()}notifyProgress(t,e,s,o,r,l=!1){const i=Date.now();if(l||i-this.lastProgressUpdate>=this.PROGRESS_UPDATE_INTERVAL){const n={type:r,url:t,bytes:e,contentLength:s,lengthComputable:o};this.notifyListeners("progress",n),this.lastProgressUpdate=i}}handleError(t,e,s){return t instanceof TypeError&&t.message==="Failed to fetch"?{code:"OS-PLUG-FLTR-0008",message:"Failed to connect to server",source:e,target:s}:t instanceof Error?{code:"OS-PLUG-FLTR-0011",message:t.message,source:e,target:s}:{code:"OS-PLUG-FLTR-0011",message:"An unknown error occurred",source:e,target:s}}extractFilename(t,e){const s=t.split("?")[0].split("#")[0].split(/[/\\]/);let o=s[s.length-1];if(!o.includes(".")){const r=e.split(".");if(r.length>1){const l=r[r.length-1].split("?")[0];o=`${r[r.length-2].split("/").pop()}.${l}`}}return o||(o="download"),o}isFilesystemAvailable(){var t,e;try{return!!((e=(t=globalThis==null?void 0:globalThis.Capacitor)==null?void 0:t.Plugins)!=null&&e.Filesystem)}catch{return!1}}async tryReadFromFilesystem(t){var e,s;if(!this.isFilesystemAvailable())return null;try{const o=(s=(e=window.Capacitor)==null?void 0:e.Plugins)==null?void 0:s.Filesystem;if(!o)return null;const r=(await o.readFile({path:t})).data;if(!r)throw new Error("No data returned from Filesystem plugin");const l=this.getMimeTypeFromPath(t)||"application/octet-stream",i=atob(r),n=[];for(let a=0;a<i.length;a+=512){const c=i.slice(a,a+512),p=new Array(c.length);for(let h=0;h<c.length;h++)p[h]=c.charCodeAt(h);const d=new Uint8Array(p);n.push(d)}return new Blob(n,{type:l})}catch(o){return console.error("Error reading file from Filesystem:",o),null}}async tryWriteToFilesystem(t,e){var s,o;if(!this.isFilesystemAvailable())return!1;try{const r=(o=(s=window.Capacitor)==null?void 0:s.Plugins)==null?void 0:o.Filesystem;if(!r)return!1;const l=await this.blobToBase64(t);if(!l)throw new Error("Failed to convert blob to base64");const i=e.split("/");if(i.length>1){const n=i.slice(0,-1).join("/");await r.stat({path:n}).catch(async()=>{await r.mkdir({path:n,recursive:!0})})}return await r.writeFile({path:e,data:l.split(",")[1],recursive:!0}),!0}catch(r){return console.error("Error writing file to Filesystem:",r),!1}}blobToBase64(t){return new Promise((e,s)=>{const o=new FileReader;o.onload=()=>e(o.result),o.onerror=()=>s(new Error("Failed to convert blob to base64")),o.readAsDataURL(t)})}getMimeTypeFromPath(t){var s;const e=(s=t.split(".").pop())==null?void 0:s.toLowerCase();return e&&{jpg:"image/jpeg",jpeg:"image/jpeg",png:"image/png",gif:"image/gif",pdf:"application/pdf",txt:"text/plain",html:"text/html",htm:"text/html",json:"application/json",doc:"application/msword",docx:"application/vnd.openxmlformats-officedocument.wordprocessingml.document",xls:"application/vnd.ms-excel",xlsx:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",zip:"application/zip",mp3:"audio/mpeg",mp4:"video/mp4",wav:"audio/wav",xml:"application/xml",csv:"text/csv"}[e]||null}}export{L as FileTransferWeb};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.