background/controller/file.go

241 lines
5.1 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
"github.com/disintegration/imaging"
2019-01-29 13:57:53 +00:00
"github.com/gin-gonic/gin"
2020-05-11 07:14:22 +00:00
json "github.com/json-iterator/go"
uuid "github.com/satori/go.uuid"
2020-03-27 17:11:28 +00:00
"image"
"image/color"
"io/ioutil"
2019-01-29 13:57:53 +00:00
"log"
"net/http"
2019-01-29 13:57:53 +00:00
"os"
"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) {
uid,e := uuid.NewV1()
if nil != e{
log.Print(e.Error())
return
2019-01-29 13:57:53 +00:00
}
2020-05-24 06:27:39 +00:00
file, _, err := c.Request.FormFile("image")
if nil != err || nil == file{
log.Print(err.Error())
return
}
bytes,err := ioutil.ReadAll(file)
if nil != err{
log.Print(err.Error())
return
}
header := make([]byte,512)
copy(header,bytes)
types := http.DetectContentType(header)
log.Print(types)
// jpg
if strings.Contains(types,"jpeg"){
defer file.Close()
out, err := os.Create("image/" + uid.String() + ".jpg")
if err != nil {
log.Fatal(err)
}
defer out.Close()
out.Write(bytes)
log.Print(len(bytes))
2019-12-29 09:42:27 +00:00
c.JSON(200, map[string] interface{}{"url":uid.String() + ".jpg" })
2019-01-29 13:57:53 +00:00
}
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))
2019-12-29 09:42:27 +00:00
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))
2019-12-29 09:42:27 +00:00
c.JSON(200, map[string] interface{}{"url":uid.String() + ".gif" } )
}
}
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")
2020-05-11 07:14:22 +00:00
utils.GetPathFileName("files\\" + path,&nodes)
log.Print(nodes)
bs,e := json.Marshal(nodes)
if nil != e{
log.Print(e.Error())
}
log.Print(string(bs))
2020-05-11 03:35:45 +00:00
c.JSON(200,map[string]interface{}{
"msg":"ok",
"status":200,
2020-05-11 07:14:22 +00:00
"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) {
2020-05-11 08:28:25 +00:00
resp := RespBase{Msg:"FAIL",Status: 211}
filename := c.Query("filename")
file,e := os.Open( "files//" +filename)
if nil != e{
log.Print(e.Error())
c.JSON(200,resp)
return
}
bytes,e :=ioutil.ReadAll(file)
if nil != e{
log.Print(e.Error())
c.JSON(200,resp)
return
}
c.Header("X-Content-Type-Options", "nosniff")
c.Header("Content-Disposition",
"/files/" + filename)
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")
if nil != err || nil == file{
log.Print(err.Error())
return
}
bytes,err := ioutil.ReadAll(file)
if nil != err{
log.Print(err.Error())
return
}
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()
out, err := os.Create("files/" + path + "/" + filename)
if err != nil {
log.Print(err)
}
2020-05-11 03:35:45 +00:00
defer out.Close()
out.Write(bytes)
log.Print(len(bytes))
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) {
2019-09-02 16:22:39 +00:00
resp := RespBase{Msg:"FAIL",Status: 211}
2019-09-02 16:22:39 +00:00
fileName := c.Param("file")
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
files := []string{"/home/ubuntu/api/bin/image/" +fileName}
// 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
err := imaging.Save(dst, "/home/ubuntu/api/bin/image/thumbnail_" + fileName)
if err != nil {
log.Print(err.Error())
return
}
// open file
file,e := os.Open("/home/ubuntu/api/bin/image/thumbnail_" + fileName)
2019-09-02 16:22:39 +00:00
if nil != e{
log.Print(e.Error())
c.JSON(200,resp)
2019-09-02 16:22:39 +00:00
return
2019-01-29 13:57:53 +00:00
}
defer file.Close()
2019-09-02 16:22:39 +00:00
bytes,e :=ioutil.ReadAll(file)
if nil != e{
log.Print(e.Error())
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")
2019-09-02 16:22:39 +00:00
c.Header("Content-Type","image/png")
c.Header("Content-Disposition",
"/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
2020-03-27 17:11:28 +00:00
func (this *FileController) OnDownLoad(c *gin.Context) {
2020-03-27 16:00:46 +00:00
resp := RespBase{Msg:"FAIL",Status: 211}
fileName := c.Param("file")
if "" == fileName{
c.JSON(200,resp)
return
}
file,e := os.Open(utils.GetCurrentDirectory() + "/image/" +fileName)
2020-03-27 16:00:46 +00:00
if nil != e{
log.Print(e.Error())
c.JSON(200,resp)
return
}
defer file.Close()
2020-03-27 16:00:46 +00:00
bytes,e :=ioutil.ReadAll(file)
if nil != e{
log.Print(e.Error())
c.JSON(200,resp)
return
}
c.Header("X-Content-Type-Options", "nosniff")
c.Header("Content-Type","image/png")
c.Header("Content-Disposition",
"/image/" +fileName)
c.Writer.Write(bytes)
}