package handler import ( "fssc/config" "fssc/internal/util" "fssc/web" "html/template" "net/http" "os" "path/filepath" "strings" ) // web 控制台 func WebConsole(w http.ResponseWriter, r *http.Request) { urlpath := r.URL.Query().Get("p") upath := strings.TrimSuffix(urlpath, "console") // serve download page for send realFilePath := filepath.Join(config.G.FilePath, upath) downloadPath := filepath.Join(filepath.Base(config.G.FilePath), r.URL.Path[1:]) 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 UrlPath string Files []os.DirEntry }{ DeviceName: config.G.DeviceName, Loip: config.G.LocalIP, Rundir: config.G.FilePath, DownloadPath: downloadPath, UrlPath: strings.TrimSuffix(r.URL.Path, "/"), // UrlPath: r.URL.Path, } 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} // 文件列表模板 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) } }