|
@@ -16,6 +16,7 @@ import (
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
|
|
|
+ "imuslab.com/arozos/mod/filesystem/fssort"
|
|
hidden "imuslab.com/arozos/mod/filesystem/hidden"
|
|
hidden "imuslab.com/arozos/mod/filesystem/hidden"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -60,7 +61,7 @@ func (rh *RenderHandler) BuildCacheForFolder(path string) error {
|
|
|
|
|
|
func (rh *RenderHandler) CacheExists(file string) bool {
|
|
func (rh *RenderHandler) CacheExists(file string) bool {
|
|
cacheFolder := filepath.ToSlash(filepath.Clean(filepath.Dir(file))) + "/.cache/"
|
|
cacheFolder := filepath.ToSlash(filepath.Clean(filepath.Dir(file))) + "/.cache/"
|
|
- return fileExists(cacheFolder + filepath.Base(file) + ".jpg")
|
|
|
|
|
|
+ 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
|
|
//Try to load a cache from file. If not exists, generate it now
|
|
@@ -72,28 +73,41 @@ func (rh *RenderHandler) LoadCache(file string, generateOnly bool) (string, erro
|
|
hidden.HideFile(cacheFolder)
|
|
hidden.HideFile(cacheFolder)
|
|
|
|
|
|
//Check if cache already exists. If yes, return the image from the cache folder
|
|
//Check if cache already exists. If yes, return the image from the cache folder
|
|
- if fileExists(cacheFolder + filepath.Base(file) + ".jpg") {
|
|
|
|
|
|
+ if fileExists(cacheFolder+filepath.Base(file)+".jpg") || fileExists(cacheFolder+filepath.Base(file)+".png") {
|
|
if generateOnly {
|
|
if generateOnly {
|
|
//Only generate, do not return image
|
|
//Only generate, do not return image
|
|
return "", nil
|
|
return "", nil
|
|
}
|
|
}
|
|
|
|
|
|
- //Check if the file is being writting by another process. If yes, wait for it
|
|
|
|
- counter := 0
|
|
|
|
- for rh.fileIsBusy(file) && counter < 15 {
|
|
|
|
- counter += 1
|
|
|
|
- time.Sleep(1 * time.Second)
|
|
|
|
|
|
+ //Allow thumbnail to be either jpg or png file
|
|
|
|
+ ext := ".jpg"
|
|
|
|
+ if !fileExists(cacheFolder + filepath.Base(file) + ".jpg") {
|
|
|
|
+ ext = ".png"
|
|
}
|
|
}
|
|
|
|
|
|
- //Time out and the file is still busy
|
|
|
|
- if rh.fileIsBusy(file) {
|
|
|
|
- log.Println("Process racing for cache file. Skipping", file)
|
|
|
|
- return "", errors.New("Process racing for cache file. Skipping")
|
|
|
|
|
|
+ //Updates 02/10/2021: Check if the source file is newer than the cache. Update the cache if true
|
|
|
|
+ if mtime(file) > mtime(cacheFolder+filepath.Base(file)+ext) {
|
|
|
|
+ //File is newer than cache. Delete the cache
|
|
|
|
+ os.Remove(cacheFolder + filepath.Base(file) + ext)
|
|
|
|
+ } else {
|
|
|
|
+ //Check if the file is being writting by another process. If yes, wait for it
|
|
|
|
+ counter := 0
|
|
|
|
+ for rh.fileIsBusy(file) && counter < 15 {
|
|
|
|
+ counter += 1
|
|
|
|
+ time.Sleep(1 * time.Second)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Time out and the file is still busy
|
|
|
|
+ if rh.fileIsBusy(file) {
|
|
|
|
+ log.Println("Process racing for cache file. Skipping", file)
|
|
|
|
+ return "", errors.New("Process racing for cache file. Skipping")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Read and return the image
|
|
|
|
+ ctx, err := getImageAsBase64(cacheFolder + filepath.Base(file) + ext)
|
|
|
|
+ return ctx, err
|
|
}
|
|
}
|
|
|
|
|
|
- //Read and return the image
|
|
|
|
- ctx, err := getImageAsBase64(cacheFolder + filepath.Base(file) + ".jpg")
|
|
|
|
- return ctx, err
|
|
|
|
} else {
|
|
} else {
|
|
//This file not exists yet. Check if it is being hold by another process already
|
|
//This file not exists yet. Check if it is being hold by another process already
|
|
if rh.fileIsBusy(file) {
|
|
if rh.fileIsBusy(file) {
|
|
@@ -135,6 +149,13 @@ func (rh *RenderHandler) LoadCache(file string, generateOnly bool) (string, erro
|
|
return img, err
|
|
return img, err
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ //Folder preview renderer
|
|
|
|
+ if isDir(file) && len(filepath.Base(file)) > 0 && filepath.Base(file)[:1] != "." {
|
|
|
|
+ img, err := generateThumbnailForFolder(cacheFolder, file, generateOnly)
|
|
|
|
+ rh.renderingFiles.Delete(file)
|
|
|
|
+ return img, err
|
|
|
|
+ }
|
|
|
|
+
|
|
//Other filters
|
|
//Other filters
|
|
rh.renderingFiles.Delete(file)
|
|
rh.renderingFiles.Delete(file)
|
|
return "", errors.New("No supported format")
|
|
return "", errors.New("No supported format")
|
|
@@ -169,8 +190,8 @@ func getImageAsBase64(path string) (string, error) {
|
|
return string(encoded), nil
|
|
return string(encoded), nil
|
|
}
|
|
}
|
|
|
|
|
|
-//Load a list of folder cache from websocket
|
|
|
|
-func (rh *RenderHandler) HandleLoadCache(w http.ResponseWriter, r *http.Request, rpath string) {
|
|
|
|
|
|
+//Load a list of folder cache from websocket, pass in "" (empty string) for default sorting method
|
|
|
|
+func (rh *RenderHandler) HandleLoadCache(w http.ResponseWriter, r *http.Request, rpath string, sortmode string) {
|
|
//Get a list of files pending to be cached and sent
|
|
//Get a list of files pending to be cached and sent
|
|
targetPath := filepath.ToSlash(filepath.Clean(rpath))
|
|
targetPath := filepath.ToSlash(filepath.Clean(rpath))
|
|
|
|
|
|
@@ -206,6 +227,24 @@ func (rh *RenderHandler) HandleLoadCache(w http.ResponseWriter, r *http.Request,
|
|
errorExists := false
|
|
errorExists := false
|
|
filesWithoutCache := []string{}
|
|
filesWithoutCache := []string{}
|
|
|
|
|
|
|
|
+ //Updates implementation 02/10/2021: Load thumbnail of files first before folder and apply user preference sort mode
|
|
|
|
+ if sortmode == "" {
|
|
|
|
+ sortmode = "default"
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pendingFiles := []string{}
|
|
|
|
+ pendingFolders := []string{}
|
|
|
|
+ for _, file := range files {
|
|
|
|
+ if isDir(file) {
|
|
|
|
+ pendingFiles = append(pendingFiles, file)
|
|
|
|
+ } else {
|
|
|
|
+ pendingFolders = append(pendingFolders, file)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ pendingFiles = append(pendingFiles, pendingFolders...)
|
|
|
|
+
|
|
|
|
+ files = fssort.SortFileList(pendingFiles, sortmode)
|
|
|
|
+
|
|
//Updated implementation 24/12/2020: Load image with cache first before rendering those without
|
|
//Updated implementation 24/12/2020: Load image with cache first before rendering those without
|
|
|
|
|
|
for _, file := range files {
|
|
for _, file := range files {
|