Add the delete logic for image cache.

This commit is contained in:
zicla
2018-11-26 12:32:48 +08:00
parent 3ae6dd917b
commit f83dec5f80
3 changed files with 43 additions and 6 deletions

View File

@ -141,6 +141,31 @@ func (this *ImageCacheDao) Delete(imageCache *ImageCache) {
if err != nil { if err != nil {
LogError(fmt.Sprintf("删除磁盘上的文件出错,不做任何处理")) LogError(fmt.Sprintf("删除磁盘上的文件出错,不做任何处理"))
//this.PanicError(err)
} }
} }
//删除一个matter对应的所有缓存
func (this *ImageCacheDao) DeleteByMatterUuid(matterUuid string) {
var wp = &WherePair{}
wp = wp.And(&WherePair{Query: "matter_uuid = ?", Args: []interface{}{matterUuid}})
//查询出即将删除的图片缓存
var imageCaches []*ImageCache
db := this.context.DB.Where(wp.Query, wp.Args).Find(&imageCaches)
this.PanicError(db.Error)
//删除文件记录
db = this.context.DB.Where(wp.Query, wp.Args).Delete(ImageCache{})
this.PanicError(db.Error)
//删除文件实体
for _, imageCache := range imageCaches {
err := os.Remove(CONFIG.MatterPath + imageCache.Path)
if err != nil {
LogError(fmt.Sprintf("删除磁盘上的文件出错,不做任何处理"))
}
}
}

View File

@ -7,7 +7,7 @@ type ImageCache struct {
Base Base
UserUuid string `json:"userUuid"` UserUuid string `json:"userUuid"`
MatterUuid string `json:"matterUuid"` MatterUuid string `json:"matterUuid"`
Uri string `json:"name"` Uri string `json:"uri"`
Md5 string `json:"md5"` Md5 string `json:"md5"`
Size int64 `json:"size"` Size int64 `json:"size"`
Path string `json:"path"` Path string `json:"path"`

View File

@ -11,6 +11,18 @@ import (
type MatterDao struct { type MatterDao struct {
BaseDao BaseDao
imageCacheDao *ImageCacheDao
}
//初始化方法
func (this *MatterDao) Init(context *Context) {
this.BaseDao.Init(context)
//手动装填本实例的Bean. 这里必须要用中间变量方可。
b := context.GetBean(this.imageCacheDao)
if b, ok := b.(*ImageCacheDao); ok {
this.imageCacheDao = b
}
} }
//按照Id查询文件 //按照Id查询文件
@ -22,9 +34,7 @@ func (this *MatterDao) FindByUuid(uuid string) *Matter {
if db.Error != nil { if db.Error != nil {
return nil return nil
} }
return &matter return &matter
} }
//按照Id查询文件 //按照Id查询文件
@ -214,7 +224,6 @@ func (this *MatterDao) Delete(matter *Matter) {
for _, f := range matters { for _, f := range matters {
this.Delete(f) this.Delete(f)
} }
//删除文件夹本身 //删除文件夹本身
@ -222,15 +231,18 @@ func (this *MatterDao) Delete(matter *Matter) {
this.PanicError(db.Error) this.PanicError(db.Error)
} else { } else {
//删除数据库中文件记录
db := this.context.DB.Delete(&matter) db := this.context.DB.Delete(&matter)
this.PanicError(db.Error) this.PanicError(db.Error)
//删除对应的缓存图片。
this.imageCacheDao.DeleteByMatterUuid(matter.Uuid)
//删除文件 //删除文件
err := os.Remove(CONFIG.MatterPath + matter.Path) err := os.Remove(CONFIG.MatterPath + matter.Path)
if err != nil { if err != nil {
LogError(fmt.Sprintf("删除磁盘上的文件出错,不做任何处理")) LogError(fmt.Sprintf("删除磁盘上的文件出错,不做任何处理"))
//this.PanicError(err)
} }
} }