From d030a60721cfe46cb7a37b99f46d36c03dcfff39 Mon Sep 17 00:00:00 2001 From: zicla Date: Fri, 26 Apr 2019 03:15:34 +0800 Subject: [PATCH] Refine the structure of project. --- code/alien_controller.go | 14 +++--- code/alien_service.go | 6 +-- code/base_controller.go | 24 ++++----- code/bean.go | 4 +- code/context.go | 6 +-- code/dashboard_controller.go | 8 +-- code/dav_controller.go | 8 +-- code/dav_service.go | 24 ++++----- code/footprint_controller.go | 12 ++--- code/image_cache_controller.go | 20 ++++---- code/install_controller.go | 48 +++++++++--------- code/matter_controller.go | 46 ++++++++--------- code/matter_dao.go | 18 +++---- code/matter_service.go | 74 ++++++++++++++-------------- code/preference_controller.go | 10 ++-- code/preference_dao.go | 4 +- code/router.go | 22 ++++----- code/{ => tool}/cache/cache.go | 0 code/tool/dav/prop.go | 12 ++--- code/{ => tool}/download/download.go | 6 +-- code/{ => tool}/result/web_result.go | 0 code/tool/util/util_path.go | 6 +-- code/user_controller.go | 48 +++++++++--------- code/user_service.go | 10 ++-- 24 files changed, 213 insertions(+), 217 deletions(-) rename code/{ => tool}/cache/cache.go (100%) rename code/{ => tool}/download/download.go (99%) rename code/{ => tool}/result/web_result.go (100%) diff --git a/code/alien_controller.go b/code/alien_controller.go index 1789dd5..5f058b3 100644 --- a/code/alien_controller.go +++ b/code/alien_controller.go @@ -5,7 +5,7 @@ import ( "net/http" "regexp" "strconv" - "tank/code/result" + result2 "tank/code/tool/result" "tank/code/tool/util" "time" @@ -139,7 +139,7 @@ func (this *AlienController) CheckRequestUser(writer http.ResponseWriter, reques } //系统中的用户x要获取一个UploadToken,用于提供给x信任的用户上传文件。 -func (this *AlienController) FetchUploadToken(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *AlienController) FetchUploadToken(writer http.ResponseWriter, request *http.Request) *result2.WebResult { //文件名。 filename := request.FormValue("filename") @@ -221,7 +221,7 @@ func (this *AlienController) FetchUploadToken(writer http.ResponseWriter, reques } //系统中的用户x 拿着某个文件的uuid来确认是否其信任的用户已经上传好了。 -func (this *AlienController) Confirm(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *AlienController) Confirm(writer http.ResponseWriter, request *http.Request) *result2.WebResult { matterUuid := request.FormValue("matterUuid") if matterUuid == "" { @@ -239,7 +239,7 @@ func (this *AlienController) Confirm(writer http.ResponseWriter, request *http.R } //系统中的用户x 信任的用户上传文件。这个接口需要支持跨域。 -func (this *AlienController) Upload(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *AlienController) Upload(writer http.ResponseWriter, request *http.Request) *result2.WebResult { //允许跨域请求。 this.allowCORS(writer) if request.Method == "OPTIONS" { @@ -292,7 +292,7 @@ func (this *AlienController) Upload(writer http.ResponseWriter, request *http.Re } //给一个指定的url,从该url中去拉取文件回来。此处采用uploadToken的模式。 -func (this *AlienController) CrawlToken(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *AlienController) CrawlToken(writer http.ResponseWriter, request *http.Request) *result2.WebResult { //允许跨域请求。 this.allowCORS(writer) if request.Method == "OPTIONS" { @@ -329,7 +329,7 @@ func (this *AlienController) CrawlToken(writer http.ResponseWriter, request *htt } //通过一个url直接上传,无需借助uploadToken. -func (this *AlienController) CrawlDirect(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *AlienController) CrawlDirect(writer http.ResponseWriter, request *http.Request) *result2.WebResult { //文件名。 filename := request.FormValue("filename") @@ -367,7 +367,7 @@ func (this *AlienController) CrawlDirect(writer http.ResponseWriter, request *ht } //系统中的用户x要获取一个DownloadToken,用于提供给x信任的用户下载文件。 -func (this *AlienController) FetchDownloadToken(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *AlienController) FetchDownloadToken(writer http.ResponseWriter, request *http.Request) *result2.WebResult { matterUuid := request.FormValue("matterUuid") if matterUuid == "" { diff --git a/code/alien_service.go b/code/alien_service.go index 53c455a..09983b9 100644 --- a/code/alien_service.go +++ b/code/alien_service.go @@ -3,7 +3,7 @@ package code import ( "fmt" "net/http" - "tank/code/result" + result2 "tank/code/tool/result" "tank/code/tool/util" "time" ) @@ -98,7 +98,7 @@ func (this *AlienService) PreviewOrDownload( tokenUser := this.userDao.CheckByUuid(downloadToken.UserUuid) if matter.UserUuid != tokenUser.Uuid { - panic(result.CODE_WRAPPER_UNAUTHORIZED) + panic(result2.CODE_WRAPPER_UNAUTHORIZED) } //下载之后立即过期掉。如果是分块下载的,必须以最终获取到完整的数据为准。 @@ -110,7 +110,7 @@ func (this *AlienService) PreviewOrDownload( //判断文件的所属人是否正确 operator := this.findUser(writer, request) if operator == nil || (operator.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != operator.Uuid) { - panic(result.CODE_WRAPPER_UNAUTHORIZED) + panic(result2.CODE_WRAPPER_UNAUTHORIZED) } } diff --git a/code/base_controller.go b/code/base_controller.go index 5357b21..77c5ad7 100644 --- a/code/base_controller.go +++ b/code/base_controller.go @@ -5,7 +5,7 @@ import ( "github.com/json-iterator/go" "go/types" "net/http" - "tank/code/result" + result2 "tank/code/tool/result" ) type IController interface { @@ -50,13 +50,13 @@ func (this *BaseController) HandleRoutes(writer http.ResponseWriter, request *ht } //需要进行登录验证的wrap包装 -func (this *BaseController) Wrap(f func(writer http.ResponseWriter, request *http.Request) *result.WebResult, qualifiedRole string) func(w http.ResponseWriter, r *http.Request) { +func (this *BaseController) Wrap(f func(writer http.ResponseWriter, request *http.Request) *result2.WebResult, qualifiedRole string) func(w http.ResponseWriter, r *http.Request) { return func(writer http.ResponseWriter, request *http.Request) { //writer和request赋值给自己。 - var webResult *result.WebResult = nil + var webResult *result2.WebResult = nil //只有游客接口不需要登录 if qualifiedRole != USER_ROLE_GUEST { @@ -64,10 +64,10 @@ func (this *BaseController) Wrap(f func(writer http.ResponseWriter, request *htt if user.Status == USER_STATUS_DISABLED { //判断用户是否被禁用。 - webResult = result.ConstWebResult(result.CODE_WRAPPER_USER_DISABLED) + webResult = result2.ConstWebResult(result2.CODE_WRAPPER_USER_DISABLED) } else { if qualifiedRole == USER_ROLE_ADMINISTRATOR && user.Role != USER_ROLE_ADMINISTRATOR { - webResult = result.ConstWebResult(result.CODE_WRAPPER_UNAUTHORIZED) + webResult = result2.ConstWebResult(result2.CODE_WRAPPER_UNAUTHORIZED) } else { webResult = f(writer, request) } @@ -87,7 +87,7 @@ func (this *BaseController) Wrap(f func(writer http.ResponseWriter, request *htt this.PanicError(err) - writer.WriteHeader(result.FetchHttpStatus(webResult.Code)) + writer.WriteHeader(result2.FetchHttpStatus(webResult.Code)) _, err = fmt.Fprintf(writer, string(b)) this.PanicError(err) @@ -100,20 +100,20 @@ func (this *BaseController) Wrap(f func(writer http.ResponseWriter, request *htt } //返回成功的结果。支持放置三种类型 1.字符串 2. WebResult对象 3.空指针 4.任意类型 -func (this *BaseController) Success(data interface{}) *result.WebResult { - var webResult *result.WebResult = nil +func (this *BaseController) Success(data interface{}) *result2.WebResult { + var webResult *result2.WebResult = nil if value, ok := data.(string); ok { //返回一句普通的消息 - webResult = &result.WebResult{Code: result.CODE_WRAPPER_OK.Code, Msg: value} - } else if value, ok := data.(*result.WebResult); ok { + webResult = &result2.WebResult{Code: result2.CODE_WRAPPER_OK.Code, Msg: value} + } else if value, ok := data.(*result2.WebResult); ok { //返回一个webResult对象 webResult = value } else if _, ok := data.(types.Nil); ok { //返回一个空指针 - webResult = result.ConstWebResult(result.CODE_WRAPPER_OK) + webResult = result2.ConstWebResult(result2.CODE_WRAPPER_OK) } else { //返回的类型不明确。 - webResult = &result.WebResult{Code: result.CODE_WRAPPER_OK.Code, Data: data} + webResult = &result2.WebResult{Code: result2.CODE_WRAPPER_OK.Code, Data: data} } return webResult } diff --git a/code/bean.go b/code/bean.go index 25f4869..5d77b2e 100644 --- a/code/bean.go +++ b/code/bean.go @@ -4,7 +4,7 @@ import ( "net/http" "tank/code/config" "tank/code/logger" - "tank/code/result" + result2 "tank/code/tool/result" "tank/code/tool/util" ) @@ -78,7 +78,7 @@ func (this *Bean) findUser(writer http.ResponseWriter, request *http.Request) *U //获取当前登录的用户,找不到就返回登录错误 func (this *Bean) checkUser(writer http.ResponseWriter, request *http.Request) *User { if this.findUser(writer, request) == nil { - panic(result.ConstWebResult(result.CODE_WRAPPER_LOGIN)) + panic(result2.ConstWebResult(result2.CODE_WRAPPER_LOGIN)) } else { return this.findUser(writer, request) } diff --git a/code/context.go b/code/context.go index d323f77..221b0ea 100644 --- a/code/context.go +++ b/code/context.go @@ -4,9 +4,9 @@ import ( "fmt" "github.com/jinzhu/gorm" "reflect" - "tank/code/cache" "tank/code/config" "tank/code/logger" + cache2 "tank/code/tool/cache" ) //全局唯一的上下文(在main函数中初始化) @@ -17,7 +17,7 @@ type Context struct { //数据库连接 DB *gorm.DB //session缓存 - SessionCache *cache.CacheTable + SessionCache *cache2.CacheTable //各类的Bean Map。这里面是包含ControllerMap中所有元素 BeanMap map[string]IBean //只包含了Controller的map @@ -30,7 +30,7 @@ type Context struct { func (this *Context) Init() { //创建一个用于存储session的缓存。 - this.SessionCache = cache.NewCacheTable() + this.SessionCache = cache2.NewCacheTable() //初始化Map this.BeanMap = make(map[string]IBean) diff --git a/code/dashboard_controller.go b/code/dashboard_controller.go index 128ba2b..f6fbfb8 100644 --- a/code/dashboard_controller.go +++ b/code/dashboard_controller.go @@ -3,7 +3,7 @@ package code import ( "net/http" "strconv" - "tank/code/result" + result2 "tank/code/tool/result" ) type DashboardController struct { @@ -42,14 +42,14 @@ func (this *DashboardController) RegisterRoutes() map[string]func(writer http.Re } //过去七天分时调用量 -func (this *DashboardController) InvokeList(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *DashboardController) InvokeList(writer http.ResponseWriter, request *http.Request) *result2.WebResult { return this.Success("") } //按照分页的方式获取某个图片缓存夹下图片缓存和子图片缓存夹的列表,通常情况下只有一页。 -func (this *DashboardController) Page(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *DashboardController) Page(writer http.ResponseWriter, request *http.Request) *result2.WebResult { //如果是根目录,那么就传入root. pageStr := request.FormValue("page") @@ -97,7 +97,7 @@ func (this *DashboardController) Page(writer http.ResponseWriter, request *http. } -func (this *DashboardController) ActiveIpTop10(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *DashboardController) ActiveIpTop10(writer http.ResponseWriter, request *http.Request) *result2.WebResult { list := this.dashboardDao.ActiveIpTop10() return this.Success(list) } diff --git a/code/dav_controller.go b/code/dav_controller.go index 9ab17d5..3563592 100644 --- a/code/dav_controller.go +++ b/code/dav_controller.go @@ -7,7 +7,7 @@ import ( "net/http" "regexp" "strings" - "tank/code/result" + result2 "tank/code/tool/result" "tank/code/tool/util" ) @@ -79,16 +79,16 @@ func (this *DavController) CheckCurrentUser(writer http.ResponseWriter, request //要求前端使用Basic的形式授权 writer.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) - panic(result.ConstWebResult(result.CODE_WRAPPER_LOGIN)) + panic(result2.ConstWebResult(result2.CODE_WRAPPER_LOGIN)) } user := this.userDao.FindByUsername(username) if user == nil { - panic(result.BadRequest("邮箱或密码错误")) + panic(result2.BadRequest("邮箱或密码错误")) } else { if !util.MatchBcrypt(password, user.Password) { - panic(result.BadRequest("邮箱或密码错误")) + panic(result2.BadRequest("邮箱或密码错误")) } } diff --git a/code/dav_service.go b/code/dav_service.go index 078873c..bcb2249 100644 --- a/code/dav_service.go +++ b/code/dav_service.go @@ -7,9 +7,9 @@ import ( "path" "regexp" "strings" - "tank/code/result" dav2 "tank/code/tool/dav" xml2 "tank/code/tool/dav/xml" + result2 "tank/code/tool/result" "tank/code/tool/util" ) @@ -58,7 +58,7 @@ func (this *DavService) ParseDepth(request *http.Request) int { return 1 } } else { - panic(result.BadRequest("必须指定Header Depth")) + panic(result2.BadRequest("必须指定Header Depth")) } return depth } @@ -107,7 +107,7 @@ func (this *DavService) PropstatsFromXmlNames(user *User, matter *Matter, xmlNam } if len(properties) == 0 { - panic(result.BadRequest("请求的属性项无法解析!")) + panic(result2.BadRequest("请求的属性项无法解析!")) } okPropstat := dav2.Propstat{Status: http.StatusOK, Props: properties} @@ -136,7 +136,7 @@ func (this *DavService) Propstats(user *User, matter *Matter, propfind *dav2.Pro propstats := make([]dav2.Propstat, 0) if propfind.Propname != nil { - panic(result.BadRequest("propfind.Propname != nil 尚未处理")) + panic(result2.BadRequest("propfind.Propname != nil 尚未处理")) } else if propfind.Allprop != nil { //TODO: 如果include中还有内容,那么包含进去。 @@ -313,7 +313,7 @@ func (this *DavService) prepareMoveCopy( var destinationPath string if destinationStr == "" { - panic(result.BadRequest("Header Destination必填")) + panic(result2.BadRequest("Header Destination必填")) } //如果是重命名,那么就不是http开头了。 @@ -323,7 +323,7 @@ func (this *DavService) prepareMoveCopy( 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(result2.BadRequest("Destination Host不一致. %s %s != %s", destinationStr, destinationUrl.Host, request.Host)) } fullDestinationPath = destinationUrl.Path } @@ -338,7 +338,7 @@ func (this *DavService) prepareMoveCopy( if len(strs) == 2 { destinationPath = strs[1] } else { - panic(result.BadRequest("目标前缀必须为:%s", WEBDAV_PREFFIX)) + panic(result2.BadRequest("目标前缀必须为:%s", WEBDAV_PREFFIX)) } destinationName = util.GetFilenameOfPath(destinationPath) @@ -361,7 +361,7 @@ func (this *DavService) prepareMoveCopy( //如果是空或者/就是请求根目录 if srcMatter.Uuid == MATTER_ROOT { - panic(result.BadRequest("你不能移动根目录!")) + panic(result2.BadRequest("你不能移动根目录!")) } //寻找目标文件夹matter @@ -405,19 +405,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(result2.BadRequest("不支持LOCK方法")) } //解锁 func (this *DavService) HandleUnlock(writer http.ResponseWriter, request *http.Request, user *User, subPath string) { - panic(result.BadRequest("不支持UNLOCK方法")) + panic(result2.BadRequest("不支持UNLOCK方法")) } //修改文件属性 func (this *DavService) HandleProppatch(writer http.ResponseWriter, request *http.Request, user *User, subPath string) { - panic(result.BadRequest("不支持PROPPATCH方法")) + panic(result2.BadRequest("不支持PROPPATCH方法")) } //处理所有的请求 @@ -481,7 +481,7 @@ func (this *DavService) HandleDav(writer http.ResponseWriter, request *http.Requ } else { - panic(result.BadRequest("该方法还不支持。%s", method)) + panic(result2.BadRequest("该方法还不支持。%s", method)) } diff --git a/code/footprint_controller.go b/code/footprint_controller.go index e45efa1..5134010 100644 --- a/code/footprint_controller.go +++ b/code/footprint_controller.go @@ -3,7 +3,7 @@ package code import ( "net/http" "strconv" - "tank/code/result" + result2 "tank/code/tool/result" ) type FootprintController struct { @@ -43,11 +43,11 @@ func (this *FootprintController) RegisterRoutes() map[string]func(writer http.Re } //查看详情。 -func (this *FootprintController) Detail(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *FootprintController) Detail(writer http.ResponseWriter, request *http.Request) *result2.WebResult { uuid := request.FormValue("uuid") if uuid == "" { - panic(result.BadRequest("图片缓存的uuid必填")) + panic(result2.BadRequest("图片缓存的uuid必填")) } footprint := this.footprintService.Detail(uuid) @@ -65,7 +65,7 @@ func (this *FootprintController) Detail(writer http.ResponseWriter, request *htt } //按照分页的方式查询 -func (this *FootprintController) Page(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *FootprintController) Page(writer http.ResponseWriter, request *http.Request) *result2.WebResult { //如果是根目录,那么就传入root. pageStr := request.FormValue("page") @@ -109,11 +109,11 @@ func (this *FootprintController) Page(writer http.ResponseWriter, request *http. } //删除一条记录 -func (this *FootprintController) Delete(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *FootprintController) Delete(writer http.ResponseWriter, request *http.Request) *result2.WebResult { uuid := request.FormValue("uuid") if uuid == "" { - panic(result.BadRequest("uuid必填")) + panic(result2.BadRequest("uuid必填")) } footprint := this.footprintDao.FindByUuid(uuid) diff --git a/code/image_cache_controller.go b/code/image_cache_controller.go index 7617200..b32a564 100644 --- a/code/image_cache_controller.go +++ b/code/image_cache_controller.go @@ -4,7 +4,7 @@ import ( "net/http" "strconv" "strings" - "tank/code/result" + result2 "tank/code/tool/result" ) type ImageCacheController struct { @@ -45,11 +45,11 @@ func (this *ImageCacheController) RegisterRoutes() map[string]func(writer http.R } //查看某个图片缓存的详情。 -func (this *ImageCacheController) Detail(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *ImageCacheController) Detail(writer http.ResponseWriter, request *http.Request) *result2.WebResult { uuid := request.FormValue("uuid") if uuid == "" { - panic(result.BadRequest("图片缓存的uuid必填")) + panic(result2.BadRequest("图片缓存的uuid必填")) } imageCache := this.imageCacheService.Detail(uuid) @@ -67,7 +67,7 @@ func (this *ImageCacheController) Detail(writer http.ResponseWriter, request *ht } //按照分页的方式获取某个图片缓存夹下图片缓存和子图片缓存夹的列表,通常情况下只有一页。 -func (this *ImageCacheController) Page(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *ImageCacheController) Page(writer http.ResponseWriter, request *http.Request) *result2.WebResult { //如果是根目录,那么就传入root. pageStr := request.FormValue("page") pageSizeStr := request.FormValue("pageSize") @@ -123,11 +123,11 @@ func (this *ImageCacheController) Page(writer http.ResponseWriter, request *http } //删除一个图片缓存 -func (this *ImageCacheController) Delete(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *ImageCacheController) Delete(writer http.ResponseWriter, request *http.Request) *result2.WebResult { uuid := request.FormValue("uuid") if uuid == "" { - panic(result.BadRequest("图片缓存的uuid必填")) + panic(result2.BadRequest("图片缓存的uuid必填")) } imageCache := this.imageCacheDao.FindByUuid(uuid) @@ -136,7 +136,7 @@ func (this *ImageCacheController) Delete(writer http.ResponseWriter, request *ht user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR && imageCache.UserUuid != user.Uuid { - panic(result.Unauthorized("没有权限")) + panic(result2.Unauthorized("没有权限")) } this.imageCacheDao.Delete(imageCache) @@ -145,11 +145,11 @@ func (this *ImageCacheController) Delete(writer http.ResponseWriter, request *ht } //删除一系列图片缓存。 -func (this *ImageCacheController) DeleteBatch(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *ImageCacheController) DeleteBatch(writer http.ResponseWriter, request *http.Request) *result2.WebResult { uuids := request.FormValue("uuids") if uuids == "" { - panic(result.BadRequest("图片缓存的uuids必填")) + panic(result2.BadRequest("图片缓存的uuids必填")) } uuidArray := strings.Split(uuids, ",") @@ -161,7 +161,7 @@ func (this *ImageCacheController) DeleteBatch(writer http.ResponseWriter, reques //判断图片缓存的所属人是否正确 user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR && imageCache.UserUuid != user.Uuid { - panic(result.Unauthorized("没有权限")) + panic(result2.Unauthorized("没有权限")) } this.imageCacheDao.Delete(imageCache) diff --git a/code/install_controller.go b/code/install_controller.go index 3792b46..4b95277 100644 --- a/code/install_controller.go +++ b/code/install_controller.go @@ -12,7 +12,7 @@ import ( "regexp" "strconv" "tank/code/config" - "tank/code/result" + result2 "tank/code/tool/result" "tank/code/tool/util" "time" ) @@ -130,7 +130,7 @@ func (this *InstallController) getCreateSQLFromFile(tableName string) string { filePath := homePath + "/db/" + tableName + ".sql" exists, err := util.PathExists(filePath) if err != nil { - panic(result.Server("从安装目录判断建表语句文件是否存在时出错!")) + panic(result2.Server("从安装目录判断建表语句文件是否存在时出错!")) } //2. 从GOPATH下面去找,因为可能是开发环境 @@ -142,11 +142,11 @@ func (this *InstallController) getCreateSQLFromFile(tableName string) string { filePath = build.Default.GOPATH + "/src/tank/build/db/" + tableName + ".sql" exists, err = util.PathExists(filePath) if err != nil { - panic(result.Server("从GOPATH判断建表语句文件是否存在时出错!")) + panic(result2.Server("从GOPATH判断建表语句文件是否存在时出错!")) } if !exists { - panic(result.Server("%s 或 %s 均不存在,请检查你的安装情况。", filePath1, filePath)) + panic(result2.Server("%s 或 %s 均不存在,请检查你的安装情况。", filePath1, filePath)) } } @@ -219,17 +219,17 @@ func (this *InstallController) validateTableMetaList(tableInfoList []*InstallTab strs = append(strs, v.DBName) } - panic(result.BadRequest(fmt.Sprintf("%s 表的以下字段缺失:%v", tableInfo.Name, strs))) + panic(result2.BadRequest(fmt.Sprintf("%s 表的以下字段缺失:%v", tableInfo.Name, strs))) } } else { - panic(result.BadRequest(tableInfo.Name + "表不存在")) + panic(result2.BadRequest(tableInfo.Name + "表不存在")) } } } //验证数据库连接 -func (this *InstallController) Verify(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *InstallController) Verify(writer http.ResponseWriter, request *http.Request) *result2.WebResult { db := this.openDbConnection(writer, request) defer this.closeDbConnection(db) @@ -242,7 +242,7 @@ func (this *InstallController) Verify(writer http.ResponseWriter, request *http. } //获取需要安装的数据库表 -func (this *InstallController) TableInfoList(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *InstallController) TableInfoList(writer http.ResponseWriter, request *http.Request) *result2.WebResult { db := this.openDbConnection(writer, request) defer this.closeDbConnection(db) @@ -251,7 +251,7 @@ func (this *InstallController) TableInfoList(writer http.ResponseWriter, request } //创建缺失数据库和表 -func (this *InstallController) CreateTable(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *InstallController) CreateTable(writer http.ResponseWriter, request *http.Request) *result2.WebResult { var tableNames = []IBase{&Dashboard{}, &DownloadToken{}, &Footprint{}, &ImageCache{}, &Matter{}, &Preference{}, &Session{}, &UploadToken{}, &User{}} var installTableInfos []*InstallTableInfo @@ -280,7 +280,7 @@ func (this *InstallController) CreateTable(writer http.ResponseWriter, request * } //获取管理员列表(10条记录) -func (this *InstallController) AdminList(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *InstallController) AdminList(writer http.ResponseWriter, request *http.Request) *result2.WebResult { db := this.openDbConnection(writer, request) defer this.closeDbConnection(db) @@ -298,7 +298,7 @@ func (this *InstallController) AdminList(writer http.ResponseWriter, request *ht } //创建管理员 -func (this *InstallController) CreateAdmin(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *InstallController) CreateAdmin(writer http.ResponseWriter, request *http.Request) *result2.WebResult { db := this.openDbConnection(writer, request) defer this.closeDbConnection(db) @@ -309,15 +309,15 @@ func (this *InstallController) CreateAdmin(writer http.ResponseWriter, request * //验证超级管理员的信息 if m, _ := regexp.MatchString(`^[0-9a-zA-Z_]+$`, adminUsername); !m { - panic(result.BadRequest(`超级管理员用户名必填,且只能包含字母,数字和'_''`)) + panic(result2.BadRequest(`超级管理员用户名必填,且只能包含字母,数字和'_''`)) } if len(adminPassword) < 6 { - panic(result.BadRequest(`超级管理员密码长度至少为6位`)) + panic(result2.BadRequest(`超级管理员密码长度至少为6位`)) } if adminEmail == "" { - panic(result.BadRequest(`超级管理员邮箱必填`)) + panic(result2.BadRequest(`超级管理员邮箱必填`)) } //检查是否有重复。 @@ -325,14 +325,14 @@ func (this *InstallController) CreateAdmin(writer http.ResponseWriter, request * db1 := db.Model(&User{}).Where("email = ?", adminEmail).Count(&count1) this.PanicError(db1.Error) if count1 > 0 { - panic(result.BadRequest(`该邮箱已存在`)) + panic(result2.BadRequest(`该邮箱已存在`)) } var count2 int64 db2 := db.Model(&User{}).Where("username = ?", adminUsername).Count(&count2) this.PanicError(db2.Error) if count2 > 0 { - panic(result.BadRequest(`该用户名已存在`)) + panic(result2.BadRequest(`该用户名已存在`)) } user := &User{} @@ -359,7 +359,7 @@ func (this *InstallController) CreateAdmin(writer http.ResponseWriter, request * } //(如果数据库中本身存在管理员了)验证管理员 -func (this *InstallController) ValidateAdmin(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *InstallController) ValidateAdmin(writer http.ResponseWriter, request *http.Request) *result2.WebResult { db := this.openDbConnection(writer, request) defer this.closeDbConnection(db) @@ -369,24 +369,24 @@ func (this *InstallController) ValidateAdmin(writer http.ResponseWriter, request //验证超级管理员的信息 if adminEmail == "" { - panic(result.BadRequest(`超级管理员邮箱必填`)) + panic(result2.BadRequest(`超级管理员邮箱必填`)) } if len(adminPassword) < 6 { - panic(result.BadRequest(`超级管理员密码长度至少为6位`)) + panic(result2.BadRequest(`超级管理员密码长度至少为6位`)) } var existEmailUser = &User{} db = db.Where(&User{Email: adminEmail}).First(existEmailUser) if db.Error != nil { - panic(result.BadRequest(fmt.Sprintf("%s对应的用户不存在", adminEmail))) + panic(result2.BadRequest(fmt.Sprintf("%s对应的用户不存在", adminEmail))) } if !util.MatchBcrypt(adminPassword, existEmailUser.Password) { - panic(result.BadRequest("邮箱或密码错误")) + panic(result2.BadRequest("邮箱或密码错误")) } if existEmailUser.Role != USER_ROLE_ADMINISTRATOR { - panic(result.BadRequest("该账号不是管理员")) + panic(result2.BadRequest("该账号不是管理员")) } return this.Success("OK") @@ -394,7 +394,7 @@ func (this *InstallController) ValidateAdmin(writer http.ResponseWriter, request } //完成系统安装 -func (this *InstallController) Finish(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *InstallController) Finish(writer http.ResponseWriter, request *http.Request) *result2.WebResult { mysqlPortStr := request.FormValue("mysqlPort") mysqlHost := request.FormValue("mysqlHost") @@ -422,7 +422,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(result2.BadRequest(`请至少配置一名管理员`)) } var configItem = &config.ConfigItem{ diff --git a/code/matter_controller.go b/code/matter_controller.go index b5a4f0b..323074e 100644 --- a/code/matter_controller.go +++ b/code/matter_controller.go @@ -4,7 +4,7 @@ import ( "net/http" "strconv" "strings" - "tank/code/result" + result2 "tank/code/tool/result" ) type MatterController struct { @@ -68,11 +68,11 @@ func (this *MatterController) RegisterRoutes() map[string]func(writer http.Respo } //查看某个文件的详情。 -func (this *MatterController) Detail(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *MatterController) Detail(writer http.ResponseWriter, request *http.Request) *result2.WebResult { uuid := request.FormValue("uuid") if uuid == "" { - panic(result.BadRequest("文件的uuid必填")) + panic(result2.BadRequest("文件的uuid必填")) } matter := this.matterService.Detail(uuid) @@ -90,7 +90,7 @@ func (this *MatterController) Detail(writer http.ResponseWriter, request *http.R } //按照分页的方式获取某个文件夹下文件和子文件夹的列表,通常情况下只有一页。 -func (this *MatterController) Page(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *MatterController) Page(writer http.ResponseWriter, request *http.Request) *result2.WebResult { //如果是根目录,那么就传入root. pageStr := request.FormValue("page") @@ -171,7 +171,7 @@ func (this *MatterController) Page(writer http.ResponseWriter, request *http.Req } //创建一个文件夹。 -func (this *MatterController) CreateDirectory(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *MatterController) CreateDirectory(writer http.ResponseWriter, request *http.Request) *result2.WebResult { puuid := request.FormValue("puuid") name := request.FormValue("name") @@ -197,7 +197,7 @@ func (this *MatterController) CreateDirectory(writer http.ResponseWriter, reques } //上传文件 -func (this *MatterController) Upload(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *MatterController) Upload(writer http.ResponseWriter, request *http.Request) *result2.WebResult { userUuid := request.FormValue("userUuid") puuid := request.FormValue("puuid") @@ -240,7 +240,7 @@ func (this *MatterController) Upload(writer http.ResponseWriter, request *http.R } //从一个Url中去爬取资源 -func (this *MatterController) Crawl(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *MatterController) Crawl(writer http.ResponseWriter, request *http.Request) *result2.WebResult { userUuid := request.FormValue("userUuid") puuid := request.FormValue("puuid") @@ -280,11 +280,11 @@ func (this *MatterController) Crawl(writer http.ResponseWriter, request *http.Re } //删除一个文件 -func (this *MatterController) Delete(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *MatterController) Delete(writer http.ResponseWriter, request *http.Request) *result2.WebResult { uuid := request.FormValue("uuid") if uuid == "" { - panic(result.BadRequest("文件的uuid必填")) + panic(result2.BadRequest("文件的uuid必填")) } matter := this.matterDao.CheckByUuid(uuid) @@ -292,7 +292,7 @@ func (this *MatterController) Delete(writer http.ResponseWriter, request *http.R //判断文件的所属人是否正确 user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { - panic(result.Unauthorized("没有权限")) + panic(result2.Unauthorized("没有权限")) } this.matterService.AtomicDelete(matter) @@ -301,11 +301,11 @@ func (this *MatterController) Delete(writer http.ResponseWriter, request *http.R } //删除一系列文件。 -func (this *MatterController) DeleteBatch(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *MatterController) DeleteBatch(writer http.ResponseWriter, request *http.Request) *result2.WebResult { uuids := request.FormValue("uuids") if uuids == "" { - panic(result.BadRequest("文件的uuids必填")) + panic(result2.BadRequest("文件的uuids必填")) } uuidArray := strings.Split(uuids, ",") @@ -323,7 +323,7 @@ func (this *MatterController) DeleteBatch(writer http.ResponseWriter, request *h //判断文件的所属人是否正确 user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { - panic(result.Unauthorized("没有权限")) + panic(result2.Unauthorized("没有权限")) } this.matterService.AtomicDelete(matter) @@ -334,7 +334,7 @@ func (this *MatterController) DeleteBatch(writer http.ResponseWriter, request *h } //重命名一个文件或一个文件夹 -func (this *MatterController) Rename(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *MatterController) Rename(writer http.ResponseWriter, request *http.Request) *result2.WebResult { uuid := request.FormValue("uuid") name := request.FormValue("name") @@ -345,7 +345,7 @@ func (this *MatterController) Rename(writer http.ResponseWriter, request *http.R matter := this.matterDao.CheckByUuid(uuid) if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { - panic(result.Unauthorized("没有权限")) + panic(result2.Unauthorized("没有权限")) } this.matterService.AtomicRename(matter, name, user) @@ -354,7 +354,7 @@ func (this *MatterController) Rename(writer http.ResponseWriter, request *http.R } //改变一个文件的公私有属性 -func (this *MatterController) ChangePrivacy(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *MatterController) ChangePrivacy(writer http.ResponseWriter, request *http.Request) *result2.WebResult { uuid := request.FormValue("uuid") privacyStr := request.FormValue("privacy") privacy := false @@ -371,7 +371,7 @@ func (this *MatterController) ChangePrivacy(writer http.ResponseWriter, request //权限验证 user := this.checkUser(writer, request) if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { - panic(result.Unauthorized("没有权限")) + panic(result2.Unauthorized("没有权限")) } matter.Privacy = privacy @@ -381,7 +381,7 @@ func (this *MatterController) ChangePrivacy(writer http.ResponseWriter, request } //将一个文件夹或者文件移入到另一个文件夹下。 -func (this *MatterController) Move(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *MatterController) Move(writer http.ResponseWriter, request *http.Request) *result2.WebResult { srcUuidsStr := request.FormValue("srcUuids") destUuid := request.FormValue("destUuid") @@ -390,7 +390,7 @@ func (this *MatterController) Move(writer http.ResponseWriter, request *http.Req var srcUuids []string //验证参数。 if srcUuidsStr == "" { - panic(result.BadRequest("srcUuids参数必填")) + panic(result2.BadRequest("srcUuids参数必填")) } else { srcUuids = strings.Split(srcUuidsStr, ",") } @@ -405,11 +405,11 @@ func (this *MatterController) Move(writer http.ResponseWriter, request *http.Req //验证dest是否有问题 var destMatter = this.matterDao.CheckWithRootByUuid(destUuid, user) if !destMatter.Dir { - panic(result.BadRequest("目标不是文件夹")) + panic(result2.BadRequest("目标不是文件夹")) } if user.Role != USER_ROLE_ADMINISTRATOR && destMatter.UserUuid != user.Uuid { - panic(result.Unauthorized("没有权限")) + panic(result2.Unauthorized("没有权限")) } var srcMatters []*Matter @@ -419,14 +419,14 @@ func (this *MatterController) Move(writer http.ResponseWriter, request *http.Req srcMatter := this.matterDao.CheckByUuid(uuid) if srcMatter.Puuid == destMatter.Uuid { - panic(result.BadRequest("没有进行移动,操作无效!")) + panic(result2.BadRequest("没有进行移动,操作无效!")) } //判断同级文件夹中是否有同名的文件 count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, destMatter.Uuid, srcMatter.Dir, srcMatter.Name) if count > 0 { - panic(result.BadRequest("【" + srcMatter.Name + "】在目标文件夹已经存在了,操作失败。")) + panic(result2.BadRequest("【" + srcMatter.Name + "】在目标文件夹已经存在了,操作失败。")) } //判断和目标文件夹是否是同一个主人。 diff --git a/code/matter_dao.go b/code/matter_dao.go index 9d0c905..f04cec4 100644 --- a/code/matter_dao.go +++ b/code/matter_dao.go @@ -5,7 +5,7 @@ import ( "github.com/nu7hatch/gouuid" "os" "tank/code/config" - "tank/code/result" + result2 "tank/code/tool/result" "tank/code/tool/util" "time" ) @@ -33,7 +33,7 @@ func (this *MatterDao) FindByUuid(uuid string) *Matter { var matter Matter db := CONTEXT.DB.Where(&Matter{Base: Base{Uuid: uuid}}).First(&matter) if db.Error != nil { - if db.Error.Error() == result.DB_ERROR_NOT_FOUND { + if db.Error.Error() == result2.DB_ERROR_NOT_FOUND { return nil } else { this.PanicError(db.Error) @@ -46,7 +46,7 @@ func (this *MatterDao) FindByUuid(uuid string) *Matter { func (this *MatterDao) CheckByUuid(uuid string) *Matter { matter := this.FindByUuid(uuid) if matter == nil { - panic(result.NotFound("%s 对应的matter不存在", uuid)) + panic(result2.NotFound("%s 对应的matter不存在", uuid)) } return matter } @@ -55,13 +55,13 @@ func (this *MatterDao) CheckByUuid(uuid string) *Matter { func (this *MatterDao) CheckWithRootByUuid(uuid string, user *User) *Matter { if uuid == "" { - panic(result.BadRequest("uuid cannot be nil.")) + panic(result2.BadRequest("uuid cannot be nil.")) } var matter *Matter if uuid == MATTER_ROOT { if user == nil { - panic(result.BadRequest("user cannot be nil.")) + panic(result2.BadRequest("user cannot be nil.")) } matter = NewRootMatter(user) } else { @@ -77,7 +77,7 @@ func (this *MatterDao) CheckWithRootByPath(path string, user *User) *Matter { var matter *Matter if user == nil { - panic(result.BadRequest("user cannot be nil.")) + panic(result2.BadRequest("user cannot be nil.")) } //目标文件夹matter @@ -335,7 +335,7 @@ func (this *MatterDao) findByUserUuidAndPath(userUuid string, path string) *Matt db := CONTEXT.DB.Model(&Matter{}).Where(wp.Query, wp.Args...).First(matter) if db.Error != nil { - if db.Error.Error() == result.DB_ERROR_NOT_FOUND { + if db.Error.Error() == result2.DB_ERROR_NOT_FOUND { return nil } else { this.PanicError(db.Error) @@ -349,11 +349,11 @@ func (this *MatterDao) findByUserUuidAndPath(userUuid string, path string) *Matt func (this *MatterDao) checkByUserUuidAndPath(userUuid string, path string) *Matter { if path == "" { - panic(result.BadRequest("path 不能为空")) + panic(result2.BadRequest("path 不能为空")) } matter := this.findByUserUuidAndPath(userUuid, path) if matter == nil { - panic(result.NotFound("path = %s 不存在", path)) + panic(result2.NotFound("path = %s 不存在", path)) } return matter diff --git a/code/matter_service.go b/code/matter_service.go index 999503f..9bb89c2 100644 --- a/code/matter_service.go +++ b/code/matter_service.go @@ -6,8 +6,8 @@ import ( "os" "regexp" "strings" - "tank/code/download" - "tank/code/result" + download2 "tank/code/tool/download" + result2 "tank/code/tool/result" "tank/code/tool/util" ) @@ -65,14 +65,14 @@ func (this *MatterService) DownloadFile( filename string, withContentDisposition bool) { - download.DownloadFile(writer, request, filePath, filename, withContentDisposition) + download2.DownloadFile(writer, request, filePath, filename, withContentDisposition) } //删除文件 func (this *MatterService) AtomicDelete(matter *Matter) { if matter == nil { - panic(result.BadRequest("matter不能为nil")) + panic(result2.BadRequest("matter不能为nil")) } //操作锁 @@ -86,17 +86,17 @@ func (this *MatterService) AtomicDelete(matter *Matter) { func (this *MatterService) Upload(file io.Reader, user *User, dirMatter *Matter, filename string, privacy bool) *Matter { if user == nil { - panic(result.BadRequest("user cannot be nil.")) + panic(result2.BadRequest("user cannot be nil.")) } //验证dirMatter if dirMatter == nil { - panic(result.BadRequest("dirMatter cannot be nil.")) + panic(result2.BadRequest("dirMatter cannot be nil.")) } //文件名不能太长。 if len(filename) > MATTER_NAME_MAX_LENGTH { - panic(result.BadRequest("文件名不能超过%s", MATTER_NAME_MAX_LENGTH)) + panic(result2.BadRequest("文件名不能超过%s", MATTER_NAME_MAX_LENGTH)) } //文件夹路径 @@ -105,7 +105,7 @@ func (this *MatterService) Upload(file io.Reader, user *User, dirMatter *Matter, count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, dirMatter.Uuid, false, filename) if count > 0 { - panic(result.BadRequest("该目录下%s已经存在了", filename)) + panic(result2.BadRequest("该目录下%s已经存在了", filename)) } //获取文件应该存放在的物理路径的绝对路径和相对路径。 @@ -144,7 +144,7 @@ func (this *MatterService) Upload(file io.Reader, user *User, dirMatter *Matter, err = os.Remove(fileAbsolutePath) this.PanicError(err) - panic(result.BadRequest("文件大小超出限制 %s > %s ", util.HumanFileSize(user.SizeLimit), util.HumanFileSize(fileSize))) + panic(result2.BadRequest("文件大小超出限制 %s > %s ", util.HumanFileSize(user.SizeLimit), util.HumanFileSize(fileSize))) } } @@ -169,7 +169,7 @@ func (this *MatterService) Upload(file io.Reader, user *User, dirMatter *Matter, func (this *MatterService) AtomicUpload(file io.Reader, user *User, dirMatter *Matter, filename string, privacy bool) *Matter { if user == nil { - panic(result.BadRequest("user cannot be nil.")) + panic(result2.BadRequest("user cannot be nil.")) } //操作锁 @@ -184,33 +184,33 @@ func (this *MatterService) createDirectory(dirMatter *Matter, name string, user //父级matter必须存在 if dirMatter == nil { - panic(result.BadRequest("dirMatter必须指定")) + panic(result2.BadRequest("dirMatter必须指定")) } //必须是文件夹 if !dirMatter.Dir { - panic(result.BadRequest("dirMatter必须是文件夹")) + panic(result2.BadRequest("dirMatter必须是文件夹")) } if dirMatter.UserUuid != user.Uuid { - panic(result.BadRequest("dirMatter的userUuid和user不一致")) + panic(result2.BadRequest("dirMatter的userUuid和user不一致")) } name = strings.TrimSpace(name) //验证参数。 if name == "" { - panic(result.BadRequest("name参数必填,并且不能全是空格")) + panic(result2.BadRequest("name参数必填,并且不能全是空格")) } if len(name) > MATTER_NAME_MAX_LENGTH { - panic(result.BadRequest("name长度不能超过%d", MATTER_NAME_MAX_LENGTH)) + panic(result2.BadRequest("name长度不能超过%d", MATTER_NAME_MAX_LENGTH)) } if m, _ := regexp.MatchString(`[<>|*?/\\]`, name); m { - panic(result.BadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`)) + panic(result2.BadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`)) } //判断同级文件夹中是否有同名的文件夹 @@ -218,14 +218,14 @@ func (this *MatterService) createDirectory(dirMatter *Matter, name string, user if count > 0 { - panic(result.BadRequest("%s 已经存在了,请使用其他名称。", name)) + panic(result2.BadRequest("%s 已经存在了,请使用其他名称。", name)) } parts := strings.Split(dirMatter.Path, "/") this.logger.Info("%s的层数:%d", dirMatter.Name, len(parts)) if len(parts) > 32 { - panic(result.BadRequest("文件夹最多%d层", MATTER_NAME_MAX_DEPTH)) + panic(result2.BadRequest("文件夹最多%d层", MATTER_NAME_MAX_DEPTH)) } //绝对路径 @@ -277,7 +277,7 @@ func (this *MatterService) handleOverwrite(userUuid string, destinationPath stri //要求覆盖。那么删除。 this.matterDao.Delete(destMatter) } else { - panic(result.BadRequest("%s已经存在,操作失败!", destMatter.Path)) + panic(result2.BadRequest("%s已经存在,操作失败!", destMatter.Path)) } } @@ -287,11 +287,11 @@ func (this *MatterService) handleOverwrite(userUuid string, destinationPath stri func (this *MatterService) move(srcMatter *Matter, destDirMatter *Matter) { if srcMatter == nil { - panic(result.BadRequest("srcMatter cannot be nil.")) + panic(result2.BadRequest("srcMatter cannot be nil.")) } if !destDirMatter.Dir { - panic(result.BadRequest("目标必须为文件夹")) + panic(result2.BadRequest("目标必须为文件夹")) } if srcMatter.Dir { @@ -341,7 +341,7 @@ func (this *MatterService) move(srcMatter *Matter, destDirMatter *Matter) { func (this *MatterService) AtomicMove(srcMatter *Matter, destDirMatter *Matter, overwrite bool) { if srcMatter == nil { - panic(result.BadRequest("srcMatter cannot be nil.")) + panic(result2.BadRequest("srcMatter cannot be nil.")) } //操作锁 @@ -349,10 +349,10 @@ func (this *MatterService) AtomicMove(srcMatter *Matter, destDirMatter *Matter, defer this.userService.MatterUnlock(srcMatter.UserUuid) if destDirMatter == nil { - panic(result.BadRequest("destDirMatter cannot be nil.")) + panic(result2.BadRequest("destDirMatter cannot be nil.")) } if !destDirMatter.Dir { - panic(result.BadRequest("目标必须为文件夹")) + panic(result2.BadRequest("目标必须为文件夹")) } //文件夹不能把自己移入到自己中,也不可以移入到自己的子文件夹下。 @@ -377,7 +377,7 @@ func (this *MatterService) AtomicMove(srcMatter *Matter, destDirMatter *Matter, func (this *MatterService) AtomicMoveBatch(srcMatters []*Matter, destDirMatter *Matter) { if destDirMatter == nil { - panic(result.BadRequest("destDirMatter cannot be nil.")) + panic(result2.BadRequest("destDirMatter cannot be nil.")) } //操作锁 @@ -385,11 +385,11 @@ func (this *MatterService) AtomicMoveBatch(srcMatters []*Matter, destDirMatter * defer this.userService.MatterUnlock(destDirMatter.UserUuid) if srcMatters == nil { - panic(result.BadRequest("srcMatters cannot be nil.")) + panic(result2.BadRequest("srcMatters cannot be nil.")) } if !destDirMatter.Dir { - panic(result.BadRequest("目标必须为文件夹")) + panic(result2.BadRequest("目标必须为文件夹")) } //文件夹不能把自己移入到自己中,也不可以移入到自己的子文件夹下。 @@ -469,7 +469,7 @@ func (this *MatterService) copy(srcMatter *Matter, destDirMatter *Matter, name s func (this *MatterService) AtomicCopy(srcMatter *Matter, destDirMatter *Matter, name string, overwrite bool) { if srcMatter == nil { - panic(result.BadRequest("srcMatter cannot be nil.")) + panic(result2.BadRequest("srcMatter cannot be nil.")) } //操作锁 @@ -477,7 +477,7 @@ func (this *MatterService) AtomicCopy(srcMatter *Matter, destDirMatter *Matter, defer this.userService.MatterUnlock(srcMatter.UserUuid) if !destDirMatter.Dir { - panic(result.BadRequest("目标必须为文件夹")) + panic(result2.BadRequest("目标必须为文件夹")) } destinationPath := destDirMatter.Path + "/" + name @@ -490,7 +490,7 @@ func (this *MatterService) AtomicCopy(srcMatter *Matter, destDirMatter *Matter, func (this *MatterService) AtomicRename(matter *Matter, name string, user *User) { if user == nil { - panic(result.BadRequest("user cannot be nil")) + panic(result2.BadRequest("user cannot be nil")) } //操作锁 @@ -499,10 +499,10 @@ func (this *MatterService) AtomicRename(matter *Matter, name string, user *User) //验证参数。 if name == "" { - panic(result.BadRequest("name参数必填")) + panic(result2.BadRequest("name参数必填")) } if m, _ := regexp.MatchString(`[<>|*?/\\]`, name); m { - panic(result.BadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`)) + panic(result2.BadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`)) } if len(name) > 200 { @@ -510,14 +510,14 @@ func (this *MatterService) AtomicRename(matter *Matter, name string, user *User) } if name == matter.Name { - panic(result.BadRequest("新名称和旧名称一样,操作失败!")) + panic(result2.BadRequest("新名称和旧名称一样,操作失败!")) } //判断同级文件夹中是否有同名的文件 count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, matter.Puuid, matter.Dir, name) if count > 0 { - panic(result.BadRequest("【" + name + "】已经存在了,请使用其他名称。")) + panic(result2.BadRequest("【" + name + "】已经存在了,请使用其他名称。")) } if matter.Dir { @@ -594,7 +594,7 @@ func (this *MatterService) CreateDirectories(user *User, dirPath string) *Matter folders := strings.Split(dirPath, "/") if len(folders) > MATTER_NAME_MAX_DEPTH { - panic(result.BadRequest("文件夹最多%d层。", MATTER_NAME_MAX_DEPTH)) + panic(result2.BadRequest("文件夹最多%d层。", MATTER_NAME_MAX_DEPTH)) } var dirMatter *Matter @@ -616,7 +616,7 @@ func (this *MatterService) CreateDirectories(user *User, dirPath string) *Matter func (this *MatterService) WrapDetail(matter *Matter) *Matter { if matter == nil { - panic(result.BadRequest("matter cannot be nil.")) + panic(result2.BadRequest("matter cannot be nil.")) } //组装file的内容,展示其父组件。 @@ -642,7 +642,7 @@ func (this *MatterService) Detail(uuid string) *Matter { func (this *MatterService) AtomicCrawl(url string, filename string, user *User, dirMatter *Matter, privacy bool) *Matter { if user == nil { - panic(result.BadRequest("user cannot be nil.")) + panic(result2.BadRequest("user cannot be nil.")) } //操作锁 diff --git a/code/preference_controller.go b/code/preference_controller.go index 2997a70..b7e5e0d 100644 --- a/code/preference_controller.go +++ b/code/preference_controller.go @@ -2,7 +2,7 @@ package code import ( "net/http" - "tank/code/result" + result2 "tank/code/tool/result" "tank/code/tool/util" ) @@ -43,7 +43,7 @@ func (this *PreferenceController) RegisterRoutes() map[string]func(writer http.R } //查看某个偏好设置的详情。 -func (this *PreferenceController) Fetch(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *PreferenceController) Fetch(writer http.ResponseWriter, request *http.Request) *result2.WebResult { preference := this.preferenceService.Fetch() @@ -51,7 +51,7 @@ func (this *PreferenceController) Fetch(writer http.ResponseWriter, request *htt } //修改 -func (this *PreferenceController) Edit(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *PreferenceController) Edit(writer http.ResponseWriter, request *http.Request) *result2.WebResult { //验证参数。 name := request.FormValue("name") @@ -86,13 +86,13 @@ func (this *PreferenceController) Edit(writer http.ResponseWriter, request *http } //清扫系统,所有数据全部丢失。一定要非常慎点,非常慎点!只在系统初始化的时候点击! -func (this *PreferenceController) SystemCleanup(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *PreferenceController) SystemCleanup(writer http.ResponseWriter, request *http.Request) *result2.WebResult { user := this.checkUser(writer, request) password := request.FormValue("password") if !util.MatchBcrypt(password, user.Password) { - panic(result.BadRequest("密码错误,不能执行!")) + panic(result2.BadRequest("密码错误,不能执行!")) } for _, bean := range CONTEXT.BeanMap { diff --git a/code/preference_dao.go b/code/preference_dao.go index b9f0d5b..af93504 100644 --- a/code/preference_dao.go +++ b/code/preference_dao.go @@ -2,7 +2,7 @@ package code import ( "github.com/nu7hatch/gouuid" - "tank/code/result" + result2 "tank/code/tool/result" "time" ) @@ -18,7 +18,7 @@ func (this *PreferenceDao) Fetch() *Preference { db := CONTEXT.DB.First(preference) if db.Error != nil { - if db.Error.Error() == result.DB_ERROR_NOT_FOUND { + if db.Error.Error() == result2.DB_ERROR_NOT_FOUND { preference.Name = "蓝眼云盘" preference.ShowAlien = true this.Create(preference) diff --git a/code/router.go b/code/router.go index c60137f..8e27461 100644 --- a/code/router.go +++ b/code/router.go @@ -9,7 +9,7 @@ import ( "strings" "tank/code/config" "tank/code/logger" - "tank/code/result" + result2 "tank/code/tool/result" "tank/code/tool/util" "time" ) @@ -74,26 +74,26 @@ func (this *Router) GlobalPanicHandler(writer http.ResponseWriter, request *http logger.LOGGER.Error("错误: %v", err) - var webResult *result.WebResult = nil + var webResult *result2.WebResult = nil if value, ok := err.(string); ok { //一个字符串,默认是请求错误。 - webResult = result.CustomWebResult(result.CODE_WRAPPER_BAD_REQUEST, value) - } else if value, ok := err.(*result.WebResult); ok { + webResult = result2.CustomWebResult(result2.CODE_WRAPPER_BAD_REQUEST, value) + } else if value, ok := err.(*result2.WebResult); ok { //一个WebResult对象 webResult = value - } else if value, ok := err.(*result.CodeWrapper); ok { + } else if value, ok := err.(*result2.CodeWrapper); ok { //一个WebResult对象 - webResult = result.ConstWebResult(value) + webResult = result2.ConstWebResult(value) } else if value, ok := err.(error); ok { //一个普通的错误对象 - webResult = result.CustomWebResult(result.CODE_WRAPPER_UNKNOWN, value.Error()) + webResult = result2.CustomWebResult(result2.CODE_WRAPPER_UNKNOWN, value.Error()) } else { //其他不能识别的内容 - webResult = result.ConstWebResult(result.CODE_WRAPPER_UNKNOWN) + webResult = result2.ConstWebResult(result2.CODE_WRAPPER_UNKNOWN) } //修改http code码 - writer.WriteHeader(result.FetchHttpStatus(webResult.Code)) + writer.WriteHeader(result2.FetchHttpStatus(webResult.Code)) //输出的是json格式 返回的内容申明是json,utf-8 writer.Header().Set("Content-Type", "application/json;charset=UTF-8") @@ -152,7 +152,7 @@ func (this *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request) } if !canHandle { - panic(result.CustomWebResult(result.CODE_WRAPPER_NOT_FOUND, fmt.Sprintf("没有找到能够处理%s的方法", path))) + panic(result2.CustomWebResult(result2.CODE_WRAPPER_NOT_FOUND, fmt.Sprintf("没有找到能够处理%s的方法", path))) } } @@ -166,7 +166,7 @@ func (this *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request) if handler, ok := this.installRouteMap[path]; ok { handler(writer, request) } else { - panic(result.ConstWebResult(result.CODE_WRAPPER_NOT_INSTALLED)) + panic(result2.ConstWebResult(result2.CODE_WRAPPER_NOT_INSTALLED)) } } diff --git a/code/cache/cache.go b/code/tool/cache/cache.go similarity index 100% rename from code/cache/cache.go rename to code/tool/cache/cache.go diff --git a/code/tool/dav/prop.go b/code/tool/dav/prop.go index 75b1bd7..4b2b4e3 100644 --- a/code/tool/dav/prop.go +++ b/code/tool/dav/prop.go @@ -10,8 +10,8 @@ import ( "fmt" "io" "net/http" - "tank/code/result" "tank/code/tool/dav/xml" + result2 "tank/code/tool/result" ) // Proppatch describes a property update instruction as defined in RFC 4918. @@ -239,20 +239,20 @@ func ReadPropfind(reader io.Reader) (propfind *Propfind) { err = errInvalidPropfind } - panic(result.BadRequest(err.Error())) + panic(result2.BadRequest(err.Error())) } if propfind.Allprop == nil && propfind.Include != nil { - panic(result.BadRequest(errInvalidPropfind.Error())) + panic(result2.BadRequest(errInvalidPropfind.Error())) } if propfind.Allprop != nil && (propfind.Prop != nil || propfind.Propname != nil) { - panic(result.BadRequest(errInvalidPropfind.Error())) + panic(result2.BadRequest(errInvalidPropfind.Error())) } if propfind.Prop != nil && propfind.Propname != nil { - panic(result.BadRequest(errInvalidPropfind.Error())) + panic(result2.BadRequest(errInvalidPropfind.Error())) } if propfind.Propname == nil && propfind.Allprop == nil && propfind.Prop == nil { - panic(result.BadRequest(errInvalidPropfind.Error())) + panic(result2.BadRequest(errInvalidPropfind.Error())) } return propfind diff --git a/code/download/download.go b/code/tool/download/download.go similarity index 99% rename from code/download/download.go rename to code/tool/download/download.go index a53a4f0..ea53c1c 100644 --- a/code/download/download.go +++ b/code/tool/download/download.go @@ -11,7 +11,7 @@ import ( "os" "strconv" "strings" - "tank/code/result" + "tank/code/tool/result" "tank/code/tool/util" "time" ) @@ -205,15 +205,12 @@ func SumRangesSize(ranges []HttpRange) (size int64) { return } - func PanicError(err error) { if err != nil { panic(err) } } - - //文件下载。具有进度功能。 //下载功能参考:https://github.com/Masterminds/go-fileserver func DownloadFile( @@ -231,7 +228,6 @@ func DownloadFile( PanicError(e) }() - //根据参数添加content-disposition。该Header会让浏览器自动下载,而不是预览。 if withContentDisposition { fileName := url.QueryEscape(filename) diff --git a/code/result/web_result.go b/code/tool/result/web_result.go similarity index 100% rename from code/result/web_result.go rename to code/tool/result/web_result.go diff --git a/code/tool/util/util_path.go b/code/tool/util/util_path.go index 2290216..7d58dae 100644 --- a/code/tool/util/util_path.go +++ b/code/tool/util/util_path.go @@ -9,7 +9,7 @@ import ( "os/user" "path/filepath" "strings" - "tank/code/result" + result2 "tank/code/tool/result" ) //判断文件或文件夹是否已经存在 @@ -126,13 +126,13 @@ func GetFilenameOfPath(fullPath string) string { func DeleteEmptyDir(dirPath string) bool { dir, err := ioutil.ReadDir(dirPath) if err != nil { - panic(result.BadRequest("尝试读取目录%s时出错 %s", dirPath, err.Error())) + panic(result2.BadRequest("尝试读取目录%s时出错 %s", dirPath, err.Error())) } if len(dir) == 0 { //空文件夹 err = os.Remove(dirPath) if err != nil { - panic(result.BadRequest("删除磁盘上的文件夹%s出错 %s", dirPath, err.Error())) + panic(result2.BadRequest("删除磁盘上的文件夹%s出错 %s", dirPath, err.Error())) } return true } diff --git a/code/user_controller.go b/code/user_controller.go index e4de8b8..8be4ba8 100644 --- a/code/user_controller.go +++ b/code/user_controller.go @@ -5,7 +5,7 @@ import ( "regexp" "strconv" "tank/code/config" - "tank/code/result" + result2 "tank/code/tool/result" "tank/code/tool/util" "time" ) @@ -43,25 +43,25 @@ func (this *UserController) RegisterRoutes() map[string]func(writer http.Respons //参数: // @email:邮箱 // @password:密码 -func (this *UserController) Login(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *UserController) Login(writer http.ResponseWriter, request *http.Request) *result2.WebResult { email := request.FormValue("email") password := request.FormValue("password") if "" == email || "" == password { - panic(result.BadRequest("请输入邮箱和密码")) + panic(result2.BadRequest("请输入邮箱和密码")) } user := this.userDao.FindByEmail(email) if user == nil { - panic(result.BadRequest("邮箱或密码错误")) + panic(result2.BadRequest("邮箱或密码错误")) } else { if !util.MatchBcrypt(password, user.Password) { - panic(result.BadRequest("邮箱或密码错误")) + panic(result2.BadRequest("邮箱或密码错误")) } } @@ -96,7 +96,7 @@ func (this *UserController) Login(writer http.ResponseWriter, request *http.Requ } //创建一个用户 -func (this *UserController) Create(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *UserController) Create(writer http.ResponseWriter, request *http.Request) *result2.WebResult { username := request.FormValue("username") if m, _ := regexp.MatchString(`^[0-9a-zA-Z_]+$`, username); !m { @@ -159,7 +159,7 @@ func (this *UserController) Create(writer http.ResponseWriter, request *http.Req } //编辑一个用户的资料。 -func (this *UserController) Edit(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *UserController) Edit(writer http.ResponseWriter, request *http.Request) *result2.WebResult { avatarUrl := request.FormValue("avatarUrl") uuid := request.FormValue("uuid") @@ -187,7 +187,7 @@ func (this *UserController) Edit(writer http.ResponseWriter, request *http.Reque user.SizeLimit = sizeLimit } else { if currentUser.Uuid != uuid { - panic(result.Unauthorized("没有权限")) + panic(result2.Unauthorized("没有权限")) } } @@ -202,7 +202,7 @@ func (this *UserController) Edit(writer http.ResponseWriter, request *http.Reque } //获取用户详情 -func (this *UserController) Detail(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *UserController) Detail(writer http.ResponseWriter, request *http.Request) *result2.WebResult { uuid := request.FormValue("uuid") @@ -213,7 +213,7 @@ func (this *UserController) Detail(writer http.ResponseWriter, request *http.Req } //退出登录 -func (this *UserController) Logout(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *UserController) Logout(writer http.ResponseWriter, request *http.Request) *result2.WebResult { //session置为过期 sessionCookie, err := request.Cookie(config.COOKIE_AUTH_KEY) @@ -249,7 +249,7 @@ func (this *UserController) Logout(writer http.ResponseWriter, request *http.Req } //获取用户列表 管理员的权限。 -func (this *UserController) Page(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *UserController) Page(writer http.ResponseWriter, request *http.Request) *result2.WebResult { pageStr := request.FormValue("page") pageSizeStr := request.FormValue("pageSize") @@ -301,7 +301,7 @@ func (this *UserController) Page(writer http.ResponseWriter, request *http.Reque } //禁用用户 -func (this *UserController) Disable(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *UserController) Disable(writer http.ResponseWriter, request *http.Request) *result2.WebResult { uuid := request.FormValue("uuid") @@ -309,11 +309,11 @@ func (this *UserController) Disable(writer http.ResponseWriter, request *http.Re loginUser := this.checkUser(writer, request) if uuid == loginUser.Uuid { - panic(result.BadRequest("你不能操作自己的状态。")) + panic(result2.BadRequest("你不能操作自己的状态。")) } if user.Status == USER_STATUS_DISABLED { - panic(result.BadRequest("用户已经被禁用,操作无效。")) + panic(result2.BadRequest("用户已经被禁用,操作无效。")) } user.Status = USER_STATUS_DISABLED @@ -325,18 +325,18 @@ func (this *UserController) Disable(writer http.ResponseWriter, request *http.Re } //启用用户 -func (this *UserController) Enable(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *UserController) Enable(writer http.ResponseWriter, request *http.Request) *result2.WebResult { uuid := request.FormValue("uuid") user := this.userDao.CheckByUuid(uuid) loginUser := this.checkUser(writer, request) if uuid == loginUser.Uuid { - panic(result.BadRequest("你不能操作自己的状态。")) + panic(result2.BadRequest("你不能操作自己的状态。")) } if user.Status == USER_STATUS_OK { - panic(result.BadRequest("用户已经是正常状态,操作无效。")) + panic(result2.BadRequest("用户已经是正常状态,操作无效。")) } user.Status = USER_STATUS_OK @@ -348,12 +348,12 @@ func (this *UserController) Enable(writer http.ResponseWriter, request *http.Req } //用户修改密码 -func (this *UserController) ChangePassword(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *UserController) ChangePassword(writer http.ResponseWriter, request *http.Request) *result2.WebResult { oldPassword := request.FormValue("oldPassword") newPassword := request.FormValue("newPassword") if oldPassword == "" || newPassword == "" { - panic(result.BadRequest("旧密码和新密码都不能为空")) + panic(result2.BadRequest("旧密码和新密码都不能为空")) } user := this.checkUser(writer, request) @@ -364,7 +364,7 @@ func (this *UserController) ChangePassword(writer http.ResponseWriter, request * } if !util.MatchBcrypt(oldPassword, user.Password) { - panic(result.BadRequest("旧密码不正确!")) + panic(result2.BadRequest("旧密码不正确!")) } user.Password = util.GetBcrypt(newPassword) @@ -375,21 +375,21 @@ func (this *UserController) ChangePassword(writer http.ResponseWriter, request * } //管理员重置用户密码 -func (this *UserController) ResetPassword(writer http.ResponseWriter, request *http.Request) *result.WebResult { +func (this *UserController) ResetPassword(writer http.ResponseWriter, request *http.Request) *result2.WebResult { userUuid := request.FormValue("userUuid") password := request.FormValue("password") if userUuid == "" { - panic(result.BadRequest("用户不能为空")) + panic(result2.BadRequest("用户不能为空")) } if password == "" { - panic(result.BadRequest("密码不能为空")) + panic(result2.BadRequest("密码不能为空")) } currentUser := this.checkUser(writer, request) if currentUser.Role != USER_ROLE_ADMINISTRATOR { - panic(result.Unauthorized("没有权限")) + panic(result2.Unauthorized("没有权限")) } user := this.userDao.CheckByUuid(userUuid) diff --git a/code/user_service.go b/code/user_service.go index 76a7e32..db7313f 100644 --- a/code/user_service.go +++ b/code/user_service.go @@ -2,9 +2,9 @@ package code import ( "net/http" - "tank/code/cache" "tank/code/config" - "tank/code/result" + cache2 "tank/code/tool/cache" + result2 "tank/code/tool/result" "time" ) @@ -15,7 +15,7 @@ type UserService struct { sessionDao *SessionDao //操作文件的锁。 - locker *cache.CacheTable + locker *cache2.CacheTable } //初始化方法 @@ -34,7 +34,7 @@ func (this *UserService) Init() { } //创建一个用于存储用户文件锁的缓存。 - this.locker = cache.NewCacheTable() + this.locker = cache2.NewCacheTable() } @@ -50,7 +50,7 @@ func (this *UserService) MatterLock(userUuid string) { //当前被锁住了。 if cacheItem != nil && cacheItem.Data() != nil { - panic(result.BadRequest("当前正在进行文件操作,请稍后再试!")) + panic(result2.BadRequest("当前正在进行文件操作,请稍后再试!")) } //添加一把新锁,有效期为12小时