diff --git a/rest/bean.go b/rest/bean.go index 4635c3f..a730455 100644 --- a/rest/bean.go +++ b/rest/bean.go @@ -8,6 +8,8 @@ import ( type IBean interface { //初始化方法 Init() + //系统清理方法 + Cleanup() //所有配置都加载完成后调用的方法,包括数据库加载完毕 ConfigPost() //快速的Panic方法 @@ -26,6 +28,11 @@ func (this *Bean) ConfigPost() { } +//系统大清理,一般时产品即将上线时,清除脏数据,只执行一次。 +func (this *Bean) Cleanup() { + +} + //处理错误的统一方法 可以省去if err!=nil 这段代码 func (this *Bean) PanicError(err error) { if err != nil { diff --git a/rest/context.go b/rest/context.go index 4ec4e99..eda2344 100644 --- a/rest/context.go +++ b/rest/context.go @@ -137,6 +137,7 @@ func (this *Context) registerBeans() { //session this.registerBean(new(SessionDao)) + this.registerBean(new(SessionService)) //uploadToken this.registerBean(new(UploadTokenDao)) diff --git a/rest/dashboard_dao.go b/rest/dashboard_dao.go index 7c9ee88..8c16585 100644 --- a/rest/dashboard_dao.go +++ b/rest/dashboard_dao.go @@ -110,3 +110,10 @@ func (this *DashboardDao) ActiveIpTop10() []*DashboardIpTimes { return dashboardIpTimes } + +//执行清理操作 +func (this *DashboardDao) Cleanup() { + this.logger.Info("[DashboardDao]执行清理:清除数据库中所有Dashboard记录。") + db := CONTEXT.DB.Where("uuid is not null").Delete(Dashboard{}) + this.PanicError(db.Error) +} diff --git a/rest/download_token_dao.go b/rest/download_token_dao.go index 0c2ca28..9993932 100644 --- a/rest/download_token_dao.go +++ b/rest/download_token_dao.go @@ -58,3 +58,10 @@ func (this *DownloadTokenDao) Save(downloadToken *DownloadToken) *DownloadToken return downloadToken } + +//执行清理操作 +func (this *DownloadTokenDao) Cleanup() { + this.logger.Info("[DownloadTokenDao]执行清理:清除数据库中所有DownloadToken记录。") + db := CONTEXT.DB.Where("uuid is not null").Delete(DownloadToken{}) + this.PanicError(db.Error) +} diff --git a/rest/footprint_dao.go b/rest/footprint_dao.go index cd9408b..a32d320 100644 --- a/rest/footprint_dao.go +++ b/rest/footprint_dao.go @@ -118,3 +118,10 @@ func (this *FootprintDao) AvgCostBetweenTime(startTime time.Time, endTime time.T return int64(cost) } + +//执行清理操作 +func (this *FootprintDao) Cleanup() { + this.logger.Info("[FootprintDao]执行清理:清除数据库中所有Footprint记录。") + db := CONTEXT.DB.Where("uuid is not null").Delete(Footprint{}) + this.PanicError(db.Error) +} diff --git a/rest/image_cache_dao.go b/rest/image_cache_dao.go index d8cb95f..c81f7e1 100644 --- a/rest/image_cache_dao.go +++ b/rest/image_cache_dao.go @@ -197,3 +197,12 @@ func (this *ImageCacheDao) SizeBetweenTime(startTime time.Time, endTime time.Tim row.Scan(&size) return size } + + +//执行清理操作 +func (this *ImageCacheDao) Cleanup() { + this.logger.Info("[ImageCacheDao]执行清理:清除数据库中所有ImageCache记录。") + db := CONTEXT.DB.Where("uuid is not null").Delete(ImageCache{}) + this.PanicError(db.Error) +} + diff --git a/rest/matter_dao.go b/rest/matter_dao.go index 4bc433b..96ac988 100644 --- a/rest/matter_dao.go +++ b/rest/matter_dao.go @@ -246,7 +246,6 @@ func (this *MatterDao) Delete(matter *Matter) { //从磁盘中删除该文件夹。 DeleteEmptyDir(matter.AbsolutePath()) - } else { //删除数据库中文件记录 @@ -287,3 +286,14 @@ func (this *MatterDao) SizeBetweenTime(startTime time.Time, endTime time.Time) i this.PanicError(err) return size } + +//执行清理操作 +func (this *MatterDao) Cleanup() { + this.logger.Info("[MatterDao]执行清理:清除数据库中所有Matter记录。删除磁盘中所有Matter文件。") + db := CONTEXT.DB.Where("uuid is not null").Delete(Matter{}) + this.PanicError(db.Error) + + err := os.RemoveAll(CONFIG.MatterPath) + this.PanicError(err) + +} diff --git a/rest/matter_service.go b/rest/matter_service.go index 224958c..3c9b26f 100644 --- a/rest/matter_service.go +++ b/rest/matter_service.go @@ -159,6 +159,9 @@ func (this *MatterService) Upload(file multipart.File, user *User, puuid string, fileAbsolutePath := dirAbsolutePath + "/" + filename fileRelativePath := dirRelativePath + "/" + filename + //创建父文件夹 + MakeDirAll(fileAbsolutePath) + //如果文件已经存在了,那么直接覆盖。 exist, err := PathExists(fileAbsolutePath) this.PanicError(err) diff --git a/rest/preference_controller.go b/rest/preference_controller.go index a1b046c..22f10b3 100644 --- a/rest/preference_controller.go +++ b/rest/preference_controller.go @@ -35,16 +35,17 @@ func (this *PreferenceController) RegisterRoutes() map[string]func(writer http.R //每个Controller需要主动注册自己的路由。 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) + return routeMap } //查看某个偏好设置的详情。 func (this *PreferenceController) Fetch(writer http.ResponseWriter, request *http.Request) *WebResult { - preference := this.preferenceDao.Fetch() + preference := this.preferenceService.Fetch() return this.Success(preference) - } //修改 @@ -76,5 +77,25 @@ func (this *PreferenceController) Edit(writer http.ResponseWriter, request *http preference = this.preferenceDao.Save(preference) + //重置缓存中的偏好 + this.preferenceService.Reset() + return this.Success(preference) } + +//清扫系统,所有数据全部丢失。一定要非常慎点,非常慎点!只在系统初始化的时候点击! +func (this *PreferenceController) SystemCleanup(writer http.ResponseWriter, request *http.Request) *WebResult { + + user := this.checkUser(writer, request) + password := request.FormValue("password") + + if !MatchBcrypt(password, user.Password) { + this.PanicBadRequest("密码错误,不能执行!") + } + + for _, bean := range CONTEXT.BeanMap { + bean.Cleanup() + } + + return this.Success("OK") +} diff --git a/rest/preference_dao.go b/rest/preference_dao.go index 24c9b31..a992f96 100644 --- a/rest/preference_dao.go +++ b/rest/preference_dao.go @@ -17,7 +17,7 @@ func (this *PreferenceDao) Fetch() *Preference { db := CONTEXT.DB.First(preference) if db.Error != nil { - if db.Error.Error() == "record not found" { + if db.Error.Error() == DB_ERROR_NOT_FOUND { preference.Name = "蓝眼云盘" preference.ShowAlien = true this.Create(preference) @@ -25,7 +25,6 @@ func (this *PreferenceDao) Fetch() *Preference { } else { return nil } - } return preference @@ -54,3 +53,12 @@ func (this *PreferenceDao) Save(preference *Preference) *Preference { return preference } + + +//执行清理操作 +func (this *PreferenceDao) Cleanup() { + + this.logger.Info("[PreferenceDao]执行清理:清除数据库中所有Preference记录。") + db := CONTEXT.DB.Where("uuid is not null").Delete(Preference{}) + this.PanicError(db.Error) +} diff --git a/rest/preference_service.go b/rest/preference_service.go index 3fa6914..e12c6de 100644 --- a/rest/preference_service.go +++ b/rest/preference_service.go @@ -4,6 +4,7 @@ package rest type PreferenceService struct { Bean preferenceDao *PreferenceDao + preference *Preference } //初始化方法 @@ -17,3 +18,28 @@ func (this *PreferenceService) Init() { } } + +//获取单例的配置。 +func (this *PreferenceService) Fetch() *Preference { + + if this.preference == nil { + this.preference = this.preferenceDao.Fetch() + } + + return this.preference +} + +//清空单例配置。 +func (this *PreferenceService) Reset() { + + this.preference = nil + +} + +//执行清理操作 +func (this *PreferenceService) Cleanup() { + + this.logger.Info("[PreferenceService]执行清理:重置缓存中的preference。") + + this.Reset() +} diff --git a/rest/session_dao.go b/rest/session_dao.go index 570777a..e89841a 100644 --- a/rest/session_dao.go +++ b/rest/session_dao.go @@ -10,14 +10,6 @@ type SessionDao struct { BaseDao } -//构造函数 -func NewSessionDao() *SessionDao { - - var sessionDao = &SessionDao{} - sessionDao.Init() - return sessionDao -} - //按照Id查询session. func (this *SessionDao) FindByUuid(uuid string) *Session { @@ -78,3 +70,9 @@ func (this *SessionDao) Delete(uuid string) { } +//执行清理操作 +func (this *SessionDao) Cleanup() { + this.logger.Info("[SessionDao]执行清理:清除数据库中所有Session记录。") + db := CONTEXT.DB.Where("uuid is not null").Delete(Session{}) + this.PanicError(db.Error) +} diff --git a/rest/session_service.go b/rest/session_service.go new file mode 100644 index 0000000..bb39541 --- /dev/null +++ b/rest/session_service.go @@ -0,0 +1,33 @@ +package rest + +//@Service +type SessionService struct { + Bean + userDao *UserDao + sessionDao *SessionDao +} + +//初始化方法 +func (this *SessionService) Init() { + this.Bean.Init() + + //手动装填本实例的Bean. 这里必须要用中间变量方可。 + b := CONTEXT.GetBean(this.userDao) + if b, ok := b.(*UserDao); ok { + this.userDao = b + } + + b = CONTEXT.GetBean(this.sessionDao) + if b, ok := b.(*SessionDao); ok { + this.sessionDao = b + } + +} + +//执行清理操作 +func (this *SessionService) Cleanup() { + + this.logger.Info("[SessionService]执行清理:清除缓存中所有Session记录,共%d条。", CONTEXT.SessionCache.Count()) + + CONTEXT.SessionCache.Truncate() +} diff --git a/rest/user_dao.go b/rest/user_dao.go index 04c81b7..31f2120 100644 --- a/rest/user_dao.go +++ b/rest/user_dao.go @@ -1,7 +1,6 @@ package rest import ( - "github.com/nu7hatch/gouuid" "time" ) @@ -138,3 +137,10 @@ func (this *UserDao) Save(user *User) *User { this.PanicError(db.Error) return user } + +//执行清理操作 +func (this *UserDao) Cleanup() { + this.logger.Info("[UserDao]执行清理:清除数据库中所有User记录。") + db := CONTEXT.DB.Where("uuid is not null and role != ?", USER_ROLE_ADMINISTRATOR).Delete(User{}) + this.PanicError(db.Error) +} diff --git a/rest/web_result.go b/rest/web_result.go index da92487..c0b1e73 100644 --- a/rest/web_result.go +++ b/rest/web_result.go @@ -94,5 +94,5 @@ var ( DB_ERROR_DUPLICATE_KEY = "Error 1062: Duplicate entry" DB_ERROR_NOT_FOUND = "record not found" DB_TOO_MANY_CONNECTIONS = "Error 1040: Too many connections" - DB_BAD_CONNECTION = "driver: bad connection" + DB_BAD_CONNECTION = "driver: bad connection" )