diff --git a/code/rest/alien_service.go b/code/rest/alien_service.go index 968ca2a..2624b0c 100644 --- a/code/rest/alien_service.go +++ b/code/rest/alien_service.go @@ -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 { diff --git a/code/rest/matter_controller.go b/code/rest/matter_controller.go index 0d93d23..f7751d9 100644 --- a/code/rest/matter_controller.go +++ b/code/rest/matter_controller.go @@ -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) } diff --git a/code/rest/matter_dao.go b/code/rest/matter_dao.go index 3ec3e25..9d5c615 100644 --- a/code/rest/matter_dao.go +++ b/code/rest/matter_dao.go @@ -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{} diff --git a/code/rest/matter_service.go b/code/rest/matter_service.go index 272271e..672f2ac 100644 --- a/code/rest/matter_service.go +++ b/code/rest/matter_service.go @@ -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) diff --git a/code/rest/preference_controller.go b/code/rest/preference_controller.go index 1cb6c6f..d25431e 100644 --- a/code/rest/preference_controller.go +++ b/code/rest/preference_controller.go @@ -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) diff --git a/code/rest/preference_dao.go b/code/rest/preference_dao.go index 5e0e5be..b177bc0 100644 --- a/code/rest/preference_dao.go +++ b/code/rest/preference_dao.go @@ -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 { diff --git a/code/rest/preference_model.go b/code/rest/preference_model.go index 55c2468..2645c62 100644 --- a/code/rest/preference_model.go +++ b/code/rest/preference_model.go @@ -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` diff --git a/code/tool/util/util.zip.go b/code/tool/util/util_zip.go similarity index 88% rename from code/tool/util/util.zip.go rename to code/tool/util/util_zip.go index 66153b6..80bfbc5 100644 --- a/code/tool/util/util.zip.go +++ b/code/tool/util/util_zip.go @@ -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)