Add the download dir max size in the preference.

This commit is contained in:
zicla 2019-04-29 01:05:41 +08:00
parent 177ee3a6fb
commit 120d9a55c8
8 changed files with 44 additions and 42 deletions

View File

@ -118,7 +118,7 @@ func (this *AlienService) PreviewOrDownload(
this.logger.Info("准备下载文件夹 %s", matter.Name)
//目标地点
this.matterService.AtomicDownloadDirectory(writer, request, matter)
this.matterService.DownloadDirectory(writer, request, matter)
} else {

View File

@ -109,7 +109,6 @@ func (this *MatterController) Page(writer http.ResponseWriter, request *http.Req
userUuid := request.FormValue("userUuid")
name := request.FormValue("name")
dir := request.FormValue("dir")
alien := request.FormValue("alien")
orderDir := request.FormValue("orderDir")
orderSize := request.FormValue("orderSize")
orderName := request.FormValue("orderName")
@ -170,7 +169,7 @@ func (this *MatterController) Page(writer http.ResponseWriter, request *http.Req
},
}
pager := this.matterDao.Page(page, pageSize, puuid, userUuid, name, dir, alien, extensions, sortArray)
pager := this.matterDao.Page(page, pageSize, puuid, userUuid, name, dir, extensions, sortArray)
return this.Success(pager)
}

View File

