完善文件管理相关接口

master
a7458969 2020-05-11 11:35:45 +08:00
parent 302f63dcfe
commit 9f1536013d
4 changed files with 149 additions and 44 deletions

View File

@ -75,7 +75,12 @@ func (this *FileController) OnUpload(c *gin.Context) {
}
func (this *FileController) FileList(c *gin.Context) {
path := c.Query("path")
utils.GetPathFileName("files/" + path)
c.JSON(200,map[string]interface{}{
"msg":"ok",
"status":200,
})
}
func (this *FileController) FileType(c *gin.Context) {
@ -86,13 +91,9 @@ func (this *FileController) DownloadFile(c *gin.Context) {
}
func (this *FileController) OnUploadFile(c *gin.Context) {
uid,e := uuid.NewV1()
if nil != e{
log.Print(e.Error())
return
}
func (this *FileController) OnFileUploadFile(c *gin.Context) {
path := c.Query("path")
filename := c.Query("filename")
file, _, err := c.Request.FormFile("file")
if nil != err || nil == file{
log.Print(err.Error())
@ -108,39 +109,15 @@ func (this *FileController) OnUploadFile(c *gin.Context) {
types := http.DetectContentType(header)
log.Print(types)
// jpg
if strings.Contains(types,"jpeg"){
defer file.Close()
out, err := os.Create("file/" + uid.String() + ".jpg")
if err != nil {
log.Fatal(err)
}
defer out.Close()
out.Write(bytes)
log.Print(len(bytes))
c.JSON(200, map[string] interface{}{"url":uid.String() + ".jpg" })
}
if strings.Contains(types,"png"){
defer file.Close()
out, err := os.Create("image/" + uid.String() + ".png")
if err != nil {
log.Fatal(err)
}
defer out.Close()
out.Write(bytes)
log.Print(len(bytes))
c.JSON(200, map[string] interface{}{"url":uid.String() + ".png" })
}
if strings.Contains(types,"gif"){
defer file.Close()
out, err := os.Create("image/" + uid.String() + ".gif")
if err != nil {
log.Fatal(err)
}
defer out.Close()
out.Write(bytes)
log.Print(len(bytes))
c.JSON(200, map[string] interface{}{"url":uid.String() + ".gif" } )
defer file.Close()
out, err := os.Create("files/" + path + "/" + filename)
if err != nil {
log.Print(err)
}
defer out.Close()
out.Write(bytes)
log.Print(len(bytes))
c.JSON(200, map[string] interface{}{"msg": "ok" })
}
func (this *FileController) OnThumbnail(c *gin.Context) {

113
files/file/test Normal file

File diff suppressed because one or more lines are too long

View File

@ -130,10 +130,10 @@ func main() {
api.GET("/hardware",controller.ReadHardWare) // 读取硬件
api.DELETE("/hardware",controller.DeleteHardWare) // 读取硬件
api.PUT("file",fileController.OnUploadFile) // 上传文件
api.GET("file",fileController.DownloadFile) // 下载 文件
api.GET("filelist",fileController.FileList) // 文件列表
api.GET("fileType",fileController.FileType) // 文件类型
api.PUT("/file",fileController.OnFileUploadFile) // 上传文件
api.GET("/file",fileController.DownloadFile) // 下载 文件
api.GET("/filelist",fileController.FileList) // 文件列表
api.GET("/fileType",fileController.FileType) // 文件类型
}

View File

@ -2,6 +2,7 @@ package utils
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -23,3 +24,17 @@ func GetCurrentDirectory() string {
}
return strings.Replace(dir, "\\", "/", -1)
}
func GetPathFileName(pathname string) (error) {
rd, err := ioutil.ReadDir(pathname)
for _, fi := range rd {
if fi.IsDir() {
fmt.Printf("[%s]\n", pathname+"\\"+fi.Name())
GetPathFileName(pathname + fi.Name() + "\\")
} else {
fmt.Println(fi.Name())
}
}
return err
}