Ready to add the file downloader.
This commit is contained in:
@ -2,8 +2,6 @@ package rest
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/disintegration/imaging"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@ -348,118 +346,49 @@ func (this *AlienController) Download(writer http.ResponseWriter, request *http.
|
|||||||
diskFile, err := os.Open(CONFIG.MatterPath + matter.Path)
|
diskFile, err := os.Open(CONFIG.MatterPath + matter.Path)
|
||||||
this.PanicError(err)
|
this.PanicError(err)
|
||||||
defer diskFile.Close()
|
defer diskFile.Close()
|
||||||
|
|
||||||
// 防止中文乱码
|
// 防止中文乱码
|
||||||
fileName := url.QueryEscape(matter.Name)
|
fileName := url.QueryEscape(matter.Name)
|
||||||
writer.Header().Set("Content-Type", GetMimeType(fileName))
|
mimeType := GetMimeType(fileName)
|
||||||
|
writer.Header().Set("Content-Type", mimeType)
|
||||||
|
|
||||||
//如果是图片或者文本就直接打开。
|
//如果是图片或者文本或者视频就直接打开。其余的一律以下载形式返回。
|
||||||
mimeType := GetMimeType(matter.Name)
|
if strings.Index(mimeType, "image") != 0 && strings.Index(mimeType, "text") != 0 && strings.Index(mimeType, "video") != 0 {
|
||||||
if strings.Index(mimeType, "image") != 0 && strings.Index(mimeType, "text") != 0 {
|
|
||||||
writer.Header().Set("content-disposition", "attachment; filename=\""+fileName+"\"")
|
writer.Header().Set("content-disposition", "attachment; filename=\""+fileName+"\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
//对图片做缩放处理。
|
//对图片做缩放处理。
|
||||||
imageProcess := request.FormValue("imageProcess")
|
imageProcess := request.FormValue("imageProcess")
|
||||||
if imageProcess == "resize" {
|
if imageProcess == "resize" {
|
||||||
|
this.matterService.ResizeImage(writer, request, matter, diskFile)
|
||||||
//当前的文件是否是图片,只有图片才能处理。
|
|
||||||
extension := GetExtension(matter.Name)
|
|
||||||
formats := map[string]imaging.Format{
|
|
||||||
".jpg": imaging.JPEG,
|
|
||||||
".jpeg": imaging.JPEG,
|
|
||||||
".png": imaging.PNG,
|
|
||||||
".tif": imaging.TIFF,
|
|
||||||
".tiff": imaging.TIFF,
|
|
||||||
".bmp": imaging.BMP,
|
|
||||||
".gif": imaging.GIF,
|
|
||||||
}
|
|
||||||
|
|
||||||
format, ok := formats[extension]
|
|
||||||
if !ok {
|
|
||||||
panic("该图片格式不支持处理")
|
|
||||||
}
|
|
||||||
|
|
||||||
imageResizeM := request.FormValue("imageResizeM")
|
|
||||||
if imageResizeM == "" {
|
|
||||||
imageResizeM = "fit"
|
|
||||||
} else if imageResizeM != "fit" && imageResizeM != "fill" && imageResizeM != "fixed" {
|
|
||||||
panic("imageResizeM参数错误")
|
|
||||||
}
|
|
||||||
imageResizeWStr := request.FormValue("imageResizeW")
|
|
||||||
var imageResizeW int
|
|
||||||
if imageResizeWStr != "" {
|
|
||||||
imageResizeW, err = strconv.Atoi(imageResizeWStr)
|
|
||||||
this.PanicError(err)
|
|
||||||
if imageResizeW < 1 || imageResizeW > 4096 {
|
|
||||||
panic("缩放尺寸不能超过4096")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
imageResizeHStr := request.FormValue("imageResizeH")
|
|
||||||
var imageResizeH int
|
|
||||||
if imageResizeHStr != "" {
|
|
||||||
imageResizeH, err = strconv.Atoi(imageResizeHStr)
|
|
||||||
this.PanicError(err)
|
|
||||||
if imageResizeH < 1 || imageResizeH > 4096 {
|
|
||||||
panic("缩放尺寸不能超过4096")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//单边缩略
|
|
||||||
if imageResizeM == "fit" {
|
|
||||||
//将图缩略成宽度为100,高度按比例处理。
|
|
||||||
if imageResizeW > 0 {
|
|
||||||
src, err := imaging.Decode(diskFile)
|
|
||||||
this.PanicError(err)
|
|
||||||
dst := imaging.Resize(src, imageResizeW, 0, imaging.Lanczos)
|
|
||||||
|
|
||||||
err = imaging.Encode(writer, dst, format)
|
|
||||||
this.PanicError(err)
|
|
||||||
} else if imageResizeH > 0 {
|
|
||||||
//将图缩略成高度为100,宽度按比例处理。
|
|
||||||
src, err := imaging.Decode(diskFile)
|
|
||||||
this.PanicError(err)
|
|
||||||
dst := imaging.Resize(src, 0, imageResizeH, imaging.Lanczos)
|
|
||||||
|
|
||||||
err = imaging.Encode(writer, dst, format)
|
|
||||||
this.PanicError(err)
|
|
||||||
} else {
|
|
||||||
panic("单边缩略必须指定imageResizeW或imageResizeH")
|
|
||||||
}
|
|
||||||
} else if imageResizeM == "fill" {
|
|
||||||
//固定宽高,自动裁剪
|
|
||||||
if imageResizeW > 0 && imageResizeH > 0 {
|
|
||||||
src, err := imaging.Decode(diskFile)
|
|
||||||
this.PanicError(err)
|
|
||||||
dst := imaging.Fill(src, imageResizeW, imageResizeH, imaging.Center, imaging.Lanczos)
|
|
||||||
err = imaging.Encode(writer, dst, format)
|
|
||||||
this.PanicError(err)
|
|
||||||
} else {
|
|
||||||
panic("固定宽高,自动裁剪 必须同时指定imageResizeW和imageResizeH")
|
|
||||||
}
|
|
||||||
} else if imageResizeM == "fixed" {
|
|
||||||
//强制宽高缩略
|
|
||||||
if imageResizeW > 0 && imageResizeH > 0 {
|
|
||||||
src, err := imaging.Decode(diskFile)
|
|
||||||
this.PanicError(err)
|
|
||||||
dst := imaging.Resize(src, imageResizeW, imageResizeH, imaging.Lanczos)
|
|
||||||
|
|
||||||
err = imaging.Encode(writer, dst, format)
|
|
||||||
this.PanicError(err)
|
|
||||||
} else {
|
|
||||||
panic("强制宽高缩略必须同时指定imageResizeW和imageResizeH")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
//显示文件大小。
|
//显示文件大小。
|
||||||
fileInfo, err := diskFile.Stat()
|
//fileInfo, err := diskFile.Stat()
|
||||||
if err != nil {
|
//if err != nil {
|
||||||
panic(err)
|
// panic(err)
|
||||||
}
|
//}
|
||||||
writer.Header().Set("Content-Length", strconv.Itoa(int(fileInfo.Size())))
|
//
|
||||||
|
//contentLength := strconv.Itoa(int(fileInfo.Size()))
|
||||||
_, err = io.Copy(writer, diskFile)
|
//writer.Header().Set("Content-Length", contentLength)
|
||||||
this.PanicError(err)
|
//
|
||||||
|
////如果是视频,支持断点下载。
|
||||||
|
//if strings.Index(mimeType, "video") == 0 {
|
||||||
|
//
|
||||||
|
// if request.Header.Get("Range") == "" {
|
||||||
|
// writer.Header().Set("Accept-Ranges", "bytes")
|
||||||
|
// } else {
|
||||||
|
// writer.Header().Set("Accept-Ranges", fmt.Sprintf("bytes 0-%s/%s", contentLength, contentLength))
|
||||||
|
// }
|
||||||
|
// writer.Header().Set("Connection", "keep-alive")
|
||||||
|
// //writer.Header().Set("ETag", "5b376c95-355866")
|
||||||
|
// writer.Header().Set("Last-Modified", "Sat, 30 Jun 2018 11:42:13 GMT")
|
||||||
|
// //writer.Header().Set("Server", "nginx/1.12.2")
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//_, err = io.Copy(writer, diskFile)
|
||||||
|
//this.PanicError(err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"net/http"
|
||||||
|
"github.com/disintegration/imaging"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
//@Service
|
//@Service
|
||||||
@ -155,3 +158,97 @@ func (this *MatterService) Upload(file multipart.File, user *User, puuid string,
|
|||||||
|
|
||||||
return matter
|
return matter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//处理图片下载功能。
|
||||||
|
func (this *MatterService) ResizeImage(writer http.ResponseWriter, request *http.Request, matter *Matter, diskFile *os.File) {
|
||||||
|
|
||||||
|
//当前的文件是否是图片,只有图片才能处理。
|
||||||
|
extension := GetExtension(matter.Name)
|
||||||
|
formats := map[string]imaging.Format{
|
||||||
|
".jpg": imaging.JPEG,
|
||||||
|
".jpeg": imaging.JPEG,
|
||||||
|
".png": imaging.PNG,
|
||||||
|
".tif": imaging.TIFF,
|
||||||
|
".tiff": imaging.TIFF,
|
||||||
|
".bmp": imaging.BMP,
|
||||||
|
".gif": imaging.GIF,
|
||||||
|
}
|
||||||
|
|
||||||
|
format, ok := formats[extension]
|
||||||
|
if !ok {
|
||||||
|
panic("该图片格式不支持处理")
|
||||||
|
}
|
||||||
|
|
||||||
|
imageResizeM := request.FormValue("imageResizeM")
|
||||||
|
if imageResizeM == "" {
|
||||||
|
imageResizeM = "fit"
|
||||||
|
} else if imageResizeM != "fit" && imageResizeM != "fill" && imageResizeM != "fixed" {
|
||||||
|
panic("imageResizeM参数错误")
|
||||||
|
}
|
||||||
|
imageResizeWStr := request.FormValue("imageResizeW")
|
||||||
|
var imageResizeW int
|
||||||
|
if imageResizeWStr != "" {
|
||||||
|
imageResizeW, err := strconv.Atoi(imageResizeWStr)
|
||||||
|
this.PanicError(err)
|
||||||
|
if imageResizeW < 1 || imageResizeW > 4096 {
|
||||||
|
panic("缩放尺寸不能超过4096")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
imageResizeHStr := request.FormValue("imageResizeH")
|
||||||
|
var imageResizeH int
|
||||||
|
if imageResizeHStr != "" {
|
||||||
|
imageResizeH, err := strconv.Atoi(imageResizeHStr)
|
||||||
|
this.PanicError(err)
|
||||||
|
if imageResizeH < 1 || imageResizeH > 4096 {
|
||||||
|
panic("缩放尺寸不能超过4096")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//单边缩略
|
||||||
|
if imageResizeM == "fit" {
|
||||||
|
//将图缩略成宽度为100,高度按比例处理。
|
||||||
|
if imageResizeW > 0 {
|
||||||
|
src, err := imaging.Decode(diskFile)
|
||||||
|
this.PanicError(err)
|
||||||
|
dst := imaging.Resize(src, imageResizeW, 0, imaging.Lanczos)
|
||||||
|
|
||||||
|
err = imaging.Encode(writer, dst, format)
|
||||||
|
this.PanicError(err)
|
||||||
|
} else if imageResizeH > 0 {
|
||||||
|
//将图缩略成高度为100,宽度按比例处理。
|
||||||
|
src, err := imaging.Decode(diskFile)
|
||||||
|
this.PanicError(err)
|
||||||
|
dst := imaging.Resize(src, 0, imageResizeH, imaging.Lanczos)
|
||||||
|
|
||||||
|
err = imaging.Encode(writer, dst, format)
|
||||||
|
|
||||||
|
this.PanicError(err)
|
||||||
|
} else {
|
||||||
|
panic("单边缩略必须指定imageResizeW或imageResizeH")
|
||||||
|
}
|
||||||
|
} else if imageResizeM == "fill" {
|
||||||
|
//固定宽高,自动裁剪
|
||||||
|
if imageResizeW > 0 && imageResizeH > 0 {
|
||||||
|
src, err := imaging.Decode(diskFile)
|
||||||
|
this.PanicError(err)
|
||||||
|
dst := imaging.Fill(src, imageResizeW, imageResizeH, imaging.Center, imaging.Lanczos)
|
||||||
|
err = imaging.Encode(writer, dst, format)
|
||||||
|
this.PanicError(err)
|
||||||
|
} else {
|
||||||
|
panic("固定宽高,自动裁剪 必须同时指定imageResizeW和imageResizeH")
|
||||||
|
}
|
||||||
|
} else if imageResizeM == "fixed" {
|
||||||
|
//强制宽高缩略
|
||||||
|
if imageResizeW > 0 && imageResizeH > 0 {
|
||||||
|
src, err := imaging.Decode(diskFile)
|
||||||
|
this.PanicError(err)
|
||||||
|
dst := imaging.Resize(src, imageResizeW, imageResizeH, imaging.Lanczos)
|
||||||
|
|
||||||
|
err = imaging.Encode(writer, dst, format)
|
||||||
|
this.PanicError(err)
|
||||||
|
} else {
|
||||||
|
panic("强制宽高缩略必须同时指定imageResizeW和imageResizeH")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -116,7 +116,7 @@ func (this *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request)
|
|||||||
filePath = dir + "/index.html"
|
filePath = dir + "/index.html"
|
||||||
exists, _ = PathExists(filePath)
|
exists, _ = PathExists(filePath)
|
||||||
if !exists {
|
if !exists {
|
||||||
panic(fmt.Sprintf("404 not found:%s", requestURI))
|
panic(fmt.Sprintf("404 not found:%s", filePath))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user