Finish the System Cleanup feature.

This commit is contained in:
zicla 2019-04-04 03:14:47 +08:00
parent 7697891807
commit 5936f917b3
15 changed files with 158 additions and 15 deletions

View File

@ -8,6 +8,8 @@ import (
type IBean interface { type IBean interface {
//初始化方法 //初始化方法
Init() Init()
//系统清理方法
Cleanup()
//所有配置都加载完成后调用的方法,包括数据库加载完毕 //所有配置都加载完成后调用的方法,包括数据库加载完毕
ConfigPost() ConfigPost()
//快速的Panic方法 //快速的Panic方法
@ -26,6 +28,11 @@ func (this *Bean) ConfigPost() {
} }
//系统大清理,一般时产品即将上线时,清除脏数据,只执行一次。
func (this *Bean) Cleanup() {
}
//处理错误的统一方法 可以省去if err!=nil 这段代码 //处理错误的统一方法 可以省去if err!=nil 这段代码
func (this *Bean) PanicError(err error) { func (this *Bean) PanicError(err error) {
if err != nil { if err != nil {

View File

@ -137,6 +137,7 @@ func (this *Context) registerBeans() {
//session //session
this.registerBean(new(SessionDao)) this.registerBean(new(SessionDao))
this.registerBean(new(SessionService))
//uploadToken //uploadToken
this.registerBean(new(UploadTokenDao)) this.registerBean(new(UploadTokenDao))

View File

@ -110,3 +110,10 @@ func (this *DashboardDao) ActiveIpTop10() []*DashboardIpTimes {
return 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)
}

View File

@ -58,3 +58,10 @@ func (this *DownloadTokenDao) Save(downloadToken *DownloadToken) *DownloadToken
return 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)
}

View File

@ -118,3 +118,10 @@ func (this *FootprintDao) AvgCostBetweenTime(startTime time.Time, endTime time.T
return int64(cost) 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)
}

View File

@ -197,3 +197,12 @@ func (this *ImageCacheDao) SizeBetweenTime(startTime time.Time, endTime time.Tim
row.Scan(&size) row.Scan(&size)
return 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)
}

View File

@ -246,7 +246,6 @@ func (this *MatterDao) Delete(matter *Matter) {
//从磁盘中删除该文件夹。 //从磁盘中删除该文件夹。
DeleteEmptyDir(matter.AbsolutePath()) DeleteEmptyDir(matter.AbsolutePath())
} else { } else {
//删除数据库中文件记录 //删除数据库中文件记录
@ -287,3 +286,14 @@ func (this *MatterDao) SizeBetweenTime(startTime time.Time, endTime time.Time) i
this.PanicError(err) this.PanicError(err)
return size 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)
}

View File

@ -159,6 +159,9 @@ func (this *MatterService) Upload(file multipart.File, user *User, puuid string,
fileAbsolutePath := dirAbsolutePath + "/" + filename fileAbsolutePath := dirAbsolutePath + "/" + filename
fileRelativePath := dirRelativePath + "/" + filename fileRelativePath := dirRelativePath + "/" + filename
//创建父文件夹
MakeDirAll(fileAbsolutePath)
//如果文件已经存在了,那么直接覆盖。 //如果文件已经存在了,那么直接覆盖。
exist, err := PathExists(fileAbsolutePath) exist, err := PathExists(fileAbsolutePath)
this.PanicError(err) this.PanicError(err)

View File

@ -35,16 +35,17 @@ func (this *PreferenceController) RegisterRoutes() map[string]func(writer http.R
//每个Controller需要主动注册自己的路由。 //每个Controller需要主动注册自己的路由。
routeMap["/api/preference/fetch"] = this.Wrap(this.Fetch, 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/edit"] = this.Wrap(this.Edit, USER_ROLE_ADMINISTRATOR)
routeMap["/api/preference/system_cleanup"] = this.Wrap(this.SystemCleanup, USER_ROLE_ADMINISTRATOR)
return routeMap return routeMap
} }
//查看某个偏好设置的详情。 //查看某个偏好设置的详情。
func (this *PreferenceController) Fetch(writer http.ResponseWriter, request *http.Request) *WebResult { func (this *PreferenceController) Fetch(writer http.ResponseWriter, request *http.Request) *WebResult {
preference := this.preferenceDao.Fetch() preference := this.preferenceService.Fetch()
return this.Success(preference) return this.Success(preference)
} }
//修改 //修改
@ -76,5 +77,25 @@ func (this *PreferenceController) Edit(writer http.ResponseWriter, request *http
preference = this.preferenceDao.Save(preference) preference = this.preferenceDao.Save(preference)
//重置缓存中的偏好
this.preferenceService.Reset()
return this.Success(preference) 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")
}

View File

@ -17,7 +17,7 @@ func (this *PreferenceDao) Fetch() *Preference {
db := CONTEXT.DB.First(preference) db := CONTEXT.DB.First(preference)
if db.Error != nil { if db.Error != nil {
if db.Error.Error() == "record not found" { if db.Error.Error() == DB_ERROR_NOT_FOUND {
preference.Name = "蓝眼云盘" preference.Name = "蓝眼云盘"
preference.ShowAlien = true preference.ShowAlien = true
this.Create(preference) this.Create(preference)
@ -25,7 +25,6 @@ func (this *PreferenceDao) Fetch() *Preference {
} else { } else {
return nil return nil
} }
} }
return preference return preference
@ -54,3 +53,12 @@ func (this *PreferenceDao) Save(preference *Preference) *Preference {
return 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)
}

View File

@ -4,6 +4,7 @@ package rest
type PreferenceService struct { type PreferenceService struct {
Bean Bean
preferenceDao *PreferenceDao 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()
}

View File

@ -10,14 +10,6 @@ type SessionDao struct {
BaseDao BaseDao
} }
//构造函数
func NewSessionDao() *SessionDao {
var sessionDao = &SessionDao{}
sessionDao.Init()
return sessionDao
}
//按照Id查询session. //按照Id查询session.
func (this *SessionDao) FindByUuid(uuid string) *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)
}

33
rest/session_service.go Normal file
View File

@ -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()
}

View File

@ -1,7 +1,6 @@
package rest package rest
import ( import (
"github.com/nu7hatch/gouuid" "github.com/nu7hatch/gouuid"
"time" "time"
) )
@ -138,3 +137,10 @@ func (this *UserDao) Save(user *User) *User {
this.PanicError(db.Error) this.PanicError(db.Error)
return user 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)
}