@ -221,7 +221,7 @@ func (this *MatterDao) List(puuid string, userUuid string, sortArray []builder.O
}
//获取某个文件夹下所有的文件和子文件
func (this *MatterDao) Page(page int, pageSize int, puuid string, userUuid string, name string, dir string, alien string, extensions []string, sortArray []builder.OrderPair) *Pager {
func (this *MatterDao) Page(page int, pageSize int, puuid string, userUuid string, name string, dir string, extensions []string, sortArray []builder.OrderPair) *Pager {
var wp = &builder.WherePair{}
@ -243,12 +243,6 @@ func (this *MatterDao) Page(page int, pageSize int, puuid string, userUuid strin
wp = wp.And(&builder.WherePair{Query: "dir = ?", Args: []interface{}{0}})
}
if alien == TRUE {
wp = wp.And(&builder.WherePair{Query: "alien = ?", Args: []interface{}{1}})
} else if alien == FALSE {
wp = wp.And(&builder.WherePair{Query: "alien = ?", Args: []interface{}{0}})
}
var conditionDB *gorm.DB
if extensions != nil && len(extensions) > 0 {
var orWp = &builder.WherePair{}

View File

@ -27,6 +27,7 @@ type MatterService struct {
userService *UserService
imageCacheDao *ImageCacheDao
imageCacheService *ImageCacheService
preferenceService *PreferenceService
}
//初始化方法
@ -59,6 +60,11 @@ func (this *MatterService) Init() {
this.imageCacheService = b
}
b = core.CONTEXT.GetBean(this.preferenceService)
if b, ok := b.(*PreferenceService); ok {
this.preferenceService = b
}
}
//文件下载。支持分片下载
@ -73,7 +79,7 @@ func (this *MatterService) DownloadFile(
}
//下载文件夹
func (this *MatterService) AtomicDownloadDirectory(
func (this *MatterService) DownloadDirectory(
writer http.ResponseWriter,
request *http.Request,
matter *Matter) {
@ -86,15 +92,17 @@ func (this *MatterService) AtomicDownloadDirectory(
panic(result.BadRequest("matter 只能是文件夹"))
}
//操作锁
this.userService.MatterLock(matter.UserUuid)
defer this.userService.MatterUnlock(matter.UserUuid)
//验证文件夹中文件总大小。
sumSize := this.matterDao.SumSizeByUserUuidAndPath(matter.UserUuid, matter.Path)
this.logger.Info("文件夹 %s 大小为 %s", matter.Name, util.HumanFileSize(sumSize))
//TODO: 文件夹下载的大小限制
//判断用户自身上传大小的限制
preference := this.preferenceService.Fetch()
if preference.DownloadDirMaxSize >= 0 {
if sumSize > preference.DownloadDirMaxSize {
panic(result.BadRequest("文件夹大小%s超出下载限制%s ", util.HumanFileSize(sumSize), util.HumanFileSize(preference.DownloadDirMaxSize)))
}
}
//准备zip放置的目录。
destZipDirPath := fmt.Sprintf("%s/%d", GetUserZipRootDir(matter.Username), time.Now().UnixNano()/1e6)

View File

@ -5,6 +5,7 @@ import (
"github.com/eyebluecn/tank/code/tool/result"
"github.com/eyebluecn/tank/code/tool/util"
"net/http"
"strconv"
)
type PreferenceController struct {
@ -39,7 +40,7 @@ func (this *PreferenceController) RegisterRoutes() map[string]func(writer http.R
routeMap["/api/preference/ping"] = this.Wrap(this.Ping, USER_ROLE_GUEST)
routeMap["/api/preference/fetch"] = this.Wrap(this.Fetch, USER_ROLE_GUEST)
routeMap["/api/preference/edit"] = this.Wrap(this.Edit, USER_ROLE_ADMINISTRATOR)
routeMap["/api/preference/system_cleanup"] = this.Wrap(this.SystemCleanup, USER_ROLE_ADMINISTRATOR)
routeMap["/api/preference/system/cleanup"] = this.Wrap(this.SystemCleanup, USER_ROLE_ADMINISTRATOR)
return routeMap
}
@ -70,21 +71,27 @@ func (this *PreferenceController) Edit(writer http.ResponseWriter, request *http
logoUrl := request.FormValue("logoUrl")
faviconUrl := request.FormValue("faviconUrl")
footerLine1 := request.FormValue("footerLine1")
footerLine2 := request.FormValue("footerLine2")
showAlienStr := request.FormValue("showAlien")
copyright := request.FormValue("copyright")
record := request.FormValue("record")
downloadDirMaxSizeStr := request.FormValue("downloadDirMaxSize")
var downloadDirMaxSize int64 = 0
if downloadDirMaxSizeStr == "" {
panic("文件下载最大限制必填!")
} else {
intDownloadDirMaxSize, err := strconv.Atoi(downloadDirMaxSizeStr)
this.PanicError(err)
downloadDirMaxSize = int64(intDownloadDirMaxSize)
}
preference := this.preferenceDao.Fetch()
preference.Name = name
preference.LogoUrl = logoUrl
preference.FaviconUrl = faviconUrl
preference.FooterLine1 = footerLine1
preference.FooterLine2 = footerLine2
if showAlienStr == TRUE {
preference.ShowAlien = true
} else if showAlienStr == FALSE {
preference.ShowAlien = false
}
preference.Copyright = copyright
preference.Record = record
preference.DownloadDirMaxSize = downloadDirMaxSize
preference = this.preferenceDao.Save(preference)

View File

@ -21,7 +21,7 @@ func (this *PreferenceDao) Fetch() *Preference {
if db.Error.Error() == result.DB_ERROR_NOT_FOUND {
preference.Name = "蓝眼云盘"
preference.ShowAlien = true
this.Create(preference)
return preference
} else {

View File

@ -4,12 +4,12 @@ import "github.com/eyebluecn/tank/code/core"
type Preference struct {
Base
Name string `json:"name" gorm:"type:varchar(45)"`
LogoUrl string `json:"logoUrl" gorm:"type:varchar(255)"`
FaviconUrl string `json:"faviconUrl" gorm:"type:varchar(255)"`
FooterLine1 string `json:"footerLine1" gorm:"type:varchar(1024)"`
FooterLine2 string `json:"footerLine2" gorm:"type:varchar(1024)"`
ShowAlien bool `json:"showAlien" gorm:"type:tinyint(1) not null;default:1"`
Name string `json:"name" gorm:"type:varchar(45)"`
LogoUrl string `json:"logoUrl" gorm:"type:varchar(255)"`
FaviconUrl string `json:"faviconUrl" gorm:"type:varchar(255)"`
Copyright string `json:"copyright" gorm:"type:varchar(1024)"`
Record string `json:"record" gorm:"type:varchar(1024)"`
DownloadDirMaxSize int64 `json:"downloadDirMaxSize" gorm:"type:bigint(20) not null;default:-1"`
}
// set File's table name to be `profiles`

View File

@ -10,6 +10,7 @@ import (
"strings"
)
//将srcPath压缩到destPath。
func Zip(srcPath string, destPath string) {
if PathExists(destPath) {
@ -40,8 +41,6 @@ func Zip(srcPath string, destPath string) {
return errBack
}
//fmt.Println("遍历文件: " + path)
// 通过文件信息,创建 zip 的文件信息
fileHeader, err := zip.FileInfoHeader(fileInfo)
if err != nil {
@ -49,7 +48,7 @@ func Zip(srcPath string, destPath string) {
}
// 替换文件信息中的文件名
fileHeader.Name = strings.TrimPrefix(prefix+"/"+fileInfo.Name(), string(filepath.Separator))
fileHeader.Name = strings.TrimPrefix(prefix+"/"+fileInfo.Name(), "/")
// 目录加上/
if fileInfo.IsDir() {
@ -59,8 +58,6 @@ func Zip(srcPath string, destPath string) {
prefix = prefix + "/" + fileInfo.Name()
}
//fmt.Println("头部情况: " + fileHeader.Name)
// 写入文件信息,并返回一个 Write 结构
writer, err := zipWriter.CreateHeader(fileHeader)
if err != nil {
@ -89,9 +86,6 @@ func Zip(srcPath string, destPath string) {
return
}
// 输出压缩的内容
//fmt.Printf("成功压缩文件: %s, 共写入了 %d 个字符的数据\n", path, n)
return nil
})
PanicError(err)