3 Star 15 Fork 4

eyebluecn / tank

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
matter_service.go 18.58 KB
一键复制 编辑 原始数据 按行查看 历史
Zicla 提交于 2018-07-23 21:31 . Remove the tmp things.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
package rest
import (
"io"
"mime/multipart"
"os"
"regexp"
"strings"
"net/http"
"github.com/disintegration/imaging"
"strconv"
"mime"
"path/filepath"
"net/url"
"errors"
"time"
"fmt"
"net/textproto"
)
//@Service
type MatterService struct {
Bean
matterDao *MatterDao
}
//初始化方法
func (this *MatterService) Init(context *Context) {
//手动装填本实例的Bean. 这里必须要用中间变量方可。
b := context.GetBean(this.matterDao)
if b, ok := b.(*MatterDao); ok {
this.matterDao = b
}
}
//根据一个文件夹路径,找到最后一个文件夹的uuid,如果中途出错,返回err.
func (this *MatterService) GetDirUuid(userUuid string, dir string) string {
if dir == "" {
panic(`文件夹不能为空`)
} else if dir[0:1] != "/" {
panic(`文件夹必须以/开头`)
} else if strings.Index(dir, "//") != -1 {
panic(`文件夹不能出现连续的//`)
} else if m, _ := regexp.MatchString(`[<>|*?\\]`, dir); m {
panic(`文件夹中不能包含以下特殊符号:< > | * ? \`)
}
if dir == "/" {
return "root"
}
if dir[len(dir)-1] == '/' {
dir = dir[:len(dir)-1]
}
//递归找寻文件的上级目录uuid.
folders := strings.Split(dir, "/")
if len(folders) > 32 {
panic("文件夹最多32层。")
}
puuid := "root"
for k, name := range folders {
if len(name) > 200 {
panic("每级文件夹的最大长度为200")
}
if k == 0 {
continue
}
matter := this.matterDao.FindByUserUuidAndPuuidAndNameAndDirTrue(userUuid, puuid, name)
if matter == nil {
//创建一个文件夹。这里一般都是通过alien接口来创建的文件夹。
matter = &Matter{
Puuid: puuid,
UserUuid: userUuid,
Dir: true,
Alien: true,
Name: name,
}
matter = this.matterDao.Create(matter)
}
puuid = matter.Uuid
}
return puuid
}
//获取某个文件的详情,会把父级依次倒着装进去。如果中途出错,直接抛出异常。
func (this *MatterService) Detail(uuid string) *Matter {
matter := this.matterDao.CheckByUuid(uuid)
//组装file的内容,展示其父组件。
puuid := matter.Puuid
tmpMatter := matter
for puuid != "root" {
pFile := this.matterDao.CheckByUuid(puuid)
tmpMatter.Parent = pFile
tmpMatter = pFile
puuid = pFile.Puuid
}
return matter
}
//开始上传文件
//上传文件. alien表明文件是否是应用使用的文件。
func (this *MatterService) Upload(file multipart.File, user *User, puuid string, filename string, privacy bool, alien bool) *Matter {
//文件名不能太长。
if len(filename) > 200 {
panic("文件名不能超过200")
}
//获取文件应该存放在的物理路径的绝对路径和相对路径。
absolutePath, relativePath := GetUserFilePath(user.Username)
absolutePath = absolutePath + "/" + filename
relativePath = relativePath + "/" + filename
distFile, err := os.OpenFile(absolutePath, os.O_WRONLY|os.O_CREATE, 0777)
this.PanicError(err)
defer distFile.Close()
written, err := io.Copy(distFile, file)
this.PanicError(err)
//判断用户自身上传大小的限制。
if user.SizeLimit >= 0 {
if written > user.SizeLimit {
panic("您最大只能上传" + HumanFileSize(user.SizeLimit) + "的文件")
}
}
//查找文件夹下面是否有同名文件。
matters := this.matterDao.ListByUserUuidAndPuuidAndDirAndName(user.Uuid, puuid, false, filename)
//如果有同名的文件,那么我们直接覆盖同名文件。
for _, dbFile := range matters {
this.matterDao.Delete(dbFile)
}
//将文件信息存入数据库中。
matter := &Matter{
Puuid: puuid,
UserUuid: user.Uuid,
Dir: false,
Alien: alien,
Name: filename,
Md5: "",
Size: written,
Privacy: privacy,
Path: relativePath,
}
matter = this.matterDao.Create(matter)
return matter
}
// 从指定的url下载一个文件。参考:https://golangcode.com/download-a-file-from-a-url/
func (this *MatterService) httpDownloadFile(filepath string, url string) (int64, error) {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return 0, err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return 0, err
}
defer resp.Body.Close()
// Write the body to file
size, err := io.Copy(out, resp.Body)
if err != nil {
return 0, err
}
return size, nil
}
//去指定的url中爬文件
func (this *MatterService) Crawl(url string, filename string, user *User, puuid string, privacy bool) *Matter {
//文件名不能太长。
if len(filename) > 200 {
panic("文件名不能超过200")
}
//获取文件应该存放在的物理路径的绝对路径和相对路径。
absolutePath, relativePath := GetUserFilePath(user.Username)
absolutePath = absolutePath + "/" + filename
relativePath = relativePath + "/" + filename
//使用临时文件存放
fmt.Printf("存放于%s", absolutePath)
size, err := this.httpDownloadFile(absolutePath, url)
this.PanicError(err)
//判断用户自身上传大小的限制。
if user.SizeLimit >= 0 {
if size > user.SizeLimit {
panic("您最大只能上传" + HumanFileSize(user.SizeLimit) + "的文件")
}
}
//查找文件夹下面是否有同名文件。
matters := this.matterDao.ListByUserUuidAndPuuidAndDirAndName(user.Uuid, puuid, false, filename)
//如果有同名的文件,那么我们直接覆盖同名文件。
for _, dbFile := range matters {
this.matterDao.Delete(dbFile)
}
//将文件信息存入数据库中。
matter := &Matter{
Puuid: puuid,
UserUuid: user.Uuid,
Dir: false,
Alien: false,
Name: filename,
Md5: "",
Size: size,
Privacy: privacy,
Path: relativePath,
}
matter = this.matterDao.Create(matter)
return matter
}
//图片预处理功能。
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)
mimeType := GetMimeType(fileName)
writer.Header().Set("Content-Type", mimeType)
//当前的文件是否是图片,只有图片才能处理。
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")
}
}
}
// 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:文件修改过。
func (this *MatterService) checkLastModified(w http.ResponseWriter, r *http.Request, modifyTime time.Time) bool {
if modifyTime.IsZero() {
return false
}
// The Date-Modified header truncates sub-second precision, so
// use mtime < t+1s instead of mtime <= t to check for unmodified.
if t, err := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && modifyTime.Before(t.Add(1*time.Second)) {
h := w.Header()
delete(h, "Content-Type")
delete(h, "Content-Length")
w.WriteHeader(http.StatusNotModified)
return true
}
w.Header().Set("Last-Modified", modifyTime.UTC().Format(http.TimeFormat))
return false
}
// 处理ETag标签
// checkETag implements If-None-Match and If-Range checks.
//
// The ETag or modtime must have been previously set in the
// ResponseWriter's headers. The modtime is only compared at second
// granularity and may be the zero value to mean unknown.
//
// The return value is the effective request "Range" header to use and
// whether this request is now considered done.
func (this *MatterService) checkETag(w http.ResponseWriter, r *http.Request, modtime time.Time) (rangeReq string, done bool) {
etag := w.Header().Get("Etag")
rangeReq = r.Header.Get("Range")
// Invalidate the range request if the entity doesn't match the one
// the client was expecting.
// "If-Range: version" means "ignore the Range: header unless version matches the
// current file."
// We only support ETag versions.
// The caller must have set the ETag on the response already.
if ir := r.Header.Get("If-Range"); ir != "" && ir != etag {
// The If-Range value is typically the ETag value, but it may also be
// the modtime date. See golang.org/issue/8367.
timeMatches := false
if !modtime.IsZero() {
if t, err := http.ParseTime(ir); err == nil && t.Unix() == modtime.Unix() {
timeMatches = true
}
}
if !timeMatches {
rangeReq = ""
}
}
if inm := r.Header.Get("If-None-Match"); inm != "" {
// Must know ETag.
if etag == "" {
return rangeReq, false
}
// (bradfitz): non-GET/HEAD requests require more work:
// sending a different status code on matches, and
// also can't use weak cache validators (those with a "W/
// prefix). But most users of ServeContent will be using
// it on GET or HEAD, so only support those for now.
if r.Method != "GET" && r.Method != "HEAD" {
return rangeReq, false
}
// (bradfitz): deal with comma-separated or multiple-valued
// list of If-None-match values. For now just handle the common
// case of a single item.
if inm == etag || inm == "*" {
h := w.Header()
delete(h, "Content-Type")
delete(h, "Content-Length")
w.WriteHeader(http.StatusNotModified)
return "", true
}
}
return rangeReq, false
}
// parseRange parses a Range header string as per RFC 2616.
func (this *MatterService) parseRange(s string, size int64) ([]httpRange, error) {
if s == "" {
return nil, nil // header not present
}
const b = "bytes="
if !strings.HasPrefix(s, b) {
return nil, errors.New("invalid range")
}
var ranges []httpRange
for _, ra := range strings.Split(s[len(b):], ",") {
ra = strings.TrimSpace(ra)
if ra == "" {
continue
}
i := strings.Index(ra, "-")
if i < 0 {
return nil, errors.New("invalid range")
}
start, end := strings.TrimSpace(ra[:i]), strings.TrimSpace(ra[i+1:])
var r httpRange
if start == "" {
// If no start is specified, end specifies the
// range start relative to the end of the file.
i, err := strconv.ParseInt(end, 10, 64)
if err != nil {
return nil, errors.New("invalid range")
}
if i > size {
i = size
}
r.start = size - i
r.length = size - r.start
} else {
i, err := strconv.ParseInt(start, 10, 64)
if err != nil || i >= size || i < 0 {
return nil, errors.New("invalid range")
}
r.start = i
if end == "" {
// If no end is specified, range extends to end of the file.
r.length = size - r.start
} else {
i, err := strconv.ParseInt(end, 10, 64)
if err != nil || r.start > i {
return nil, errors.New("invalid range")
}
if i >= size {
i = size - 1
}
r.length = i - r.start + 1
}
}
ranges = append(ranges, r)
}
return ranges, nil
}
// rangesMIMESize returns the number of bytes it takes to encode the
// provided ranges as a multipart response.
func (this *MatterService) rangesMIMESize(ranges []httpRange, contentType string, contentSize int64) (encSize int64) {
var w countingWriter
mw := multipart.NewWriter(&w)
for _, ra := range ranges {
mw.CreatePart(ra.mimeHeader(contentType, contentSize))
encSize += ra.length
}
mw.Close()
encSize += int64(w)
return
}
func (this *MatterService) sumRangesSize(ranges []httpRange) (size int64) {
for _, ra := range ranges {
size += ra.length
}
return
}
//文件下载功能。
//下载功能参考:https://github.com/Masterminds/go-fileserver
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 := diskFile.Stat()
if err != nil {
this.PanicWebError(err.Error(), http.StatusInternalServerError)
}
modifyTime := fileInfo.ModTime()
if this.checkLastModified(writer, request, modifyTime) {
return
}
rangeReq, done := this.checkETag(writer, request, modifyTime)
if done {
return
}
code := http.StatusOK
// From net/http/sniff.go
// The algorithm uses at most sniffLen bytes to make its decision.
const sniffLen = 512
// 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.
ctypes, haveType := writer.Header()["Content-Type"]
var ctype string
if !haveType {
ctype = mime.TypeByExtension(filepath.Ext(fileInfo.Name()))
if ctype == "" {
// read a chunk to decide between utf-8 text and binary
var buf [sniffLen]byte
n, _ := io.ReadFull(diskFile, buf[:])
ctype = http.DetectContentType(buf[:n])
_, err := diskFile.Seek(0, os.SEEK_SET) // rewind to output whole file
if err != nil {
this.PanicWebError("无法准确定位文件", http.StatusInternalServerError)
return
}
}
writer.Header().Set("Content-Type", ctype)
} else if len(ctypes) > 0 {
ctype = ctypes[0]
}
size := fileInfo.Size()
// handle Content-Range header.
sendSize := size
var sendContent io.Reader = diskFile
if size >= 0 {
ranges, err := this.parseRange(rangeReq, size)
if err != nil {
panic("range header出错")
this.PanicWebError("range header error", http.StatusRequestedRangeNotSatisfiable)
return
}
if this.sumRangesSize(ranges) > size {
// The total number of bytes in all the ranges
// is larger than the size of the file by
// itself, so this is probably an attack, or a
// dumb client. Ignore the range request.
ranges = nil
}
switch {
case len(ranges) == 1:
// RFC 2616, Section 14.16:
// "When an HTTP message includes the content of a single
// range (for example, a response to a request for a
// single range, or to a request for a set of ranges
// that overlap without any holes), this content is
// transmitted with a Content-Range header, and a
// Content-Length header showing the number of bytes
// actually transferred.
// ...
// A response to a request for a single range MUST NOT
// be sent using the multipart/byteranges media type."
ra := ranges[0]
if _, err := diskFile.Seek(ra.start, io.SeekStart); err != nil {
this.PanicWebError(err.Error(), http.StatusRequestedRangeNotSatisfiable)
return
}
sendSize = ra.length
code = http.StatusPartialContent
writer.Header().Set("Content-Range", ra.contentRange(size))
case len(ranges) > 1:
sendSize = this.rangesMIMESize(ranges, ctype, size)
code = http.StatusPartialContent
pr, pw := io.Pipe()
mw := multipart.NewWriter(pw)
writer.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
sendContent = pr
defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish.
go func() {
for _, ra := range ranges {
part, err := mw.CreatePart(ra.mimeHeader(ctype, size))
if err != nil {
pw.CloseWithError(err)
return
}
if _, err := diskFile.Seek(ra.start, io.SeekStart); err != nil {
pw.CloseWithError(err)
return
}
if _, err := io.CopyN(part, diskFile, ra.length); err != nil {
pw.CloseWithError(err)
return
}
}
mw.Close()
pw.Close()
}()
}
writer.Header().Set("Accept-Ranges", "bytes")
if writer.Header().Get("Content-Encoding") == "" {
writer.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
}
}
writer.WriteHeader(code)
if request.Method != "HEAD" {
io.CopyN(writer, sendContent, sendSize)
}
}
1
https://gitee.com/eyebluecn/tank.git
git@gitee.com:eyebluecn/tank.git
eyebluecn
tank
tank
v1.0.5

搜索帮助