Browse Source

调整输出json

master
453530270@qq.com 2 years ago
parent
commit
06a210b953
  1. 44
      fssc/internal/handler/nfhandler.go

44
fssc/internal/handler/nfhandler.go

@ -12,7 +12,17 @@ import (
// json 结构体
type Response struct {
Status string `json:"status"`
Message string `json:"message"`
Message string `json:"data"`
}
// 文件输出的结构
type FileJson struct {
Fname string `json:"fname"`
Isdir bool `json:isdir`
}
type FilesListJson struct {
Flist []FileJson `json:"list"`
}
// 遍历监视目录,发送到json中
@ -20,6 +30,7 @@ func NfTest(w http.ResponseWriter, r *http.Request) {
// 监听的根目录
realFilePath := filepath.Join(config.G.FilePath, ".")
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)
@ -33,7 +44,7 @@ func NfTest(w http.ResponseWriter, r *http.Request) {
IsDir bool
FileName string
DownloadPath string
Files []os.DirEntry
// Files []os.DirEntry
}{
DeviceName: config.G.DeviceName,
Loip: config.G.LocalIP,
@ -41,6 +52,13 @@ func NfTest(w http.ResponseWriter, r *http.Request) {
DownloadPath: downloadPath,
}
// 文件灌入数组中
// var gmap map[string]interface{}
// gmap = make(map[string]interface{})
// list json
var flist FilesListJson
if fileInfo.IsDir() {
data.IsDir = true
// 遍历目录
@ -49,25 +67,29 @@ func NfTest(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data.Files = files
// data.Files = files
for _, v := range files {
// gmap[k]["filename"] = v.Name()
// gmap[k]["isdir"] = v.IsDir()
flist.Flist = append(flist.Flist, FileJson{Fname: v.Name(), Isdir: v.IsDir()})
}
} else {
data.FileName = filepath.Base(realFilePath)
flist.Flist = append(flist.Flist, FileJson{Fname: filepath.Base(realFilePath), Isdir: true})
}
// for _,enty:= range data {
// }
//结果生成json
ss, err := json.Marshal(data)
// 绑定到jfile
jdata, err := json.Marshal(flist)
if err != nil {
fmt.Printf("json encode erorr:%s", err)
fmt.Printf("gmap has error:%v\n", err)
}
// respone file list
response := Response{
Status: "success",
Message: string(ss),
Message: string(jdata),
}
//
w.Header().Set("Content-Type", "application/json")

Loading…
Cancel
Save