Finish the delete things for matter and cache.
This commit is contained in:
parent
28b4227215
commit
7697891807
@ -128,7 +128,7 @@ func (this *AlienService) PreviewOrDownload(
|
|||||||
this.matterService.DownloadFile(writer, request, GetUserCacheRootDir(imageCache.Username)+imageCache.Path, imageCache.Name, withContentDisposition)
|
this.matterService.DownloadFile(writer, request, GetUserCacheRootDir(imageCache.Username)+imageCache.Path, imageCache.Name, withContentDisposition)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
this.matterService.DownloadFile(writer, request, GetUserFileRootDir(matter.Username)+matter.Path, matter.Name, withContentDisposition)
|
this.matterService.DownloadFile(writer, request, matter.AbsolutePath(), matter.Name, withContentDisposition)
|
||||||
}
|
}
|
||||||
|
|
||||||
//文件下载次数加一,为了加快访问速度,异步进行
|
//文件下载次数加一,为了加快访问速度,异步进行
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
"github.com/nu7hatch/gouuid"
|
"github.com/nu7hatch/gouuid"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -140,10 +140,9 @@ func (this *ImageCacheDao) Save(imageCache *ImageCache) *ImageCache {
|
|||||||
//删除一个文件包括文件夹
|
//删除一个文件包括文件夹
|
||||||
func (this *ImageCacheDao) deleteFileAndDir(imageCache *ImageCache) {
|
func (this *ImageCacheDao) deleteFileAndDir(imageCache *ImageCache) {
|
||||||
|
|
||||||
filePath := CONFIG.MatterPath + imageCache.Path
|
filePath := GetUserCacheRootDir(imageCache.Username) + imageCache.Path
|
||||||
//递归找寻文件的上级目录uuid. 因为是/开头的缘故
|
|
||||||
parts := strings.Split(imageCache.Path, "/")
|
dirPath := filepath.Dir(filePath)
|
||||||
dirPath := CONFIG.MatterPath + "/" + parts[1] + "/" + parts[2] + "/" + parts[3] + "/" + parts[4]
|
|
||||||
|
|
||||||
//删除文件
|
//删除文件
|
||||||
err := os.Remove(filePath)
|
err := os.Remove(filePath)
|
||||||
@ -151,11 +150,9 @@ func (this *ImageCacheDao) deleteFileAndDir(imageCache *ImageCache) {
|
|||||||
this.logger.Error(fmt.Sprintf("删除磁盘上的文件%s出错 %s", filePath, err.Error()))
|
this.logger.Error(fmt.Sprintf("删除磁盘上的文件%s出错 %s", filePath, err.Error()))
|
||||||
}
|
}
|
||||||
|
|
||||||
//删除这一层文件夹
|
//如果这一层文件夹是空的,那么删除文件夹本身。
|
||||||
err = os.Remove(dirPath)
|
DeleteEmptyDir(dirPath)
|
||||||
if err != nil {
|
|
||||||
this.logger.Error(fmt.Sprintf("删除磁盘上的文件夹%s出错 %s", dirPath, err.Error()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//删除一个文件,数据库中删除,物理磁盘上删除。
|
//删除一个文件,数据库中删除,物理磁盘上删除。
|
||||||
|
@ -20,3 +20,8 @@ type ImageCache struct {
|
|||||||
func (this *ImageCache) TableName() string {
|
func (this *ImageCache) TableName() string {
|
||||||
return TABLE_PREFIX + "image_cache"
|
return TABLE_PREFIX + "image_cache"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取该ImageCache的绝对路径。path代表的是相对路径。
|
||||||
|
func (this *ImageCache) AbsolutePath() string {
|
||||||
|
return GetUserCacheRootDir(this.Username) + this.Path
|
||||||
|
}
|
||||||
|
@ -195,7 +195,7 @@ func (this *ImageCacheService) cacheImage(writer http.ResponseWriter, request *h
|
|||||||
user := this.userDao.FindByUuid(matter.UserUuid)
|
user := this.userDao.FindByUuid(matter.UserUuid)
|
||||||
|
|
||||||
//resize图片
|
//resize图片
|
||||||
dstImage := this.ResizeImage(request, GetUserFileRootDir(user.Username)+matter.Path)
|
dstImage := this.ResizeImage(request, matter.AbsolutePath())
|
||||||
|
|
||||||
cacheImageName := GetSimpleFileName(matter.Name) + "_" + mode + extension
|
cacheImageName := GetSimpleFileName(matter.Name) + "_" + mode + extension
|
||||||
cacheImageRelativePath := GetSimpleFileName(matter.Path) + "_" + mode + extension
|
cacheImageRelativePath := GetSimpleFileName(matter.Path) + "_" + mode + extension
|
||||||
|
@ -2,10 +2,10 @@ package rest
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/nu7hatch/gouuid"
|
"github.com/nu7hatch/gouuid"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -239,17 +239,16 @@ func (this *MatterDao) Delete(matter *Matter) {
|
|||||||
this.Delete(f)
|
this.Delete(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
//从磁盘中删除该文件夹。
|
//删除数据库中文件夹本身
|
||||||
removeError := os.Remove(CONFIG.MatterPath + matter.Path)
|
|
||||||
if removeError != nil {
|
|
||||||
this.logger.Error("从磁盘上删除文件夹%s出错:%s", CONFIG.MatterPath+matter.Path, removeError.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
//删除文件夹本身
|
|
||||||
db := CONTEXT.DB.Delete(&matter)
|
db := CONTEXT.DB.Delete(&matter)
|
||||||
this.PanicError(db.Error)
|
this.PanicError(db.Error)
|
||||||
|
|
||||||
|
//从磁盘中删除该文件夹。
|
||||||
|
DeleteEmptyDir(matter.AbsolutePath())
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
//删除数据库中文件记录
|
//删除数据库中文件记录
|
||||||
db := CONTEXT.DB.Delete(&matter)
|
db := CONTEXT.DB.Delete(&matter)
|
||||||
this.PanicError(db.Error)
|
this.PanicError(db.Error)
|
||||||
@ -257,25 +256,15 @@ func (this *MatterDao) Delete(matter *Matter) {
|
|||||||
//删除对应的缓存图片。
|
//删除对应的缓存图片。
|
||||||
this.imageCacheDao.DeleteByMatterUuid(matter.Uuid)
|
this.imageCacheDao.DeleteByMatterUuid(matter.Uuid)
|
||||||
|
|
||||||
filePath := CONFIG.MatterPath + matter.Path
|
|
||||||
|
|
||||||
|
|
||||||
//TODO: do it here.
|
|
||||||
//递归找寻文件的上级目录uuid. 因为是/开头的缘故
|
|
||||||
parts := strings.Split(matter.Path, "/")
|
|
||||||
dirPath := CONFIG.MatterPath + "/" + parts[1] + "/" + parts[2] + "/" + parts[3]
|
|
||||||
|
|
||||||
//删除文件
|
//删除文件
|
||||||
err := os.Remove(filePath)
|
err := os.Remove(matter.AbsolutePath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
this.logger.Error("删除磁盘上的文件出错 %s", err.Error())
|
this.logger.Error("删除磁盘上的文件出错 %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
//删除这一层文件夹
|
//如果目录为空,那么一并删除
|
||||||
err = os.Remove(dirPath)
|
dirPath := filepath.Dir(matter.AbsolutePath())
|
||||||
if err != nil {
|
DeleteEmptyDir(dirPath)
|
||||||
this.logger.Error("删除磁盘上的文件夹出错 %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -294,6 +283,7 @@ func (this *MatterDao) SizeBetweenTime(startTime time.Time, endTime time.Time) i
|
|||||||
db := CONTEXT.DB.Model(&Matter{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("SUM(size)")
|
db := CONTEXT.DB.Model(&Matter{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("SUM(size)")
|
||||||
this.PanicError(db.Error)
|
this.PanicError(db.Error)
|
||||||
row := db.Row()
|
row := db.Row()
|
||||||
row.Scan(&size)
|
err := row.Scan(&size)
|
||||||
|
this.PanicError(err)
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
|
@ -29,8 +29,7 @@ func (Matter) TableName() string {
|
|||||||
return TABLE_PREFIX + "matter"
|
return TABLE_PREFIX + "matter"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取该Matter的绝对路径。path代表的是相对路径。
|
||||||
// 获取该Matter的绝对路径。path代表的是相对路径。相对路径的根目录是 CONFIG.MatterPath
|
|
||||||
func (this *Matter) AbsolutePath() string {
|
func (this *Matter) AbsolutePath() string {
|
||||||
return CONFIG.MatterPath + this.Path
|
return GetUserFileRootDir(this.Username) + this.Path
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package rest
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/build"
|
"go/build"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -91,6 +92,25 @@ func MakeDirAll(dirPath string) string {
|
|||||||
return dirPath
|
return dirPath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//尝试删除空文件夹
|
||||||
|
func DeleteEmptyDir(dirPath string) {
|
||||||
|
dir, err := ioutil.ReadDir(dirPath)
|
||||||
|
if err != nil {
|
||||||
|
LOGGER.Error("尝试读取目录%s时出错 %s", dirPath, err.Error())
|
||||||
|
panic("尝试读取目录时出错 " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(dir) == 0 {
|
||||||
|
//空文件夹
|
||||||
|
err = os.Remove(dirPath)
|
||||||
|
if err != nil {
|
||||||
|
LOGGER.Error("删除磁盘上的文件夹%s出错 %s", dirPath, err.Error())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LOGGER.Info("文件夹不为空,%v", len(dir))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//移除某个文件夹。 例如:/var/www/matter => /var/www
|
//移除某个文件夹。 例如:/var/www/matter => /var/www
|
||||||
func RemoveDirectory(dirPath string) string {
|
func RemoveDirectory(dirPath string) string {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user