自动更新管控端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

89 lines
2.0 KiB

package handler
import (
"encoding/json"
"fmt"
"fssc/config"
"net/http"
"os"
"path/filepath"
"strings"
)
// json 结构体
type Response struct {
Status string `json:"status"` //状态
Data FilesListJson `json:"data"` //目录下的文件
Scdir string `json:"curdir"` // 扫描的目录
}
// 文件输出的结构
type FileJson struct {
Fname string `json:"fname"`
Dirflag int `json:isdir`
}
type FilesListJson struct {
Flist []FileJson `json:"list"`
}
// 遍历监视目录,发送到json中
func NfTest(w http.ResponseWriter, r *http.Request) {
// 监听的目录通过?p=的方式传入
//urlpath := r.Header.Get("p")
urlpath := r.URL.Query().Get("p")
upath := strings.TrimSuffix(urlpath, "nf")
fmt.Printf("upath is %s\n", upath)
// 监听的根目录
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 {
Rundir string
IsDir bool
FileName string
DownloadPath string
// Files []os.DirEntry
}{
Rundir: config.G.FilePath,
DownloadPath: downloadPath,
}
// list json
var flist FilesListJson
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
for _, v := range files {
flist.Flist = append(flist.Flist, FileJson{Fname: v.Name(), Dirflag: 1})
}
} else {
data.FileName = filepath.Base(realFilePath)
flist.Flist = append(flist.Flist, FileJson{Fname: filepath.Base(realFilePath), Dirflag: 0})
}
// respone file list
response := Response{
Status: "success",
Scdir: upath,
Data: flist,
}
//
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}