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

Loading…
Cancel
Save