|
@@ -18,6 +18,8 @@ import (
|
|
|
"github.com/kataras/iris/v12"
|
|
|
"go.mod/conf"
|
|
|
"go.mod/services"
|
|
|
+ "go.mod/web/utils"
|
|
|
+ "go.mod/web/viewmodels"
|
|
|
)
|
|
|
|
|
|
type UploadApi struct {
|
|
@@ -25,6 +27,7 @@ type UploadApi struct {
|
|
|
Ctx iris.Context
|
|
|
// // 需要用的service
|
|
|
ServiceContract services.ContractService
|
|
|
+ ServiceUpload services.UploadService
|
|
|
}
|
|
|
|
|
|
// Upload : 处理文件上传
|
|
@@ -45,7 +48,6 @@ func (c *UploadApi) Post() {
|
|
|
file, head, err := c.Ctx.FormFile("file")
|
|
|
if err != nil {
|
|
|
log.Printf("Failed to get form data, err:%s\n", err.Error())
|
|
|
- fmt.Println("1111")
|
|
|
errCode = -1
|
|
|
return
|
|
|
}
|
|
@@ -54,7 +56,6 @@ func (c *UploadApi) Post() {
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
if _, err := io.Copy(buf, file); err != nil {
|
|
|
log.Printf("Failed to get file data, err:%s\n", err.Error())
|
|
|
- fmt.Println("2222")
|
|
|
errCode = -2
|
|
|
return
|
|
|
}
|
|
@@ -64,11 +65,10 @@ func (c *UploadApi) Post() {
|
|
|
FileSize := int64(len(buf.Bytes()))
|
|
|
UploadAt := fmt.Sprintf("%d", time.Now().UnixNano())
|
|
|
|
|
|
- Location := conf.MergeLocalRootDir + UploadAt + head.Filename // 存储地址
|
|
|
+ Location := conf.MergeLocalRootDir + UploadAt // 存储地址
|
|
|
webLocation := "/public/" + UploadAt + head.Filename
|
|
|
newFile, err := os.Create(Location)
|
|
|
if err != nil {
|
|
|
- fmt.Println("33333")
|
|
|
log.Printf("Failed to create file, err:%s\n", err.Error())
|
|
|
errCode = -3
|
|
|
return
|
|
@@ -77,7 +77,6 @@ func (c *UploadApi) Post() {
|
|
|
|
|
|
nByte, err := newFile.Write(buf.Bytes())
|
|
|
if int64(nByte) != FileSize || err != nil {
|
|
|
- fmt.Println("44444")
|
|
|
log.Printf("Failed to save data into file, writtenSize:%d, err:%s\n", nByte, err.Error())
|
|
|
errCode = -4
|
|
|
return
|
|
@@ -105,9 +104,58 @@ func (c *UploadApi) Post() {
|
|
|
|
|
|
id, err = c.ServiceContract.SaveUpload(webLocation, head.Filename, ext)
|
|
|
if err != nil {
|
|
|
- fmt.Println("55555")
|
|
|
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)
|
|
|
+}
|