Try to release 1.0.4

This commit is contained in:
zicla
2018-07-03 15:40:16 +08:00
parent b91374bdf3
commit b78d8bc3b6
2 changed files with 77 additions and 100 deletions

View File

@ -3,12 +3,9 @@ package rest
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"os"
"regexp" "regexp"
"strconv" "strconv"
"time" "time"
"strings"
"net/url"
) )
type AlienController struct { type AlienController struct {
@ -343,52 +340,12 @@ func (this *AlienController) Download(writer http.ResponseWriter, request *http.
} }
} }
diskFile, err := os.Open(CONFIG.MatterPath + matter.Path)
this.PanicError(err)
defer diskFile.Close()
//对图片做缩放处理。 //对图片做缩放处理。
imageProcess := request.FormValue("imageProcess") imageProcess := request.FormValue("imageProcess")
if imageProcess == "resize" { if imageProcess == "resize" {
this.matterService.ResizeImage(writer, request, matter, diskFile) this.matterService.ResizeImage(writer, request, matter)
} else { } else {
this.matterService.DownloadFile(writer, request, matter)
//如果是图片或者文本或者视频就直接打开。其余的一律以下载形式返回。
fileName := url.QueryEscape(matter.Name)
mimeType := GetMimeType(fileName)
if strings.Index(mimeType, "image") != 0 && strings.Index(mimeType, "text") != 0 && strings.Index(mimeType, "video") != 0 {
writer.Header().Set("content-disposition", "attachment; filename=\""+fileName+"\"")
}
this.matterService.DownloadFile(writer, request, matter, diskFile)
//显示文件大小。
//fileInfo, err := diskFile.Stat()
//if err != nil {
// panic(err)
//}
//
//contentLength := strconv.Itoa(int(fileInfo.Size()))
//writer.Header().Set("Content-Length", contentLength)
//
////如果是视频,支持断点下载。
//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)
} }
} }

View File

