Skip to content

Commit

Permalink
Finish the first step of i18n.
Browse files Browse the repository at this point in the history
  • Loading branch information
zicla committed May 4, 2019
1 parent 2b68733 commit 900924d
Show file tree
Hide file tree
Showing 19 changed files with 268 additions and 207 deletions.
19 changes: 10 additions & 9 deletions code/rest/alien_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rest
import (
"fmt"
"github.com/eyebluecn/tank/code/core"
"github.com/eyebluecn/tank/code/tool/i18n"
"github.com/eyebluecn/tank/code/tool/result"
"github.com/eyebluecn/tank/code/tool/util"
"net/http"
Expand Down Expand Up @@ -120,8 +121,8 @@ func (this *AlienController) FetchUploadToken(writer http.ResponseWriter, reques
filename := request.FormValue("filename")
if filename == "" {
panic("文件名必填")
} else if m, _ := regexp.MatchString(`[<>|*?/\\]`, filename); m {
panic(fmt.Sprintf(`【%s】不符合要求,文件名中不能包含以下特殊符号:< > | * ? / \`, filename))
} else if m, _ := regexp.MatchString(MATTER_NAME_PATTERN, filename); m {
panic(result.BadRequestI18n(request, i18n.MatterNameContainSpecialChars))
}

//什么时间后过期,默认24h
Expand Down Expand Up @@ -175,7 +176,7 @@ func (this *AlienController) FetchUploadToken(writer http.ResponseWriter, reques
dir := request.FormValue("dir")

user := this.checkUser(request)
dirMatter := this.matterService.CreateDirectories(user, dir)
dirMatter := this.matterService.CreateDirectories(request, user, dir)

mm, _ := time.ParseDuration(fmt.Sprintf("%ds", expire))
uploadToken := &UploadToken{
Expand Down Expand Up @@ -257,7 +258,7 @@ func (this *AlienController) Upload(writer http.ResponseWriter, request *http.Re

dirMatter := this.matterDao.CheckWithRootByUuid(uploadToken.FolderUuid, user)

matter := this.matterService.AtomicUpload(file, user, dirMatter, uploadToken.Filename, uploadToken.Privacy)
matter := this.matterService.AtomicUpload(request, file, user, dirMatter, uploadToken.Filename, uploadToken.Privacy)

//更新这个uploadToken的信息.
uploadToken.ExpireTime = time.Now()
Expand Down Expand Up @@ -294,7 +295,7 @@ func (this *AlienController) CrawlToken(writer http.ResponseWriter, request *htt

dirMatter := this.matterDao.CheckWithRootByUuid(uploadToken.FolderUuid, user)

matter := this.matterService.AtomicCrawl(url, uploadToken.Filename, user, dirMatter, uploadToken.Privacy)
matter := this.matterService.AtomicCrawl(request, url, uploadToken.Filename, user, dirMatter, uploadToken.Privacy)

//更新这个uploadToken的信息.
uploadToken.ExpireTime = time.Now()
Expand All @@ -316,8 +317,8 @@ func (this *AlienController) CrawlDirect(writer http.ResponseWriter, request *ht

if filename == "" {
panic("文件名必填")
} else if m, _ := regexp.MatchString(`[<>|*?/\\]`, filename); m {
panic(fmt.Sprintf(`【%s】不符合要求,文件名中不能包含以下特殊符号:< > | * ? / \`, filename))
} else if m, _ := regexp.MatchString(MATTER_NAME_PATTERN, filename); m {
panic(result.BadRequestI18n(request, i18n.MatterNameContainSpecialChars))
}

var privacy bool
Expand All @@ -334,9 +335,9 @@ func (this *AlienController) CrawlDirect(writer http.ResponseWriter, request *ht
}

user := this.checkUser(request)
dirMatter := this.matterService.CreateDirectories(user, dir)
dirMatter := this.matterService.CreateDirectories(request, user, dir)

matter := this.matterService.AtomicCrawl(url, filename, user, dirMatter, privacy)
matter := this.matterService.AtomicCrawl(request, url, filename, user, dirMatter, privacy)

return this.Success(matter)
}
Expand Down
7 changes: 4 additions & 3 deletions code/rest/dav_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"github.com/eyebluecn/tank/code/core"
"github.com/eyebluecn/tank/code/tool/i18n"
"github.com/eyebluecn/tank/code/tool/result"
"github.com/eyebluecn/tank/code/tool/util"
"io/ioutil"
Expand Down Expand Up @@ -84,10 +85,10 @@ func (this *DavController) CheckCurrentUser(writer http.ResponseWriter, request

user := this.userDao.FindByUsername(username)
if user == nil {
panic(result.BadRequest("用户名或密码错误"))
panic(result.BadRequestI18n(request, i18n.UsernameOrPasswordError))
} else {
if !util.MatchBcrypt(password, user.Password) {
panic(result.BadRequest("用户名或密码错误"))
panic(result.BadRequestI18n(request, i18n.UsernameOrPasswordError))
}
}

Expand All @@ -108,7 +109,7 @@ func (this *DavController) HandleRoutes(writer http.ResponseWriter, request *htt
path := request.URL.Path

//匹配 /api/dav{subPath}
pattern := fmt.Sprintf(`^%s(.*)$`, WEBDAV_PREFFIX)
pattern := fmt.Sprintf(`^%s(.*)$`, WEBDAV_PREFIX)
reg := regexp.MustCompile(pattern)
strs := reg.FindStringSubmatch(path)
if len(strs) == 2 {
Expand Down
2 changes: 1 addition & 1 deletion code/rest/dav_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

//访问前缀,这个是特殊入口
var WEBDAV_PREFFIX = "/api/dav"
var WEBDAV_PREFIX = "/api/dav"

//动态的文件属性
type LiveProp struct {
Expand Down
42 changes: 21 additions & 21 deletions code/rest/dav_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (this *DavService) ParseDepth(request *http.Request) int {
return 1
}
} else {
panic(result.BadRequest("必须指定Header Depth"))
panic(result.BadRequest("Header Depth cannot be null"))
}
return depth
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func (this *DavService) PropstatsFromXmlNames(user *User, matter *Matter, xmlNam
}

if len(properties) == 0 {
panic(result.BadRequest("请求的属性项无法解析!"))
panic(result.BadRequest("cannot parse request properties"))
}

okPropstat := dav.Propstat{Status: http.StatusOK, Props: properties}
Expand Down Expand Up @@ -137,7 +137,7 @@ func (this *DavService) Propstats(user *User, matter *Matter, propfind *dav.Prop

propstats := make([]dav.Propstat, 0)
if propfind.Propname != nil {
panic(result.BadRequest("propfind.Propname != nil 尚未处理"))
panic(result.BadRequest("TODO: propfind.Propname != nil "))
} else if propfind.Allprop != nil {

//TODO: 如果include中还有内容,那么包含进去。
Expand Down Expand Up @@ -187,7 +187,7 @@ func (this *DavService) HandlePropfind(writer http.ResponseWriter, request *http
fmt.Printf("处理Matter %s\n", matter.Path)

propstats := this.Propstats(user, matter, propfind)
path := fmt.Sprintf("%s%s", WEBDAV_PREFFIX, matter.Path)
path := fmt.Sprintf("%s%s", WEBDAV_PREFIX, matter.Path)
response := this.makePropstatResponse(path, propstats)

err := multiStatusWriter.Write(response)
Expand Down Expand Up @@ -234,10 +234,10 @@ func (this *DavService) HandlePut(writer http.ResponseWriter, request *http.Requ
//如果存在,那么先删除再说。
srcMatter := this.matterDao.findByUserUuidAndPath(user.Uuid, subPath)
if srcMatter != nil {
this.matterService.AtomicDelete(srcMatter)
this.matterService.AtomicDelete(request, srcMatter)
}

this.matterService.Upload(request.Body, user, dirMatter, filename, true)
this.matterService.Upload(request, request.Body, user, dirMatter, filename, true)

}

Expand All @@ -249,7 +249,7 @@ func (this *DavService) HandleDelete(writer http.ResponseWriter, request *http.R
//寻找符合条件的matter.
matter := this.matterDao.CheckWithRootByPath(subPath, user)

this.matterService.AtomicDelete(matter)
this.matterService.AtomicDelete(request, matter)
}

//创建文件夹
Expand All @@ -263,7 +263,7 @@ func (this *DavService) HandleMkcol(writer http.ResponseWriter, request *http.Re
//寻找符合条件的matter.
dirMatter := this.matterDao.CheckWithRootByPath(dirPath, user)

this.matterService.AtomicCreateDirectory(dirMatter, thisDirName, user)
this.matterService.AtomicCreateDirectory(request, dirMatter, thisDirName, user)

}

Expand Down Expand Up @@ -314,17 +314,17 @@ func (this *DavService) prepareMoveCopy(
var destinationPath string

if destinationStr == "" {
panic(result.BadRequest("Header Destination必填"))
panic(result.BadRequest("Header Destination cannot be null"))
}

//如果是重命名,那么就不是http开头了。
if strings.HasPrefix(destinationStr, WEBDAV_PREFFIX) {
if strings.HasPrefix(destinationStr, WEBDAV_PREFIX) {
fullDestinationPath = destinationStr
} else {
destinationUrl, err := url.Parse(destinationStr)
this.PanicError(err)
if destinationUrl.Host != request.Host {
panic(result.BadRequest("Destination Host不一致. %s %s != %s", destinationStr, destinationUrl.Host, request.Host))
panic(result.BadRequest("Destination Host not the same. %s %s != %s", destinationStr, destinationUrl.Host, request.Host))
}
fullDestinationPath = destinationUrl.Path
}
Expand All @@ -333,13 +333,13 @@ func (this *DavService) prepareMoveCopy(
fullDestinationPath = path.Clean(fullDestinationPath)

//去除前缀
pattern := fmt.Sprintf(`^%s(.*)$`, WEBDAV_PREFFIX)
pattern := fmt.Sprintf(`^%s(.*)$`, WEBDAV_PREFIX)
reg := regexp.MustCompile(pattern)
strs := reg.FindStringSubmatch(fullDestinationPath)
if len(strs) == 2 {
destinationPath = strs[1]
} else {
panic(result.BadRequest("目标前缀必须为:%s", WEBDAV_PREFFIX))
panic(result.BadRequest("destination prefix must be %s", WEBDAV_PREFIX))
}

destinationName = util.GetFilenameOfPath(destinationPath)
Expand All @@ -362,7 +362,7 @@ func (this *DavService) prepareMoveCopy(

//如果是空或者/就是请求根目录
if srcMatter.Uuid == MATTER_ROOT {
panic(result.BadRequest("你不能移动根目录!"))
panic(result.BadRequest("you cannot move the root directory"))
}

//寻找目标文件夹matter
Expand All @@ -381,9 +381,9 @@ func (this *DavService) HandleMove(writer http.ResponseWriter, request *http.Req
//移动到新目录中去。
if destinationDirPath == srcDirPath {
//文件夹没变化,相当于重命名。
this.matterService.AtomicRename(srcMatter, destinationName, user)
this.matterService.AtomicRename(request, srcMatter, destinationName, user)
} else {
this.matterService.AtomicMove(srcMatter, destDirMatter, overwrite)
this.matterService.AtomicMove(request, srcMatter, destDirMatter, overwrite)
}

this.logger.Info("完成移动 %s => %s", subPath, destDirMatter.Path)
Expand All @@ -397,7 +397,7 @@ func (this *DavService) HandleCopy(writer http.ResponseWriter, request *http.Req
srcMatter, destDirMatter, _, _, destinationName, overwrite := this.prepareMoveCopy(writer, request, user, subPath)

//复制到新目录中去。
this.matterService.AtomicCopy(srcMatter, destDirMatter, destinationName, overwrite)
this.matterService.AtomicCopy(request, srcMatter, destDirMatter, destinationName, overwrite)

this.logger.Info("完成复制 %s => %s", subPath, destDirMatter.Path)

Expand All @@ -406,19 +406,19 @@ func (this *DavService) HandleCopy(writer http.ResponseWriter, request *http.Req
//加锁
func (this *DavService) HandleLock(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {

panic(result.BadRequest("不支持LOCK方法"))
panic(result.BadRequest("not support LOCK yet."))
}

//解锁
func (this *DavService) HandleUnlock(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {

panic(result.BadRequest("不支持UNLOCK方法"))
panic(result.BadRequest("not support UNLOCK yet."))
}

//修改文件属性
func (this *DavService) HandleProppatch(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {

panic(result.BadRequest("不支持PROPPATCH方法"))
panic(result.BadRequest("not support PROPPATCH yet."))
}

//处理所有的请求
Expand Down Expand Up @@ -482,7 +482,7 @@ func (this *DavService) HandleDav(writer http.ResponseWriter, request *http.Requ

} else {

panic(result.BadRequest("该方法还不支持。%s", method))
panic(result.BadRequest("not support %s yet.", method))

}

Expand Down
6 changes: 3 additions & 3 deletions code/rest/image_cache_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (this *ImageCacheController) Detail(writer http.ResponseWriter, request *ht

uuid := request.FormValue("uuid")
if uuid == "" {
panic(result.BadRequest("图片缓存的uuid必填"))
panic(result.BadRequest("uuid cannot be null"))
}

imageCache := this.imageCacheService.Detail(uuid)
Expand Down Expand Up @@ -125,7 +125,7 @@ func (this *ImageCacheController) Delete(writer http.ResponseWriter, request *ht

uuid := request.FormValue("uuid")
if uuid == "" {
panic(result.BadRequest("图片缓存的uuid必填"))
panic(result.BadRequest("uuid cannot be null"))
}

imageCache := this.imageCacheDao.FindByUuid(uuid)
Expand All @@ -147,7 +147,7 @@ func (this *ImageCacheController) DeleteBatch(writer http.ResponseWriter, reques

uuids := request.FormValue("uuids")
if uuids == "" {
panic(result.BadRequest("图片缓存的uuids必填"))
panic(result.BadRequest("uuids cannot be null"))
}

uuidArray := strings.Split(uuids, ",")
Expand Down
23 changes: 12 additions & 11 deletions code/rest/install_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/eyebluecn/tank/code/core"
"github.com/eyebluecn/tank/code/tool/builder"
"github.com/eyebluecn/tank/code/tool/i18n"
"github.com/eyebluecn/tank/code/tool/result"
"github.com/eyebluecn/tank/code/tool/util"
"github.com/jinzhu/gorm"
Expand Down Expand Up @@ -226,10 +227,10 @@ func (this *InstallController) validateTableMetaList(tableInfoList []*InstallTab
strs = append(strs, v.DBName)
}

panic(result.BadRequest(fmt.Sprintf("%s 表的以下字段缺失:%v", tableInfo.Name, strs)))
panic(result.BadRequest(fmt.Sprintf("table %s miss the following fields %v", tableInfo.Name, strs)))
}
} else {
panic(result.BadRequest(tableInfo.Name + "表不存在"))
panic(result.BadRequest(tableInfo.Name + " table not exist"))
}
}

Expand Down Expand Up @@ -314,19 +315,19 @@ func (this *InstallController) CreateAdmin(writer http.ResponseWriter, request *

//验证超级管理员的信息
if m, _ := regexp.MatchString(`^[0-9a-zA-Z_]+$`, adminUsername); !m {
panic(result.BadRequest(`超级管理员用户名必填,且只能包含字母,数字和'_''`))
panic(result.BadRequest(`admin's username cannot oly be alphabet, number or '_'`))
}

if len(adminPassword) < 6 {
panic(result.BadRequest(`超级管理员密码长度至少为6位`))
panic(result.BadRequest(`admin's password at least 6 chars'`))
}

//检查是否有重复。
var count2 int64
db2 := db.Model(&User{}).Where("username = ?", adminUsername).Count(&count2)
this.PanicError(db2.Error)
if count2 > 0 {
panic(result.BadRequest(`%s该用户名已存在`, adminUsername))
panic(result.BadRequestI18n(request, i18n.UsernameExist, adminUsername))
}

user := &User{}
Expand Down Expand Up @@ -360,24 +361,24 @@ func (this *InstallController) ValidateAdmin(writer http.ResponseWriter, request

//验证超级管理员的信息
if adminUsername == "" {
panic(result.BadRequest(`超级管理员用户名必填`))
panic(result.BadRequest(`admin's username cannot be null'`))
}
if len(adminPassword) < 6 {
panic(result.BadRequest(`超级管理员密码长度至少为6位`))
panic(result.BadRequest(`admin's password at least 6 chars'`))
}

var existUsernameUser = &User{}
db = db.Where(&User{Username: adminUsername}).First(existUsernameUser)
if db.Error != nil {
panic(result.BadRequest(fmt.Sprintf("%s对应的用户不存在", adminUsername)))
panic(result.BadRequestI18n(request, i18n.UsernameNotExist, adminUsername))
}

if !util.MatchBcrypt(adminPassword, existUsernameUser.Password) {
panic(result.BadRequest("用户名或密码错误"))
panic(result.BadRequestI18n(request, i18n.UsernameOrPasswordError, adminUsername))
}

if existUsernameUser.Role != USER_ROLE_ADMINISTRATOR {
panic(result.BadRequest("该账号不是管理员"))
panic(result.BadRequestI18n(request, i18n.UsernameIsNotAdmin, adminUsername))
}

return this.Success("OK")
Expand Down Expand Up @@ -413,7 +414,7 @@ func (this *InstallController) Finish(writer http.ResponseWriter, request *http.
db1 := db.Model(&User{}).Where("role = ?", USER_ROLE_ADMINISTRATOR).Count(&count1)
this.PanicError(db1.Error)
if count1 == 0 {
panic(result.BadRequest(`请至少配置一名管理员`))
panic(result.BadRequest(`please config at least one admin user`))
}

//通知配置文件安装完毕。
Expand Down
Loading

0 comments on commit 900924d

Please sign in to comment.