diff --git a/rest/alien_service.go b/rest/alien_service.go index de778ef..fa4e459 100644 --- a/rest/alien_service.go +++ b/rest/alien_service.go @@ -128,7 +128,7 @@ func (this *AlienService) PreviewOrDownload( this.matterService.DownloadFile(writer, request, GetUserCacheRootDir(imageCache.Username)+imageCache.Path, imageCache.Name, withContentDisposition) } else { - this.matterService.DownloadFile(writer, request, GetUserFileRootDir(matter.Username)+matter.Path, matter.Name, withContentDisposition) + this.matterService.DownloadFile(writer, request, matter.AbsolutePath(), matter.Name, withContentDisposition) } //文件下载次数加一,为了加快访问速度,异步进行 diff --git a/rest/image_cache_dao.go b/rest/image_cache_dao.go index 36ff388..d8cb95f 100644 --- a/rest/image_cache_dao.go +++ b/rest/image_cache_dao.go @@ -5,7 +5,7 @@ import ( "github.com/jinzhu/gorm" "github.com/nu7hatch/gouuid" "os" - "strings" + "path/filepath" "time" ) @@ -140,10 +140,9 @@ func (this *ImageCacheDao) Save(imageCache *ImageCache) *ImageCache { //删除一个文件包括文件夹 func (this *ImageCacheDao) deleteFileAndDir(imageCache *ImageCache) { - filePath := CONFIG.MatterPath + imageCache.Path - //递归找寻文件的上级目录uuid. 因为是/开头的缘故 - parts := strings.Split(imageCache.Path, "/") - dirPath := CONFIG.MatterPath + "/" + parts[1] + "/" + parts[2] + "/" + parts[3] + "/" + parts[4] + filePath := GetUserCacheRootDir(imageCache.Username) + imageCache.Path + + dirPath := filepath.Dir(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())) } - //删除这一层文件夹 - err = os.Remove(dirPath) - if err != nil { - this.logger.Error(fmt.Sprintf("删除磁盘上的文件夹%s出错 %s", dirPath, err.Error())) - } + //如果这一层文件夹是空的,那么删除文件夹本身。 + DeleteEmptyDir(dirPath) + } //删除一个文件,数据库中删除,物理磁盘上删除。 diff --git a/rest/image_cache_model.go b/rest/image_cache_model.go index 066f45c..b648f8f 100644 --- a/rest/image_cache_model.go +++ b/rest/image_cache_model.go @@ -20,3 +20,8 @@ type ImageCache struct { func (this *ImageCache) TableName() string { return TABLE_PREFIX + "image_cache" } + +// 获取该ImageCache的绝对路径。path代表的是相对路径。 +func (this *ImageCache) AbsolutePath() string { + return GetUserCacheRootDir(this.Username) + this.Path +} diff --git a/rest/image_cache_service.go b/rest/image_cache_service.go index dca1e9c..a28f892 100644 --- a/rest/image_cache_service.go +++ b/rest/image_cache_service.go @@ -195,7 +195,7 @@ func (this *ImageCacheService) cacheImage(writer http.ResponseWriter, request *h user := this.userDao.FindByUuid(matter.UserUuid) //resize图片 - dstImage := this.ResizeImage(request, GetUserFileRootDir(user.Username)+matter.Path) + dstImage := this.ResizeImage(request, matter.AbsolutePath()) cacheImageName := GetSimpleFileName(matter.Name) + "_" + mode + extension cacheImageRelativePath := GetSimpleFileName(matter.Path) + "_" + mode + extension diff --git a/rest/matter_dao.go b/rest/matter_dao.go index 42e91d4..4bc433b 100644 --- a/rest/matter_dao.go +++ b/rest/matter_dao.go @@ -2,10 +2,10 @@ package rest import ( "github.com/jinzhu/gorm" + "path/filepath" "github.com/nu7hatch/gouuid" "os" - "strings" "time" ) @@ -239,17 +239,16 @@ func (this *MatterDao) Delete(matter *Matter) { 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) this.PanicError(db.Error) + //从磁盘中删除该文件夹。 + DeleteEmptyDir(matter.AbsolutePath()) + + } else { + //删除数据库中文件记录 db := CONTEXT.DB.Delete(&matter) this.PanicError(db.Error) @@ -257,25 +256,15 @@ func (this *MatterDao) Delete(matter *Matter) { //删除对应的缓存图片。 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 { this.logger.Error("删除磁盘上的文件出错 %s", err.Error()) } - //删除这一层文件夹 - err = os.Remove(dirPath) - if err != nil { - this.logger.Error("删除磁盘上的文件夹出错 %s", err.Error()) - } + //如果目录为空,那么一并删除 + dirPath := filepath.Dir(matter.AbsolutePath()) + DeleteEmptyDir(dirPath) } } @@ -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)") this.PanicError(db.Error) row := db.Row() - row.Scan(&size) + err := row.Scan(&size) + this.PanicError(err) return size } diff --git a/rest/matter_model.go b/rest/matter_model.go index d23044c..4fc6b66 100644 --- a/rest/matter_model.go +++ b/rest/matter_model.go @@ -29,8 +29,7 @@ func (Matter) TableName() string { return TABLE_PREFIX + "matter" } - -// 获取该Matter的绝对路径。path代表的是相对路径。相对路径的根目录是 CONFIG.MatterPath +// 获取该Matter的绝对路径。path代表的是相对路径。 func (this *Matter) AbsolutePath() string { - return CONFIG.MatterPath + this.Path + return GetUserFileRootDir(this.Username) + this.Path } diff --git a/rest/util_path.go b/rest/util_path.go index b01934f..a4e9458 100644 --- a/rest/util_path.go +++ b/rest/util_path.go @@ -3,6 +3,7 @@ package rest import ( "fmt" "go/build" + "io/ioutil" "os" "os/user" "path/filepath" @@ -91,6 +92,25 @@ func MakeDirAll(dirPath string) string { 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 func RemoveDirectory(dirPath string) string {