Finish all the work of physics file.
This commit is contained in:
@ -151,7 +151,7 @@ func (this *ImageCacheDao) deleteFileAndDir(imageCache *ImageCache) {
|
||||
}
|
||||
|
||||
//如果这一层文件夹是空的,那么删除文件夹本身。
|
||||
DeleteEmptyDir(dirPath)
|
||||
DeleteEmptyDirRecursive(dirPath)
|
||||
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,7 @@ type ImageCache struct {
|
||||
UserUuid string `json:"userUuid" gorm:"type:char(36)"`
|
||||
Username string `json:"username" gorm:"type:varchar(45) not null"`
|
||||
MatterUuid string `json:"matterUuid" gorm:"type:char(36);index:idx_mu"`
|
||||
MatterName string `json:"matterName" gorm:"type:varchar(255) not null"`
|
||||
Mode string `json:"mode" gorm:"type:varchar(512)"`
|
||||
Md5 string `json:"md5" gorm:"type:varchar(45)"`
|
||||
Size int64 `json:"size" gorm:"type:bigint(20) not null;default:0"`
|
||||
|
@ -232,6 +232,7 @@ func (this *ImageCacheService) cacheImage(writer http.ResponseWriter, request *h
|
||||
UserUuid: matter.UserUuid,
|
||||
Username: user.Username,
|
||||
MatterUuid: matter.Uuid,
|
||||
MatterName: matter.Name,
|
||||
Mode: mode,
|
||||
Size: fileInfo.Size(),
|
||||
Path: cacheImageRelativePath,
|
||||
@ -240,21 +241,3 @@ func (this *ImageCacheService) cacheImage(writer http.ResponseWriter, request *h
|
||||
|
||||
return imageCache
|
||||
}
|
||||
|
||||
//删除一个Matter的所有缓存文件。如果matter是文件夹,那么就删除该文件夹下的所有文件的缓存文件。
|
||||
func (this *ImageCacheService) Delete(matter *Matter) {
|
||||
|
||||
//目录的话递归删除。
|
||||
if matter.Dir {
|
||||
matters := this.matterDao.List(matter.Uuid, matter.UserUuid, nil)
|
||||
|
||||
for _, f := range matters {
|
||||
this.Delete(f)
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
this.imageCacheDao.DeleteByMatterUuid(matter.Uuid)
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -432,13 +432,7 @@ func (this *MatterController) Rename(writer http.ResponseWriter, request *http.R
|
||||
this.PanicBadRequest("【" + name + "】已经存在了,请使用其他名称。")
|
||||
}
|
||||
|
||||
matter.Name = name
|
||||
matter = this.matterDao.Save(matter)
|
||||
|
||||
//删除对应的缓存图片。
|
||||
if !matter.Dir {
|
||||
this.imageCacheDao.DeleteByMatterUuid(matter.Uuid)
|
||||
}
|
||||
this.matterService.Rename(matter, name)
|
||||
|
||||
return this.Success(matter)
|
||||
}
|
||||
|
@ -2,8 +2,6 @@ package rest
|
||||
|
||||
import (
|
||||
"github.com/jinzhu/gorm"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/nu7hatch/gouuid"
|
||||
"os"
|
||||
"time"
|
||||
@ -261,9 +259,7 @@ func (this *MatterDao) Delete(matter *Matter) {
|
||||
this.logger.Error("删除磁盘上的文件出错 %s", err.Error())
|
||||
}
|
||||
|
||||
//如果目录为空,那么一并删除
|
||||
dirPath := filepath.Dir(matter.AbsolutePath())
|
||||
DeleteEmptyDir(dirPath)
|
||||
//由于目录和物理结构一一对应,这里不能删除上级文件夹。
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ type MatterService struct {
|
||||
Bean
|
||||
matterDao *MatterDao
|
||||
userDao *UserDao
|
||||
imageCacheDao *ImageCacheDao
|
||||
imageCacheService *ImageCacheService
|
||||
}
|
||||
|
||||
@ -38,6 +39,11 @@ func (this *MatterService) Init() {
|
||||
this.userDao = b
|
||||
}
|
||||
|
||||
b = CONTEXT.GetBean(this.imageCacheDao)
|
||||
if b, ok := b.(*ImageCacheDao); ok {
|
||||
this.imageCacheDao = b
|
||||
}
|
||||
|
||||
b = CONTEXT.GetBean(this.imageCacheService)
|
||||
if b, ok := b.(*ImageCacheService); ok {
|
||||
this.imageCacheService = b
|
||||
@ -644,7 +650,6 @@ func (this *MatterService) adjustPath(matter *Matter, parentMatter *Matter) {
|
||||
|
||||
//调整该文件夹下文件的Path.
|
||||
matters := this.matterDao.List(matter.Uuid, matter.UserUuid, nil)
|
||||
|
||||
for _, m := range matters {
|
||||
this.adjustPath(m, matter)
|
||||
}
|
||||
@ -652,9 +657,12 @@ func (this *MatterService) adjustPath(matter *Matter, parentMatter *Matter) {
|
||||
} else {
|
||||
//如果源是普通文件
|
||||
|
||||
//删除该文件的所有缓存
|
||||
this.imageCacheDao.DeleteByMatterUuid(matter.Uuid)
|
||||
|
||||
//调整path
|
||||
matter.Path = parentMatter.Path + "/" + matter.Name
|
||||
matter = this.matterDao.Save(matter)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -671,6 +679,7 @@ func (this *MatterService) Move(srcMatter *Matter, destMatter *Matter) {
|
||||
destAbsolutePath := destMatter.AbsolutePath() + "/" + srcMatter.Name
|
||||
srcAbsolutePath := srcMatter.AbsolutePath()
|
||||
|
||||
//物理文件一口气移动
|
||||
err := os.Rename(srcAbsolutePath, destAbsolutePath)
|
||||
this.PanicError(err)
|
||||
|
||||
@ -681,8 +690,6 @@ func (this *MatterService) Move(srcMatter *Matter, destMatter *Matter) {
|
||||
|
||||
//调整该文件夹下文件的Path.
|
||||
matters := this.matterDao.List(srcMatter.Uuid, srcMatter.UserUuid, nil)
|
||||
|
||||
//调整该文件夹下面所有文件的Path值
|
||||
for _, m := range matters {
|
||||
this.adjustPath(m, srcMatter)
|
||||
}
|
||||
@ -693,11 +700,12 @@ func (this *MatterService) Move(srcMatter *Matter, destMatter *Matter) {
|
||||
destAbsolutePath := destMatter.AbsolutePath() + "/" + srcMatter.Name
|
||||
srcAbsolutePath := srcMatter.AbsolutePath()
|
||||
|
||||
//物理文件进行移动
|
||||
err := os.Rename(srcAbsolutePath, destAbsolutePath)
|
||||
this.PanicError(err)
|
||||
|
||||
//删除对应的缓存。
|
||||
this.imageCacheService.Delete(srcMatter)
|
||||
this.imageCacheDao.DeleteByMatterUuid(srcMatter.Uuid)
|
||||
|
||||
//修改数据库中信息
|
||||
srcMatter.Puuid = destMatter.Uuid
|
||||
@ -708,3 +716,54 @@ func (this *MatterService) Move(srcMatter *Matter, destMatter *Matter) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
//将一个srcMatter 重命名为 name
|
||||
func (this *MatterService) Rename(srcMatter *Matter, name string) {
|
||||
|
||||
if srcMatter.Dir {
|
||||
//如果源是文件夹
|
||||
|
||||
oldAbsolutePath := srcMatter.AbsolutePath()
|
||||
absoluteDirPath := GetDirOfPath(oldAbsolutePath)
|
||||
relativeDirPath := GetDirOfPath(srcMatter.Path)
|
||||
newAbsolutePath := absoluteDirPath + "/" + name
|
||||
|
||||
//物理文件一口气移动
|
||||
err := os.Rename(oldAbsolutePath, newAbsolutePath)
|
||||
this.PanicError(err)
|
||||
|
||||
//修改数据库中信息
|
||||
srcMatter.Name = name
|
||||
srcMatter.Path = relativeDirPath + "/" + name
|
||||
srcMatter = this.matterDao.Save(srcMatter)
|
||||
|
||||
//调整该文件夹下文件的Path.
|
||||
matters := this.matterDao.List(srcMatter.Uuid, srcMatter.UserUuid, nil)
|
||||
for _, m := range matters {
|
||||
this.adjustPath(m, srcMatter)
|
||||
}
|
||||
|
||||
} else {
|
||||
//如果源是普通文件
|
||||
|
||||
oldAbsolutePath := srcMatter.AbsolutePath()
|
||||
absoluteDirPath := GetDirOfPath(oldAbsolutePath)
|
||||
relativeDirPath := GetDirOfPath(srcMatter.Path)
|
||||
newAbsolutePath := absoluteDirPath + "/" + name
|
||||
|
||||
//物理文件进行移动
|
||||
err := os.Rename(oldAbsolutePath, newAbsolutePath)
|
||||
this.PanicError(err)
|
||||
|
||||
//删除对应的缓存。
|
||||
this.imageCacheDao.DeleteByMatterUuid(srcMatter.Uuid)
|
||||
|
||||
//修改数据库中信息
|
||||
srcMatter.Name = name
|
||||
srcMatter.Path = relativeDirPath + "/" + name
|
||||
srcMatter = this.matterDao.Save(srcMatter)
|
||||
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -92,23 +92,54 @@ func MakeDirAll(dirPath string) string {
|
||||
return dirPath
|
||||
}
|
||||
|
||||
//尝试删除空文件夹
|
||||
func DeleteEmptyDir(dirPath string) {
|
||||
//获取到一个Path的文件夹路径,eg /var/www/xx.log -> /var/www
|
||||
func GetDirOfPath(fullPath string) string {
|
||||
|
||||
index1 := strings.LastIndex(fullPath, "/")
|
||||
//可能是windows的环境
|
||||
index2 := strings.LastIndex(fullPath, "\\")
|
||||
index := index1
|
||||
if index2 > index1 {
|
||||
index = index2
|
||||
}
|
||||
|
||||
return fullPath[:index]
|
||||
}
|
||||
|
||||
//尝试删除空文件夹 true表示删掉了一个空文件夹,false表示没有删掉任何东西
|
||||
func DeleteEmptyDir(dirPath string) bool {
|
||||
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())
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
LOGGER.Info("文件夹不为空,%v", len(dir))
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//递归尝试删除空文件夹,一直空就一直删,直到不空为止
|
||||
func DeleteEmptyDirRecursive(dirPath string) {
|
||||
|
||||
fmt.Printf("递归删除删 %v \n", dirPath)
|
||||
|
||||
tmpPath := dirPath
|
||||
for DeleteEmptyDir(tmpPath) {
|
||||
|
||||
dir := GetDirOfPath(tmpPath)
|
||||
|
||||
fmt.Printf("尝试删除 %v\n", dir)
|
||||
|
||||
tmpPath = dir
|
||||
}
|
||||
}
|
||||
|
||||
//移除某个文件夹。 例如:/var/www/matter => /var/www
|
||||
|
Reference in New Issue
Block a user