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)"`
|
UserUuid string `json:"userUuid" gorm:"type:char(36)"`
|
||||||
Username string `json:"username" gorm:"type:varchar(45) not null"`
|
Username string `json:"username" gorm:"type:varchar(45) not null"`
|
||||||
MatterUuid string `json:"matterUuid" gorm:"type:char(36);index:idx_mu"`
|
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)"`
|
Mode string `json:"mode" gorm:"type:varchar(512)"`
|
||||||
Md5 string `json:"md5" gorm:"type:varchar(45)"`
|
Md5 string `json:"md5" gorm:"type:varchar(45)"`
|
||||||
Size int64 `json:"size" gorm:"type:bigint(20) not null;default:0"`
|
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,
|
UserUuid: matter.UserUuid,
|
||||||
Username: user.Username,
|
Username: user.Username,
|
||||||
MatterUuid: matter.Uuid,
|
MatterUuid: matter.Uuid,
|
||||||
|
MatterName: matter.Name,
|
||||||
Mode: mode,
|
Mode: mode,
|
||||||
Size: fileInfo.Size(),
|
Size: fileInfo.Size(),
|
||||||
Path: cacheImageRelativePath,
|
Path: cacheImageRelativePath,
|
||||||
@ -240,21 +241,3 @@ func (this *ImageCacheService) cacheImage(writer http.ResponseWriter, request *h
|
|||||||
|
|
||||||
return imageCache
|
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 + "】已经存在了,请使用其他名称。")
|
this.PanicBadRequest("【" + name + "】已经存在了,请使用其他名称。")
|
||||||
}
|
}
|
||||||
|
|
||||||
matter.Name = name
|
this.matterService.Rename(matter, name)
|
||||||
matter = this.matterDao.Save(matter)
|
|
||||||
|
|
||||||
//删除对应的缓存图片。
|
|
||||||
if !matter.Dir {
|
|
||||||
this.imageCacheDao.DeleteByMatterUuid(matter.Uuid)
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.Success(matter)
|
return this.Success(matter)
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,6 @@ package rest
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/nu7hatch/gouuid"
|
"github.com/nu7hatch/gouuid"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@ -261,9 +259,7 @@ func (this *MatterDao) Delete(matter *Matter) {
|
|||||||
this.logger.Error("删除磁盘上的文件出错 %s", err.Error())
|
this.logger.Error("删除磁盘上的文件出错 %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
//如果目录为空,那么一并删除
|
//由于目录和物理结构一一对应,这里不能删除上级文件夹。
|
||||||
dirPath := filepath.Dir(matter.AbsolutePath())
|
|
||||||
DeleteEmptyDir(dirPath)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ type MatterService struct {
|
|||||||
Bean
|
Bean
|
||||||
matterDao *MatterDao
|
matterDao *MatterDao
|
||||||
userDao *UserDao
|
userDao *UserDao
|
||||||
|
imageCacheDao *ImageCacheDao
|
||||||
imageCacheService *ImageCacheService
|
imageCacheService *ImageCacheService
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,6 +39,11 @@ func (this *MatterService) Init() {
|
|||||||
this.userDao = b
|
this.userDao = b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b = CONTEXT.GetBean(this.imageCacheDao)
|
||||||
|
if b, ok := b.(*ImageCacheDao); ok {
|
||||||
|
this.imageCacheDao = b
|
||||||
|
}
|
||||||
|
|
||||||
b = CONTEXT.GetBean(this.imageCacheService)
|
b = CONTEXT.GetBean(this.imageCacheService)
|
||||||
if b, ok := b.(*ImageCacheService); ok {
|
if b, ok := b.(*ImageCacheService); ok {
|
||||||
this.imageCacheService = b
|
this.imageCacheService = b
|
||||||
@ -644,7 +650,6 @@ func (this *MatterService) adjustPath(matter *Matter, parentMatter *Matter) {
|
|||||||
|
|
||||||
//调整该文件夹下文件的Path.
|
//调整该文件夹下文件的Path.
|
||||||
matters := this.matterDao.List(matter.Uuid, matter.UserUuid, nil)
|
matters := this.matterDao.List(matter.Uuid, matter.UserUuid, nil)
|
||||||
|
|
||||||
for _, m := range matters {
|
for _, m := range matters {
|
||||||
this.adjustPath(m, matter)
|
this.adjustPath(m, matter)
|
||||||
}
|
}
|
||||||
@ -652,9 +657,12 @@ func (this *MatterService) adjustPath(matter *Matter, parentMatter *Matter) {
|
|||||||
} else {
|
} else {
|
||||||
//如果源是普通文件
|
//如果源是普通文件
|
||||||
|
|
||||||
|
//删除该文件的所有缓存
|
||||||
|
this.imageCacheDao.DeleteByMatterUuid(matter.Uuid)
|
||||||
|
|
||||||
|
//调整path
|
||||||
matter.Path = parentMatter.Path + "/" + matter.Name
|
matter.Path = parentMatter.Path + "/" + matter.Name
|
||||||
matter = this.matterDao.Save(matter)
|
matter = this.matterDao.Save(matter)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -671,6 +679,7 @@ func (this *MatterService) Move(srcMatter *Matter, destMatter *Matter) {
|
|||||||
destAbsolutePath := destMatter.AbsolutePath() + "/" + srcMatter.Name
|
destAbsolutePath := destMatter.AbsolutePath() + "/" + srcMatter.Name
|
||||||
srcAbsolutePath := srcMatter.AbsolutePath()
|
srcAbsolutePath := srcMatter.AbsolutePath()
|
||||||
|
|
||||||
|
//物理文件一口气移动
|
||||||
err := os.Rename(srcAbsolutePath, destAbsolutePath)
|
err := os.Rename(srcAbsolutePath, destAbsolutePath)
|
||||||
this.PanicError(err)
|
this.PanicError(err)
|
||||||
|
|
||||||
@ -681,8 +690,6 @@ func (this *MatterService) Move(srcMatter *Matter, destMatter *Matter) {
|
|||||||
|
|
||||||
//调整该文件夹下文件的Path.
|
//调整该文件夹下文件的Path.
|
||||||
matters := this.matterDao.List(srcMatter.Uuid, srcMatter.UserUuid, nil)
|
matters := this.matterDao.List(srcMatter.Uuid, srcMatter.UserUuid, nil)
|
||||||
|
|
||||||
//调整该文件夹下面所有文件的Path值
|
|
||||||
for _, m := range matters {
|
for _, m := range matters {
|
||||||
this.adjustPath(m, srcMatter)
|
this.adjustPath(m, srcMatter)
|
||||||
}
|
}
|
||||||
@ -693,11 +700,12 @@ func (this *MatterService) Move(srcMatter *Matter, destMatter *Matter) {
|
|||||||
destAbsolutePath := destMatter.AbsolutePath() + "/" + srcMatter.Name
|
destAbsolutePath := destMatter.AbsolutePath() + "/" + srcMatter.Name
|
||||||
srcAbsolutePath := srcMatter.AbsolutePath()
|
srcAbsolutePath := srcMatter.AbsolutePath()
|
||||||
|
|
||||||
|
//物理文件进行移动
|
||||||
err := os.Rename(srcAbsolutePath, destAbsolutePath)
|
err := os.Rename(srcAbsolutePath, destAbsolutePath)
|
||||||
this.PanicError(err)
|
this.PanicError(err)
|
||||||
|
|
||||||
//删除对应的缓存。
|
//删除对应的缓存。
|
||||||
this.imageCacheService.Delete(srcMatter)
|
this.imageCacheDao.DeleteByMatterUuid(srcMatter.Uuid)
|
||||||
|
|
||||||
//修改数据库中信息
|
//修改数据库中信息
|
||||||
srcMatter.Puuid = destMatter.Uuid
|
srcMatter.Puuid = destMatter.Uuid
|
||||||
@ -708,3 +716,54 @@ func (this *MatterService) Move(srcMatter *Matter, destMatter *Matter) {
|
|||||||
|
|
||||||
return
|
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
|
return dirPath
|
||||||
}
|
}
|
||||||
|
|
||||||
//尝试删除空文件夹
|
//获取到一个Path的文件夹路径,eg /var/www/xx.log -> /var/www
|
||||||
func DeleteEmptyDir(dirPath string) {
|
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)
|
dir, err := ioutil.ReadDir(dirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LOGGER.Error("尝试读取目录%s时出错 %s", dirPath, err.Error())
|
LOGGER.Error("尝试读取目录%s时出错 %s", dirPath, err.Error())
|
||||||
panic("尝试读取目录时出错 " + err.Error())
|
panic("尝试读取目录时出错 " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(dir) == 0 {
|
if len(dir) == 0 {
|
||||||
//空文件夹
|
//空文件夹
|
||||||
err = os.Remove(dirPath)
|
err = os.Remove(dirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LOGGER.Error("删除磁盘上的文件夹%s出错 %s", dirPath, err.Error())
|
LOGGER.Error("删除磁盘上的文件夹%s出错 %s", dirPath, err.Error())
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
} else {
|
} else {
|
||||||
LOGGER.Info("文件夹不为空,%v", len(dir))
|
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
|
//移除某个文件夹。 例如:/var/www/matter => /var/www
|
||||||
|
Reference in New Issue
Block a user