获取磁盘列表和全路径文件下载
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"ems.agt/lib/dborm"
|
||||
"ems.agt/lib/file"
|
||||
"ems.agt/lib/log"
|
||||
"ems.agt/lib/services"
|
||||
"ems.agt/restagent/config"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/shirou/gopsutil/disk"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -14,6 +19,12 @@ var (
|
||||
UriFile = config.DefaultUriPrefix + "/fileManagement/{apiVersion}/{location}/file"
|
||||
|
||||
CustomUriFile = config.UriPrefix + "/fileManagement/{apiVersion}/{location}/file"
|
||||
|
||||
// 获取磁盘列表
|
||||
UriDiskList = config.UriPrefix + "/fileManagement/{apiVersion}/files/diskList"
|
||||
|
||||
// 获取文件列表
|
||||
UriListFiles = config.UriPrefix + "/fileManagement/{apiVersion}/files/listFiles"
|
||||
)
|
||||
|
||||
// func init() {
|
||||
@@ -89,6 +100,17 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// 全路径文件下载
|
||||
filePath := r.URL.Query().Get("path")
|
||||
if location == "path" && filePath != "" {
|
||||
// 截取文件路径
|
||||
dir := filepath.Dir(filePath)
|
||||
// 截取文件名
|
||||
fileName := filepath.Base(filePath)
|
||||
services.ResponseFileWithNameAndMD5(w, http.StatusOK, fileName, dir, "")
|
||||
return
|
||||
}
|
||||
|
||||
params := r.URL.Query()
|
||||
fileNames, ok := params["loc"]
|
||||
if !ok {
|
||||
@@ -126,3 +148,46 @@ func DeleteFile(w http.ResponseWriter, r *http.Request) {
|
||||
services.ResponseStatusOK204NoContent(w)
|
||||
return
|
||||
}
|
||||
|
||||
// 磁盘列表
|
||||
func DiskList(w http.ResponseWriter, r *http.Request) {
|
||||
disks := make([]map[string]string, 0)
|
||||
|
||||
partitions, err := disk.Partitions(false)
|
||||
if err != nil {
|
||||
services.ResponseWithJson(w, 200, disks)
|
||||
}
|
||||
|
||||
for _, partition := range partitions {
|
||||
usage, err := disk.Usage(partition.Mountpoint)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
disks = append(disks, map[string]string{
|
||||
"size": file.FormatFileSize(float64(usage.Total)),
|
||||
"used": file.FormatFileSize(float64(usage.Used)),
|
||||
"avail": file.FormatFileSize(float64(usage.Free)),
|
||||
"pcent": fmt.Sprintf("%.1f%%", usage.UsedPercent),
|
||||
"target": partition.Device,
|
||||
})
|
||||
}
|
||||
services.ResponseWithJson(w, 200, disks)
|
||||
}
|
||||
|
||||
// 获取文件列表 /files/search
|
||||
func ListFiles(w http.ResponseWriter, r *http.Request) {
|
||||
// json 請求參數獲取
|
||||
var bodyArgs FileOption
|
||||
err := services.ShouldBindJSON(r, &bodyArgs)
|
||||
if err != nil || dborm.DbClient.XEngine == nil {
|
||||
services.ResponseErrorWithJson(w, 400, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
files, err := GetFileList(bodyArgs)
|
||||
if err != nil {
|
||||
services.ResponseErrorWithJson(w, 400, err.Error())
|
||||
return
|
||||
}
|
||||
services.ResponseWithJson(w, 200, files)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user