@ -167,7 +167,11 @@ func (this *MatterService) Upload(file multipart.File, user *User, puuid string,
} }
//图片预处理功能。 //图片预处理功能。
func (this *MatterService) ResizeImage(writer http.ResponseWriter, request *http.Request, matter *Matter, diskFile *os.File) { func (this *MatterService) ResizeImage(writer http.ResponseWriter, request *http.Request, matter *Matter) {
diskFile, err := os.Open(CONFIG.MatterPath + matter.Path)
this.PanicError(err)
defer diskFile.Close()
// 防止中文乱码 // 防止中文乱码
fileName := url.QueryEscape(matter.Name) fileName := url.QueryEscape(matter.Name)
@ -265,8 +269,35 @@ func (this *MatterService) ResizeImage(writer http.ResponseWriter, request *http
} }
// httpRange specifies the byte range to be sent to the client.
type httpRange struct {
start, length int64
}
func (r httpRange) contentRange(size int64) string {
return fmt.Sprintf("bytes %d-%d/%d", r.start, r.start+r.length-1, size)
}
func (r httpRange) mimeHeader(contentType string, size int64) textproto.MIMEHeader {
return textproto.MIMEHeader{
"Content-Range": {r.contentRange(size)},
"Content-Type": {contentType},
}
}
// countingWriter counts how many bytes have been written to it.
type countingWriter int64
func (w *countingWriter) Write(p []byte) (n int, err error) {
*w += countingWriter(len(p))
return len(p), nil
}
//检查Last-Modified头。返回true: 请求已经完成了。(言下之意,文件没有修改过) 返回false文件修改过。 //检查Last-Modified头。返回true: 请求已经完成了。(言下之意,文件没有修改过) 返回false文件修改过。
func checkLastModified(w http.ResponseWriter, r *http.Request, modifyTime time.Time) bool { func (this *MatterService) checkLastModified(w http.ResponseWriter, r *http.Request, modifyTime time.Time) bool {
if modifyTime.IsZero() { if modifyTime.IsZero() {
return false return false
} }
@ -293,7 +324,7 @@ func checkLastModified(w http.ResponseWriter, r *http.Request, modifyTime time.T
// //
// The return value is the effective request "Range" header to use and // The return value is the effective request "Range" header to use and
// whether this request is now considered done. // whether this request is now considered done.
func checkETag(w http.ResponseWriter, r *http.Request, modtime time.Time) (rangeReq string, done bool) { func (this *MatterService) checkETag(w http.ResponseWriter, r *http.Request, modtime time.Time) (rangeReq string, done bool) {
etag := w.Header().Get("Etag") etag := w.Header().Get("Etag")
rangeReq = r.Header.Get("Range") rangeReq = r.Header.Get("Range")
@ -323,7 +354,7 @@ func checkETag(w http.ResponseWriter, r *http.Request, modtime time.Time) (range
return rangeReq, false return rangeReq, false
} }
// TODO(bradfitz): non-GET/HEAD requests require more work: // (bradfitz): non-GET/HEAD requests require more work:
// sending a different status code on matches, and // sending a different status code on matches, and
// also can't use weak cache validators (those with a "W/ // also can't use weak cache validators (those with a "W/
// prefix). But most users of ServeContent will be using // prefix). But most users of ServeContent will be using
@ -332,7 +363,7 @@ func checkETag(w http.ResponseWriter, r *http.Request, modtime time.Time) (range
return rangeReq, false return rangeReq, false
} }
// TODO(bradfitz): deal with comma-separated or multiple-valued // (bradfitz): deal with comma-separated or multiple-valued
// list of If-None-match values. For now just handle the common // list of If-None-match values. For now just handle the common
// case of a single item. // case of a single item.
if inm == etag || inm == "*" { if inm == etag || inm == "*" {
@ -346,24 +377,9 @@ func checkETag(w http.ResponseWriter, r *http.Request, modtime time.Time) (range
return rangeReq, false return rangeReq, false
} }
// httpRange specifies the byte range to be sent to the client.
type httpRange struct {
start, length int64
}
func (r httpRange) contentRange(size int64) string {
return fmt.Sprintf("bytes %d-%d/%d", r.start, r.start+r.length-1, size)
}
func (r httpRange) mimeHeader(contentType string, size int64) textproto.MIMEHeader {
return textproto.MIMEHeader{
"Content-Range": {r.contentRange(size)},
"Content-Type": {contentType},
}
}
// parseRange parses a Range header string as per RFC 2616. // parseRange parses a Range header string as per RFC 2616.
func parseRange(s string, size int64) ([]httpRange, error) { func (this *MatterService) parseRange(s string, size int64) ([]httpRange, error) {
if s == "" { if s == "" {
return nil, nil // header not present return nil, nil // header not present
} }
@ -420,17 +436,10 @@ func parseRange(s string, size int64) ([]httpRange, error) {
return ranges, nil return ranges, nil
} }
// countingWriter counts how many bytes have been written to it.
type countingWriter int64
func (w *countingWriter) Write(p []byte) (n int, err error) {
*w += countingWriter(len(p))
return len(p), nil
}
// rangesMIMESize returns the number of bytes it takes to encode the // rangesMIMESize returns the number of bytes it takes to encode the
// provided ranges as a multipart response. // provided ranges as a multipart response.
func rangesMIMESize(ranges []httpRange, contentType string, contentSize int64) (encSize int64) { func (this *MatterService) rangesMIMESize(ranges []httpRange, contentType string, contentSize int64) (encSize int64) {
var w countingWriter var w countingWriter
mw := multipart.NewWriter(&w) mw := multipart.NewWriter(&w)
for _, ra := range ranges { for _, ra := range ranges {
@ -442,7 +451,7 @@ func rangesMIMESize(ranges []httpRange, contentType string, contentSize int64) (
return return
} }
func sumRangesSize(ranges []httpRange) (size int64) { func (this *MatterService) sumRangesSize(ranges []httpRange) (size int64) {
for _, ra := range ranges { for _, ra := range ranges {
size += ra.length size += ra.length
} }
@ -450,20 +459,31 @@ func sumRangesSize(ranges []httpRange) (size int64) {
} }
//文件下载功能。 //文件下载功能。
func (this *MatterService) DownloadFile(w http.ResponseWriter, r *http.Request, matter *Matter, content *os.File) { func (this *MatterService) DownloadFile(writer http.ResponseWriter, request *http.Request, matter *Matter) {
diskFile, err := os.Open(CONFIG.MatterPath + matter.Path)
this.PanicError(err)
defer diskFile.Close()
//如果是图片或者文本或者视频就直接打开。其余的一律以下载形式返回。
fileName := url.QueryEscape(matter.Name)
mimeType := GetMimeType(fileName)
if strings.Index(mimeType, "image") != 0 && strings.Index(mimeType, "text") != 0 && strings.Index(mimeType, "video") != 0 {
writer.Header().Set("content-disposition", "attachment; filename=\""+fileName+"\"")
}
//显示文件大小。 //显示文件大小。
fileInfo, err := content.Stat() fileInfo, err := diskFile.Stat()
if err != nil { if err != nil {
panic(err) this.PanicWebError(err.Error(), http.StatusInternalServerError)
} }
modtime := fileInfo.ModTime() modifyTime := fileInfo.ModTime()
if checkLastModified(w, r, modtime) { if this.checkLastModified(writer, request, modifyTime) {
return return
} }
rangeReq, done := checkETag(w, r, modtime) rangeReq, done := this.checkETag(writer, request, modifyTime)
if done { if done {
return return
} }
@ -476,22 +496,22 @@ func (this *MatterService) DownloadFile(w http.ResponseWriter, r *http.Request,
// If Content-Type isn't set, use the file's extension to find it, but // If Content-Type isn't set, use the file's extension to find it, but
// if the Content-Type is unset explicitly, do not sniff the type. // if the Content-Type is unset explicitly, do not sniff the type.
ctypes, haveType := w.Header()["Content-Type"] ctypes, haveType := writer.Header()["Content-Type"]
var ctype string var ctype string
if !haveType { if !haveType {
ctype = mime.TypeByExtension(filepath.Ext(fileInfo.Name())) ctype = mime.TypeByExtension(filepath.Ext(fileInfo.Name()))
if ctype == "" { if ctype == "" {
// read a chunk to decide between utf-8 text and binary // read a chunk to decide between utf-8 text and binary
var buf [sniffLen]byte var buf [sniffLen]byte
n, _ := io.ReadFull(content, buf[:]) n, _ := io.ReadFull(diskFile, buf[:])
ctype = http.DetectContentType(buf[:n]) ctype = http.DetectContentType(buf[:n])
_, err := content.Seek(0, os.SEEK_SET) // rewind to output whole file _, err := diskFile.Seek(0, os.SEEK_SET) // rewind to output whole file
if err != nil { if err != nil {
this.PanicWebError("无法准确定位文件", http.StatusInternalServerError) this.PanicWebError("无法准确定位文件", http.StatusInternalServerError)
return return
} }
} }
w.Header().Set("Content-Type", ctype) writer.Header().Set("Content-Type", ctype)
} else if len(ctypes) > 0 { } else if len(ctypes) > 0 {
ctype = ctypes[0] ctype = ctypes[0]
} }
@ -500,15 +520,15 @@ func (this *MatterService) DownloadFile(w http.ResponseWriter, r *http.Request,
// handle Content-Range header. // handle Content-Range header.
sendSize := size sendSize := size
var sendContent io.Reader = content var sendContent io.Reader = diskFile
if size >= 0 { if size >= 0 {
ranges, err := parseRange(rangeReq, size) ranges, err := this.parseRange(rangeReq, size)
if err != nil { if err != nil {
panic("range header出错") panic("range header出错")
this.PanicWebError("range header error", http.StatusRequestedRangeNotSatisfiable) this.PanicWebError("range header error", http.StatusRequestedRangeNotSatisfiable)
return return
} }
if sumRangesSize(ranges) > size { if this.sumRangesSize(ranges) > size {
// The total number of bytes in all the ranges // The total number of bytes in all the ranges
// is larger than the size of the file by // is larger than the size of the file by
// itself, so this is probably an attack, or a // itself, so this is probably an attack, or a
@ -529,20 +549,20 @@ func (this *MatterService) DownloadFile(w http.ResponseWriter, r *http.Request,
// A response to a request for a single range MUST NOT // A response to a request for a single range MUST NOT
// be sent using the multipart/byteranges media type." // be sent using the multipart/byteranges media type."
ra := ranges[0] ra := ranges[0]
if _, err := content.Seek(ra.start, os.SEEK_SET); err != nil { if _, err := diskFile.Seek(ra.start, io.SeekStart); err != nil {
this.PanicWebError(err.Error(), http.StatusRequestedRangeNotSatisfiable) this.PanicWebError(err.Error(), http.StatusRequestedRangeNotSatisfiable)
return return
} }
sendSize = ra.length sendSize = ra.length
code = http.StatusPartialContent code = http.StatusPartialContent
w.Header().Set("Content-Range", ra.contentRange(size)) writer.Header().Set("Content-Range", ra.contentRange(size))
case len(ranges) > 1: case len(ranges) > 1:
sendSize = rangesMIMESize(ranges, ctype, size) sendSize = this.rangesMIMESize(ranges, ctype, size)
code = http.StatusPartialContent code = http.StatusPartialContent
pr, pw := io.Pipe() pr, pw := io.Pipe()
mw := multipart.NewWriter(pw) mw := multipart.NewWriter(pw)
w.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary()) writer.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
sendContent = pr sendContent = pr
defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish. defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish.
go func() { go func() {
@ -552,11 +572,11 @@ func (this *MatterService) DownloadFile(w http.ResponseWriter, r *http.Request,
pw.CloseWithError(err) pw.CloseWithError(err)
return return
} }
if _, err := content.Seek(ra.start, os.SEEK_SET); err != nil { if _, err := diskFile.Seek(ra.start, io.SeekStart); err != nil {
pw.CloseWithError(err) pw.CloseWithError(err)
return return
} }
if _, err := io.CopyN(part, content, ra.length); err != nil { if _, err := io.CopyN(part, diskFile, ra.length); err != nil {
pw.CloseWithError(err) pw.CloseWithError(err)
return return
} }
@ -566,16 +586,16 @@ func (this *MatterService) DownloadFile(w http.ResponseWriter, r *http.Request,
}() }()
} }
w.Header().Set("Accept-Ranges", "bytes") writer.Header().Set("Accept-Ranges", "bytes")
if w.Header().Get("Content-Encoding") == "" { if writer.Header().Get("Content-Encoding") == "" {
w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10)) writer.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
} }
} }
w.WriteHeader(code) writer.WriteHeader(code)
if r.Method != "HEAD" { if request.Method != "HEAD" {
io.CopyN(w, sendContent, sendSize) io.CopyN(writer, sendContent, sendSize)
} }
} }