浏览代码

Added cache remove after file move opr

tobychui 4 年之前
父节点
当前提交
aa57f71021
共有 2 个文件被更改,包括 43 次插入18 次删除
  1. 4 12
      mod/filesystem/fileOpr.go
  2. 39 6
      mod/filesystem/metadata/metadata.go

+ 4 - 12
mod/filesystem/fileOpr.go

@@ -25,6 +25,7 @@ import (
 	"time"
 
 	"imuslab.com/arozos/mod/filesystem/hidden"
+	"imuslab.com/arozos/mod/filesystem/metadata"
 
 	archiver "github.com/mholt/archiver/v3"
 )
@@ -354,18 +355,6 @@ func insideHiddenFolder(path string) bool {
 		return true
 	}
 	return FileIsHidden
-
-	/*
-		thisPathInfo := filepath.ToSlash(filepath.Clean(path))
-		pathData := strings.Split(thisPathInfo, "/")
-		for _, thispd := range pathData {
-			if len(thispd) > 0 && thispd[:1] == "." {
-				//This path contain one of the folder is hidden
-				return true
-			}
-		}
-		return false
-	*/
 }
 
 func ViewZipFile(filepath string) ([]string, error) {
@@ -599,6 +588,9 @@ func FileMove(src string, dest string, mode string, fastMove bool, progressUpdat
 		}
 	}
 
+	//Remove the source cache as it no longer exists in this folder
+	metadata.RemoveCache(src)
+
 	return nil
 }
 

+ 39 - 6
mod/filesystem/metadata/metadata.go

@@ -59,11 +59,6 @@ func (rh *RenderHandler) BuildCacheForFolder(path string) error {
 	return nil
 }
 
-func (rh *RenderHandler) CacheExists(file string) bool {
-	cacheFolder := filepath.ToSlash(filepath.Clean(filepath.Dir(file))) + "/.cache/"
-	return fileExists(cacheFolder+filepath.Base(file)+".jpg") || fileExists(cacheFolder+filepath.Base(file)+".png")
-}
-
 //Try to load a cache from file. If not exists, generate it now
 func (rh *RenderHandler) LoadCache(file string, generateOnly bool) (string, error) {
 	//Create a cache folder
@@ -248,7 +243,7 @@ func (rh *RenderHandler) HandleLoadCache(w http.ResponseWriter, r *http.Request,
 	//Updated implementation 24/12/2020: Load image with cache first before rendering those without
 
 	for _, file := range files {
-		if rh.CacheExists(file) == false {
+		if CacheExists(file) == false {
 			//Cache not exists. Render this later
 			filesWithoutCache = append(filesWithoutCache, file)
 		} else {
@@ -294,6 +289,44 @@ func (rh *RenderHandler) HandleLoadCache(w http.ResponseWriter, r *http.Request,
 
 }
 
+//Check if the cache for a file exists
+func CacheExists(file string) bool {
+	cacheFolder := filepath.ToSlash(filepath.Clean(filepath.Dir(file))) + "/.cache/"
+	return fileExists(cacheFolder+filepath.Base(file)+".jpg") || fileExists(cacheFolder+filepath.Base(file)+".png")
+}
+
+//Get cache path for this file, given realpath
+func GetCacheFilePath(file string) (string, error) {
+	if CacheExists(file) {
+		cacheFolder := filepath.ToSlash(filepath.Clean(filepath.Dir(file))) + "/.cache/"
+		if fileExists(cacheFolder + filepath.Base(file) + ".jpg") {
+			return cacheFolder + filepath.Base(file) + ".jpg", nil
+		} else if fileExists(cacheFolder + filepath.Base(file) + ".png") {
+			return cacheFolder + filepath.Base(file) + ".png", nil
+		} else {
+			return "", errors.New("Unable to resolve thumbnail cache location")
+		}
+	} else {
+		return "", errors.New("No thumbnail cached for this file")
+	}
+}
+
+//Remove cache if exists, given realpath
+func RemoveCache(file string) error {
+	if CacheExists(file) {
+		cachePath, err := GetCacheFilePath(file)
+		if err != nil {
+			return err
+		}
+
+		//Remove the thumbnail cache
+		os.Remove(cachePath)
+		return nil
+	} else {
+		return errors.New("Thumbnail cache not exists for this file")
+	}
+}
+
 func specialGlob(path string) ([]string, error) {
 	files, err := filepath.Glob(path)
 	if err != nil {