123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- /*
- * @description:
- * @Author: CP
- * @Date: 2021-03-31 15:06:21
- * @FilePath: \design_quantity\web\api\upload_api.go
- */
- package api
- import (
- "bytes"
- "fmt"
- "io"
- "log"
- "os"
- "strings"
- "time"
- "github.com/kataras/iris/v12"
- "go.mod/conf"
- "go.mod/services"
- "go.mod/web/utils"
- "go.mod/web/viewmodels"
- )
- type UploadApi struct {
- //框架-web应用上下文环境
- Ctx iris.Context
- // // 需要用的service
- ServiceContract services.ContractService
- ServiceUpload services.UploadService
- }
- // Upload : 处理文件上传
- func (c *UploadApi) Post() {
- errCode := 0
- id := "0"
- defer func() {
- if errCode < 0 {
- fmt.Println("errCode", errCode)
- c.Ctx.JSON(iris.Map{"code": -1, "msg": "上传失败"})
- } else {
- c.Ctx.JSON(iris.Map{"code": 0, "msg": "上传成功", "data": id})
- }
- return
- }()
- file, head, err := c.Ctx.FormFile("file")
- if err != nil {
- log.Printf("Failed to get form data, err:%s\n", err.Error())
- errCode = -1
- return
- }
- defer file.Close()
- buf := bytes.NewBuffer(nil)
- if _, err := io.Copy(buf, file); err != nil {
- log.Printf("Failed to get file data, err:%s\n", err.Error())
- errCode = -2
- return
- }
- // 文件元信息-todo
- // FileName := head.Filename
- FileSize := int64(len(buf.Bytes()))
- UploadAt := fmt.Sprintf("%d", time.Now().UnixNano())
- Location := conf.MergeLocalRootDir + UploadAt // 存储地址
- webLocation := "/public/" + UploadAt + head.Filename
- newFile, err := os.Create(Location)
- if err != nil {
- log.Printf("Failed to create file, err:%s\n", err.Error())
- errCode = -3
- return
- }
- defer newFile.Close()
- nByte, err := newFile.Write(buf.Bytes())
- if int64(nByte) != FileSize || err != nil {
- log.Printf("Failed to save data into file, writtenSize:%d, err:%s\n", nByte, err.Error())
- errCode = -4
- return
- }
- // fmt.Println(c.Ctx.FormValue("id"))
- // treeVM := &viewmodels.TreeSectionContract{}
- // err = c.Ctx.ReadForm(treeVM)
- // if err != nil {
- // log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
- // errCode = -6
- // return
- // }
- // fmt.Println(treeVM)
- // id, err := utils.GetDecryptId(treeVM.Id)
- // if err != nil {
- // errCode = -6
- // return
- // }
- ext := strings.Join(head.Header["Content-Type"], "")
- id, err = c.ServiceContract.SaveUpload(webLocation, head.Filename, ext)
- if err != nil {
- errCode = -6
- return
- }
- }
- func (c *UploadApi) GetFile() {
- fileData := viewmodels.Upload{}
- err := c.Ctx.ReadForm(&fileData)
- if err != nil {
- c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
- return
- }
- id, err := utils.GetDecryptId(fileData.Id)
- if err != nil {
- c.Ctx.JSON(iris.Map{"code": -1, "msg": "解析异常,请检查参数"})
- return
- }
- fileVM, err := c.ServiceUpload.Get(id)
- if err != nil {
- c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
- return
- }
- // c.Ctx.Header("Content-Type", "text/html; charset=utf-8;")
- // c.Ctx.Header("Content-Disposition", "inline;"+fileVM.Name)
- filepath := conf.NormalFileStorage + fileVM.Path
- file, err := os.Open(filepath)
- readseek := io.ReadSeeker(file)
- if err != nil {
- c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
- return
- }
- // 读取文件缓冲区
- // var buf [128]byte
- // // 文件内容字节数组
- // var content []byte
- // for {
- // n, err := file.Read(buf[:])
- // if err == io.EOF {
- // // 文件读取完成
- // break
- // }
- // if err != nil {
- // c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
- // return
- // }
- // content = append(content, buf[:n]...)
- // }
- // c.Ctx.Header("Content-Disposition", "inline;filename="+fileVM.Name+";")
- c.Ctx.ServeContent(readseek, fileVM.Name, time.Now())
- // c.Ctx.Header("Content-Type", "text/html; charset=utf-8;")
- // p, e := c.Ctx.Write(content)
- // fmt.Println("p", p)
- // fmt.Println("e", e)
- }
|