diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a9b2e5e..b0d73827 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v2.1.0 + +## What's Changed + +* Added optional volume for custom (non-selfh.st) icons ([#495](https://github.com/selfhst/icons/issues/495)) + # v2.0.0 ## Breaking Changes diff --git a/build/VERSION b/build/VERSION index 359a5b95..50aea0e7 100755 --- a/build/VERSION +++ b/build/VERSION @@ -1 +1 @@ -2.0.0 \ No newline at end of file +2.1.0 \ No newline at end of file diff --git a/build/main.go b/build/main.go index 34429efb..9109043b 100755 --- a/build/main.go +++ b/build/main.go @@ -358,6 +358,72 @@ func handleLegacyIcon(w http.ResponseWriter, r *http.Request) { handleIcon(w, r) } +func handleCustomIcon(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + filename := vars["filename"] + + if filename == "" { + http.Error(w, "Filename is required", http.StatusBadRequest) + return + } + + // Build the path to the custom icon file + customPath := filepath.Join("/app/icons/custom", filename) + + // Debug logging + log.Printf("[DEBUG] Looking for custom icon at: %s", customPath) + + // Check if file exists + if !fileExists(customPath) { + // List directory contents for debugging + if files, err := os.ReadDir("/app/icons/custom"); err == nil { + var fileList []string + for _, file := range files { + fileList = append(fileList, file.Name()) + } + log.Printf("[DEBUG] Files in /app/icons/custom: %v", fileList) + } else { + log.Printf("[DEBUG] Failed to read /app/icons/custom directory: %v", err) + } + log.Printf("[ERROR] Custom icon not found: \"%s\" at path: %s", filename, customPath) + http.Error(w, "Custom icon not found", http.StatusNotFound) + return + } + + // Read the file + data, err := os.ReadFile(customPath) + if err != nil { + log.Printf("[ERROR] Failed to read custom icon \"%s\": %v", filename, err) + http.Error(w, "Failed to read custom icon", http.StatusInternalServerError) + return + } + + // Determine content type based on file extension + ext := strings.ToLower(filepath.Ext(filename)) + var contentType string + switch ext { + case ".png": + contentType = "image/png" + case ".jpg", ".jpeg": + contentType = "image/jpeg" + case ".gif": + contentType = "image/gif" + case ".svg": + contentType = "image/svg+xml" + case ".webp": + contentType = "image/webp" + case ".ico": + contentType = "image/x-icon" + default: + contentType = "application/octet-stream" + } + + log.Printf("[SUCCESS] Serving custom icon: \"%s\" (%s)", filename, contentType) + + w.Header().Set("Content-Type", contentType) + w.Write(data) +} + func handleRoot(w http.ResponseWriter, r *http.Request) { configInfo := map[string]interface{}{ "server": "Self-hosted icon server", @@ -390,6 +456,9 @@ func main() { r := mux.NewRouter() + // Custom icons route: /custom/filename + r.HandleFunc("/custom/{filename}", handleCustomIcon).Methods("GET") + // Main route: /iconname or /iconname/colorcode r.HandleFunc("/{iconname}", handleIcon).Methods("GET") r.HandleFunc("/{iconname}/{colorcode}", handleIcon).Methods("GET")