package handler import ( "fmt" "fssc/config" "fssc/internal/util" "fssc/web" "html/template" "net/http" "os" "path/filepath" ) // web 控制台 func WebConsole(w http.ResponseWriter, r *http.Request) { //file flag isfile := r.URL.Query().Get("file") // 路径 urlpath := r.URL.Query().Get("p") if urlpath == "Li4=" { urlpath = "." } //文件名 fname := r.URL.Query().Get("f") //转码 abupath := util.Base64dec(urlpath) abfname := util.Base64dec(fname) fmt.Printf("after base64: path: %s,file:%s\n", abupath, abfname) // url if isfile == "0" { abupath = filepath.Join(abupath, abfname) } // 转成unix 风格 abupath = filepath.ToSlash(abupath) // upath := strings.TrimSuffix(abupath, "console") // serve download page for send realFilePath := filepath.Join(config.G.FilePath, abupath) downloadPath := filepath.Join(filepath.Base(config.G.FilePath), abupath) // 相对路径 relpath := filepath.Join(abupath, ".") fmt.Printf("relpath is %s\n", relpath) // fileInfo, err := os.Stat(realFilePath) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } data := struct { DeviceName string Loip string Rundir string IsDir bool FileName string DownloadPath string Relpath string //相对路径 UrlPath string Files []os.DirEntry }{ DeviceName: config.G.DeviceName, Loip: config.G.LocalIP, Rundir: config.G.FilePath, DownloadPath: downloadPath, Relpath: relpath, // UrlPath: strings.TrimSuffix(r.URL.Path, "/"), UrlPath: abupath, } if fileInfo.IsDir() { data.IsDir = true // 遍历目录 files, err := os.ReadDir(realFilePath) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } data.Files = files } else { data.FileName = filepath.Base(realFilePath) } // add self template function fmap := template.FuncMap{"b64en": util.Bas64end, "hshcal": util.CalacHash} // 文件列表模板 tmpl, err := template.New("console").Funcs(fmap).Parse(web.WbConsole) if err != nil { // http.Error(w, err.Error(), http.StatusInternalServerError) return } err = tmpl.Execute(w, data) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }