packageutilsimport("archive/zip""fmt""io""net/http""os""path/filepath""strings")funcCopyFile(src,dststring)error{in,err:=os.Open(src)iferr!=nil{returnerr}deferin.Close()out,err:=os.Create(dst)iferr!=nil{returnerr}deferout.Close()_,err=io.Copy(out,in)returnerr}funcZipDirectory(src,dststring)error{zipFile,err:=os.Create(dst)iferr!=nil{returnerr}deferzipFile.Close()archive:=zip.NewWriter(zipFile)deferarchive.Close()returnfilepath.Walk(src,func(pathstring,infoos.FileInfo,errerror)error{iferr!=nil{returnerr}ifinfo.IsDir(){returnnil}relPath,err:=filepath.Rel(src,path)iferr!=nil{returnerr}// Use forward slashes for zip file pathsrelPath=filepath.ToSlash(relPath)header,err:=zip.FileInfoHeader(info)iferr!=nil{returnerr}header.Name=relPathheader.Method=zip.Deflatewriter,err:=archive.CreateHeader(header)iferr!=nil{returnerr}file,err:=os.Open(path)iferr!=nil{returnerr}deferfile.Close()_,err=io.Copy(writer,file)returnerr})}funcGetContentType(filenamestring)string{ext:=filepath.Ext(filename)switchstrings.ToLower(ext){case".md",".markdown":return"markdown"case".jpg",".jpeg",".png",".gif",".webp",".svg",".ico",".bmp",".tiff",".tif",".heic",".heif":return"image"case".mp4",".mov",".avi",".mkv",".webm",".mp3",".wav",".ogg",".flac",".aac",".m4a",".m4b",".m4p",".m4v":return"video"case".pdf":return"pdf"case".zip",".tar",".gz",".7z",".rar":return"archive"}if!IsBinaryFile(filename){return"text"}return"binary"}funcIsBinaryFile(pathstring)bool{file,err:=os.Open(path)iferr!=nil{returnfalse}deferfile.Close()// Read first 8KB (more than enough for detection)buf:=make([]byte,8192)n,err:=file.Read(buf)iferr!=nil&&err.Error()!="EOF"{returnfalse}fori:=0;i<n;i++{b:=buf[i]// Allow common printable characters and whitespaceifb==0{returntrue}ifb<7||(b>14&&b<32){returntrue}}returnfalse}funcGetIconClass(ctypestring)string{switchctype{case"markdown":return"file-text"case"pdf":return"file-text"case"image":return"file-image"case"video":return"file-play"case"text":return"file-braces"case"archive":return"folder-archive"}return"binary"}funcDownloadFile(urlstring,deststring)error{resp,err:=http.Get(url)iferr!=nil{returnerr}deferresp.Body.Close()ifresp.StatusCode!=http.StatusOK{returnfmt.Errorf("bad status: %s",resp.Status)}out,err:=os.Create(dest)iferr!=nil{returnerr}deferout.Close()_,err=io.Copy(out,resp.Body)returnerr}funcUnzip(srcstring,deststring)error{r,err:=zip.OpenReader(src)iferr!=nil{returnerr}deferr.Close()for_,f:=ranger.File{fpath:=filepath.Join(dest,f.Name)// Check for ZipSlip (Directory traversal)if!strings.HasPrefix(fpath,filepath.Clean(dest)+string(os.PathSeparator)){returnfmt.Errorf("illegal file path: %s",fpath)}iff.FileInfo().IsDir(){os.MkdirAll(fpath,os.ModePerm)continue}iferr=os.MkdirAll(filepath.Dir(fpath),os.ModePerm);err!=nil{returnerr}outFile,err:=os.OpenFile(fpath,os.O_WRONLY|os.O_CREATE|os.O_TRUNC,f.Mode())iferr!=nil{returnerr}rc,err:=f.Open()iferr!=nil{outFile.Close()returnerr}_,err=io.Copy(outFile,rc)outFile.Close()rc.Close()iferr!=nil{returnerr}}returnnil}