From 39f68e4d8ab943319cb6418c874a9987731ca694 Mon Sep 17 00:00:00 2001
From: viktorstrate <viktorstrate@gmail.com>
Date: Sun, 23 Feb 2020 12:43:45 +0100
Subject: [PATCH] Support PHOTO_CACHE env variable

---
 .gitignore                   |  2 +-
 api/routes/photos.go         | 14 ++++++++------
 api/scanner/process_image.go |  6 +++---
 api/scanner/scanner.go       |  9 +++++++++
 4 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/.gitignore b/.gitignore
index 2fa10b3..e3307a1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,7 @@
 # See https://help.github.com/ignore-files/ for more about ignoring files.
 
 cache/
-image-cache/
+photo_cache/
 /photos_path
 
 .env
diff --git a/api/routes/photos.go b/api/routes/photos.go
index 3455017..080d368 100644
--- a/api/routes/photos.go
+++ b/api/routes/photos.go
@@ -7,11 +7,14 @@ import (
 	"log"
 	"net/http"
 	"os"
+	"path"
+	"strconv"
 
 	"github.com/gorilla/mux"
 
 	"github.com/viktorstrate/photoview/api/graphql/auth"
 	"github.com/viktorstrate/photoview/api/graphql/models"
+	"github.com/viktorstrate/photoview/api/scanner"
 )
 
 func RegisterPhotoRoutes(db *sql.DB, router *mux.Router) {
@@ -22,12 +25,12 @@ func RegisterPhotoRoutes(db *sql.DB, router *mux.Router) {
 		row := db.QueryRow("SELECT photo_url.purpose, photo.path, photo.photo_id, photo.album_id, photo_url.content_type FROM photo_url, photo WHERE photo_url.photo_name = ? AND photo_url.photo_id = photo.photo_id", image_name)
 
 		var purpose models.PhotoPurpose
-		var path string
+		var photoPath string
 		var content_type string
 		var album_id int
 		var photo_id int
 
-		if err := row.Scan(&purpose, &path, &photo_id, &album_id, &content_type); err != nil {
+		if err := row.Scan(&purpose, &photoPath, &photo_id, &album_id, &content_type); err != nil {
 			w.WriteHeader(http.StatusNotFound)
 			w.Write([]byte("404"))
 			return
@@ -102,13 +105,11 @@ func RegisterPhotoRoutes(db *sql.DB, router *mux.Router) {
 
 		}
 
-		w.Header().Set("Content-Type", content_type)
-
 		var file *os.File
 
 		if purpose == models.PhotoThumbnail || purpose == models.PhotoHighRes {
 			var err error
-			file, err = os.Open(fmt.Sprintf("./image-cache/%d/%d/%s", album_id, photo_id, image_name))
+			file, err = os.Open(path.Join(scanner.PhotoCache(), strconv.Itoa(album_id), strconv.Itoa(photo_id), image_name))
 			if err != nil {
 				w.Write([]byte("Error: " + err.Error()))
 				return
@@ -117,13 +118,14 @@ func RegisterPhotoRoutes(db *sql.DB, router *mux.Router) {
 
 		if purpose == models.PhotoOriginal {
 			var err error
-			file, err = os.Open(path)
+			file, err = os.Open(photoPath)
 			if err != nil {
 				w.Write([]byte("Error: " + err.Error()))
 				return
 			}
 		}
 
+		w.Header().Set("Content-Type", content_type)
 		if stats, err := file.Stat(); err == nil {
 			w.Header().Set("Content-Length", fmt.Sprintf("%d", stats.Size()))
 		}
diff --git a/api/scanner/process_image.go b/api/scanner/process_image.go
index b88b248..e28fcf7 100644
--- a/api/scanner/process_image.go
+++ b/api/scanner/process_image.go
@@ -81,15 +81,15 @@ func ProcessImage(tx *sql.Tx, photoPath string, albumId int, content_type string
 	// Thumbnail
 	thumbnailImage := resize.Thumbnail(1024, 1024, image, resize.Bilinear)
 
-	if _, err := os.Stat("image-cache"); os.IsNotExist(err) {
-		if err := os.Mkdir("image-cache", os.ModePerm); err != nil {
+	if _, err := os.Stat(PhotoCache()); os.IsNotExist(err) {
+		if err := os.Mkdir(PhotoCache(), os.ModePerm); err != nil {
 			log.Println("ERROR: Could not make image cache directory")
 			return err
 		}
 	}
 
 	// Make album cache dir
-	albumCachePath := path.Join("image-cache", strconv.Itoa(albumId))
+	albumCachePath := path.Join(PhotoCache(), strconv.Itoa(albumId))
 	if _, err := os.Stat(albumCachePath); os.IsNotExist(err) {
 		if err := os.Mkdir(albumCachePath, os.ModePerm); err != nil {
 			log.Println("ERROR: Could not make album image cache directory")
diff --git a/api/scanner/scanner.go b/api/scanner/scanner.go
index 10756a4..2ed0adf 100644
--- a/api/scanner/scanner.go
+++ b/api/scanner/scanner.go
@@ -348,3 +348,12 @@ func ScannerError(format string, args ...interface{}) {
 		Negative: true,
 	})
 }
+
+func PhotoCache() string {
+	photoCache := os.Getenv("PHOTO_CACHE")
+	if photoCache == "" {
+		photoCache = "./photo_cache"
+	}
+
+	return photoCache
+}
