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.
75 lines
1.5 KiB
75 lines
1.5 KiB
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"fssc/config"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// json 结构体
|
|
type Response struct {
|
|
Status string `json:"status"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
// 遍历监视目录,发送到json中
|
|
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)
|
|
return
|
|
}
|
|
|
|
data := struct {
|
|
DeviceName string
|
|
Loip string
|
|
Rundir string
|
|
IsDir bool
|
|
FileName string
|
|
DownloadPath string
|
|
Files []os.DirEntry
|
|
}{
|
|
DeviceName: config.G.DeviceName,
|
|
Loip: config.G.LocalIP,
|
|
Rundir: config.G.FilePath,
|
|
DownloadPath: downloadPath,
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
// for _,enty:= range data {
|
|
|
|
// }
|
|
|
|
//结果生成json
|
|
ss, err := json.Marshal(data)
|
|
if err != nil {
|
|
fmt.Printf("json encode erorr:%s", err)
|
|
}
|
|
|
|
// respone file list
|
|
response := Response{
|
|
Status: "success",
|
|
Message: string(ss),
|
|
}
|
|
//
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|