add: 提交
This commit is contained in:
126
features/file/file.go
Normal file
126
features/file/file.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"ems.agt/lib/log"
|
||||
"ems.agt/lib/services"
|
||||
"ems.agt/restagent/config"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
var (
|
||||
// parameter config management
|
||||
UriFile = config.UriPrefix + "/fileManagement/{apiVersion}/{location}/file"
|
||||
)
|
||||
|
||||
// func init() {
|
||||
// routes.Register("POST", UriFile, UploadFile, nil)
|
||||
// routes.Register("GET", UriFile, DownloadFile, nil)
|
||||
// routes.Register("DELETE", UriFile, DeleteFile, nil)
|
||||
// }
|
||||
|
||||
func UploadFile(w http.ResponseWriter, r *http.Request) {
|
||||
log.Debug("UploadFile processing... ")
|
||||
|
||||
_, err := services.CheckFrontValidRequest(w, r)
|
||||
if err != nil {
|
||||
log.Error("Http request error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
vars := mux.Vars(r)
|
||||
location := vars["location"]
|
||||
if location == "" {
|
||||
log.Error("location is empty")
|
||||
services.ResponseNotFound404UriNotExist(w, r)
|
||||
return
|
||||
}
|
||||
params := r.URL.Query()
|
||||
rename, ok := params["rename"]
|
||||
if !ok {
|
||||
log.Info("rename parameter is not exist")
|
||||
}
|
||||
log.Debug("rename:", rename)
|
||||
|
||||
var path, fileName string
|
||||
if len(rename) > 0 {
|
||||
fileName = rename[0]
|
||||
}
|
||||
if location == "upload" {
|
||||
path = config.GetYamlConfig().OMC.Upload
|
||||
fileName, err = services.HandleUploadFile(r, path, fileName)
|
||||
if err != nil {
|
||||
log.Error("Faile to HandleUploadFile:", err)
|
||||
services.ResponseInternalServerError500ProcessError(w, err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
path = config.GetYamlConfig().OMC.FrontUpload
|
||||
fileName, err = services.HandleUploadFile(r, path, fileName)
|
||||
if err != nil {
|
||||
log.Error("Faile to HandleUploadFile:", err)
|
||||
services.ResponseInternalServerError500ProcessError(w, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
log.Debugf("upload file=%s to path=%s", fileName, path)
|
||||
|
||||
services.ResponseStatusOK204NoContent(w)
|
||||
return
|
||||
}
|
||||
|
||||
func DownloadFile(w http.ResponseWriter, r *http.Request) {
|
||||
log.Debug("DownloadFile processing... ")
|
||||
|
||||
_, err := services.CheckFrontValidRequest(w, r)
|
||||
if err != nil {
|
||||
log.Error("Request error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
vars := mux.Vars(r)
|
||||
location := vars["location"]
|
||||
if location == "" {
|
||||
log.Error("location is empty")
|
||||
services.ResponseNotFound404UriNotExist(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
params := r.URL.Query()
|
||||
fileNames, ok := params["loc"]
|
||||
if !ok {
|
||||
log.Info("loc parameter is not exist")
|
||||
}
|
||||
var path string
|
||||
if location == "upload" {
|
||||
path = config.GetYamlConfig().OMC.Upload
|
||||
} else {
|
||||
path = config.GetYamlConfig().OMC.FrontUpload
|
||||
}
|
||||
for _, fileName := range fileNames {
|
||||
services.ResponseFileWithNameAndMD5(w, http.StatusOK, fileName, path, "")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteFile(w http.ResponseWriter, r *http.Request) {
|
||||
log.Debug("DeleteFile processing... ")
|
||||
|
||||
_, err := services.CheckFrontValidRequest(w, r)
|
||||
if err != nil {
|
||||
log.Error("Request error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
vars := mux.Vars(r)
|
||||
neType := vars["neType"]
|
||||
if neType == "" {
|
||||
log.Error("neType is empty")
|
||||
services.ResponseNotFound404UriNotExist(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
services.ResponseStatusOK204NoContent(w)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user