background/controller/file.go

297 lines
6.2 KiB
Go
Raw Normal View History

2019-03-07 06:06:29 +00:00
package controller
2019-01-29 13:57:53 +00:00
import (
"background/utils"
2020-03-27 17:11:28 +00:00
"image"
"image/color"
2021-02-03 13:43:49 +00:00
"image/jpeg"
"io/ioutil"
2019-01-29 13:57:53 +00:00
"log"
"net/http"
2019-01-29 13:57:53 +00:00
"os"
2021-02-03 13:43:49 +00:00
"github.com/disintegration/imaging"
"github.com/gin-gonic/gin"
json "github.com/json-iterator/go"
"github.com/nfnt/resize"
uuid "github.com/satori/go.uuid"
2021-02-02 17:47:01 +00:00
// "github.com/nfnt/resize"
// "image/jpeg"
"image/gif"
// "strings"
2019-01-29 13:57:53 +00:00
)
2019-03-07 06:06:29 +00:00
type FileController struct {
}
func (this *FileController) OnUpload(c *gin.Context) {
2021-02-16 06:05:21 +00:00
uid, e := uuid.NewV1()
if nil != e {
log.Print(e.Error())
return
2019-01-29 13:57:53 +00:00
}
2021-02-03 13:43:49 +00:00
imgtype := c.Query("type")
log.Print(imgtype)
2020-05-24 06:27:39 +00:00
file, _, err := c.Request.FormFile("image")
2021-02-16 06:05:21 +00:00
if nil != err || nil == file {
log.Print(err.Error())
return
}
2021-02-16 06:05:21 +00:00
if imgtype == "gif" {
allgifs, er := gif.DecodeAll(file)
if nil != er {
log.Print("decode error", er.Error())
2021-02-02 17:47:01 +00:00
return
}
2021-02-16 06:05:21 +00:00
datout, err := os.Create("image/" + uid.String() + ".gif")
2021-02-02 17:47:01 +00:00
defer datout.Close()
if err != nil {
log.Fatal(err)
return
}
2021-02-16 06:05:21 +00:00
gif.EncodeAll(datout, allgifs)
c.JSON(200, map[string]interface{}{"url": uid.String() + ".gif"})
} else {
2021-02-03 13:43:49 +00:00
img, name, err := image.Decode(file)
2021-02-16 06:05:21 +00:00
if nil != err {
2021-02-03 13:43:49 +00:00
log.Print(err.Error())
return
}
dx := img.Bounds().Dx()
// resize to width 1000 using Lanczos resampling
// and preserve aspect ratio
2021-02-16 06:05:21 +00:00
if dx > 800 {
dx = dx / 2
2021-02-03 13:43:49 +00:00
}
m := resize.Resize(uint(dx), 0, img, resize.Lanczos3)
2021-02-02 17:47:01 +00:00
2021-02-03 13:43:49 +00:00
datout, err := os.Create("image/" + uid.String() + "." + name)
defer datout.Close()
if err != nil {
log.Fatal(err)
}
jpeg.Encode(datout, m, nil)
2021-02-16 06:05:21 +00:00
c.JSON(200, map[string]interface{}{"url": uid.String() + "." + name})
2021-02-03 13:43:49 +00:00
}
2021-02-16 06:05:21 +00:00
}
2021-11-03 16:46:33 +00:00
func (this *FileController) OnUploadOrigin(c *gin.Context) {
uid, e := uuid.NewV1()
if nil != e {
log.Print(e.Error())
return
}
imgtype := c.Query("type")
file, _, err := c.Request.FormFile("image")
if nil != err || nil == file {
log.Print(err.Error())
return
}
if imgtype == "gif" {
allgifs, er := gif.DecodeAll(file)
if nil != er {
log.Print("decode error", er.Error())
return
}
datout, err := os.Create("image/" + uid.String() + ".gif")
defer datout.Close()
if err != nil {
log.Fatal(err)
return
}
gif.EncodeAll(datout, allgifs)
c.JSON(200, map[string]interface{}{"url": uid.String() + ".gif"})
} else {
img, name, err := image.Decode(file)
if nil != err {
log.Print(err.Error())
return
}
dx := img.Bounds().Dx()
// resize to width 1000 using Lanczos resampling
// and preserve aspect ratio
m := resize.Resize(uint(dx), 0, img, resize.Lanczos3)
datout, err := os.Create("image/" + uid.String() + "." + name)
defer datout.Close()
if err != nil {
log.Fatal(err)
}
jpeg.Encode(datout, m, nil)
c.JSON(200, map[string]interface{}{"url": uid.String() + "." + name})
}
}
func (this *FileController) FileList(c *gin.Context) {
2020-05-11 07:14:22 +00:00
var nodes utils.FileDesc
2020-05-11 03:35:45 +00:00
path := c.Query("path")
2021-02-16 06:05:21 +00:00
utils.GetPathFileName("files\\"+path, &nodes)
2020-05-11 07:14:22 +00:00
log.Print(nodes)
2021-02-16 06:05:21 +00:00
bs, e := json.Marshal(nodes)
if nil != e {
2020-05-11 07:14:22 +00:00
log.Print(e.Error())
}
log.Print(string(bs))
2021-02-16 06:05:21 +00:00
c.JSON(200, map[string]interface{}{
"msg": "ok",
"status": 200,
"files": nodes,
2020-05-11 03:35:45 +00:00
})
}
2020-04-28 14:57:52 +00:00
func (this *FileController) FileType(c *gin.Context) {
}
2020-04-28 14:57:52 +00:00
func (this *FileController) DownloadFile(c *gin.Context) {
2021-02-16 06:05:21 +00:00
resp := RespBase{Msg: "FAIL", Status: 211}
2020-05-11 08:28:25 +00:00
filename := c.Query("filename")
2021-02-16 06:05:21 +00:00
file, e := os.Open("files//" + filename)
if nil != e {
2020-05-11 08:28:25 +00:00
log.Print(e.Error())
2021-02-16 06:05:21 +00:00
c.JSON(200, resp)
2020-05-11 08:28:25 +00:00
return
}
2021-02-16 06:05:21 +00:00
bytes, e := ioutil.ReadAll(file)
if nil != e {
2020-05-11 08:28:25 +00:00
log.Print(e.Error())
2021-02-16 06:05:21 +00:00
c.JSON(200, resp)
2020-05-11 08:28:25 +00:00
return
}
c.Header("X-Content-Type-Options", "nosniff")
c.Header("Content-Disposition",
2021-02-16 06:05:21 +00:00
"/files/"+filename)
2020-05-11 08:28:25 +00:00
c.Writer.Write(bytes)
2020-04-28 14:57:52 +00:00
}
2020-05-11 03:35:45 +00:00
func (this *FileController) OnFileUploadFile(c *gin.Context) {
path := c.Query("path")
filename := c.Query("filename")
file, _, err := c.Request.FormFile("file")
2021-02-16 06:05:21 +00:00
if nil != err || nil == file {
log.Print(err.Error())
return
}
2021-02-16 06:05:21 +00:00
bytes, err := ioutil.ReadAll(file)
if nil != err {
log.Print(err.Error())
return
}
2021-02-16 06:05:21 +00:00
header := make([]byte, 512)
copy(header, bytes)
types := http.DetectContentType(header)
log.Print(types)
// jpg
2020-05-11 03:35:45 +00:00
defer file.Close()
2021-02-16 06:05:21 +00:00
out, err := os.Create("files/" + path + "/" + filename)
2020-05-11 03:35:45 +00:00
if err != nil {
log.Print(err)
}
2020-05-11 03:35:45 +00:00
defer out.Close()
out.Write(bytes)
log.Print(len(bytes))
2021-02-16 06:05:21 +00:00
c.JSON(200, map[string]interface{}{"msg": "ok"})
2019-01-29 13:57:53 +00:00
}
2020-03-27 17:11:28 +00:00
func (this *FileController) OnThumbnail(c *gin.Context) {
2021-02-16 06:05:21 +00:00
resp := RespBase{Msg: "FAIL", Status: 211}
2019-09-02 16:22:39 +00:00
fileName := c.Param("file")
2021-02-16 06:05:21 +00:00
if "" == fileName {
c.JSON(200, resp)
2019-09-02 16:22:39 +00:00
return
}
2020-03-27 17:11:28 +00:00
// input files
2021-02-16 06:05:21 +00:00
files := []string{"/home/ubuntu/api/bin/image/" + fileName}
2020-03-27 17:11:28 +00:00
// load images and make 100x100 thumbnails of them
var thumbnails []image.Image
for _, file := range files {
img, err := imaging.Open(file)
if err != nil {
panic(err)
}
thumb := imaging.Thumbnail(img, 100, 100, imaging.CatmullRom)
thumbnails = append(thumbnails, thumb)
}
// create a new blank image
dst := imaging.New(100*len(thumbnails), 100, color.NRGBA{0, 0, 0, 0})
// paste thumbnails into the new image side by side
for i, thumb := range thumbnails {
dst = imaging.Paste(dst, thumb, image.Pt(i*100, 0))
}
// save the combined image to file
2021-02-16 06:05:21 +00:00
err := imaging.Save(dst, "/home/ubuntu/api/bin/image/thumbnail_"+fileName)
2020-03-27 17:11:28 +00:00
if err != nil {
log.Print(err.Error())
return
}
// open file
2021-02-16 06:05:21 +00:00
file, e := os.Open("/home/ubuntu/api/bin/image/thumbnail_" + fileName)
if nil != e {
2019-09-02 16:22:39 +00:00
log.Print(e.Error())
2021-02-16 06:05:21 +00:00
c.JSON(200, resp)
2019-09-02 16:22:39 +00:00
return
2019-01-29 13:57:53 +00:00
}
defer file.Close()
2021-02-16 06:05:21 +00:00
bytes, e := ioutil.ReadAll(file)
if nil != e {
2019-09-02 16:22:39 +00:00
log.Print(e.Error())
2021-02-16 06:05:21 +00:00
c.JSON(200, resp)
2019-09-02 16:22:39 +00:00
return
2019-01-29 13:57:53 +00:00
}
c.Header("X-Content-Type-Options", "nosniff")
2021-02-16 06:05:21 +00:00
c.Header("Content-Type", "image/png")
2019-09-02 16:22:39 +00:00
c.Header("Content-Disposition",
2021-02-16 06:05:21 +00:00
"/image/"+fileName)
2019-09-02 16:22:39 +00:00
c.Writer.Write(bytes)
2019-01-29 13:57:53 +00:00
}
2020-03-27 16:00:46 +00:00
2021-02-16 06:05:21 +00:00
func (this *FileController) OnDownLoad(c *gin.Context) {
resp := RespBase{Msg: "FAIL", Status: 211}
2020-03-27 16:00:46 +00:00
fileName := c.Param("file")
2021-02-16 06:05:21 +00:00
if "" == fileName {
c.JSON(200, resp)
2020-03-27 16:00:46 +00:00
return
}
2021-02-16 06:05:21 +00:00
file, e := os.Open(utils.GetCurrentDirectory() + "/image/" + fileName)
2021-02-16 06:05:21 +00:00
if nil != e {
2020-03-27 16:00:46 +00:00
log.Print(e.Error())
2021-02-16 06:05:21 +00:00
c.JSON(200, resp)
2020-03-27 16:00:46 +00:00
return
}
defer file.Close()
2021-02-16 06:05:21 +00:00
bytes, e := ioutil.ReadAll(file)
if nil != e {
2020-03-27 16:00:46 +00:00
log.Print(e.Error())
2021-02-16 06:05:21 +00:00
c.JSON(200, resp)
2020-03-27 16:00:46 +00:00
return
}
c.Header("X-Content-Type-Options", "nosniff")
2021-02-16 06:05:21 +00:00
c.Header("Content-Type", "image/png")
2020-03-27 16:00:46 +00:00
c.Header("Content-Disposition",
2021-02-16 06:05:21 +00:00
"/image/"+fileName)
2020-03-27 16:00:46 +00:00
c.Writer.Write(bytes)
2021-02-16 06:05:21 +00:00
}