diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..27b7d6d --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2017 Anders Milje + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..03fc6e1 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# Lightweight Image Viewer +A lightweight, browser-based image viewer originally made to view enormous amounts of images on headless servers. + +I suggest not running this on any port that is open to the internet. + +## How to run it + +```go run main.go``` + +The program is now listening on port `8080` + +## How to use it + +Everything after the port in the URL is considered a path to a directory. +To view the example images you have to visit this URL in your browser: +```http://localhost:8080/tmp/ImageLight/examples``` + +![](examples/example0.png) + diff --git a/examples/exampl3.png b/examples/exampl3.png new file mode 100644 index 0000000..25fd4fd Binary files /dev/null and b/examples/exampl3.png differ diff --git a/examples/example0.png b/examples/example0.png new file mode 100644 index 0000000..3caac42 Binary files /dev/null and b/examples/example0.png differ diff --git a/examples/example1.jpg b/examples/example1.jpg new file mode 100644 index 0000000..54f9d3d Binary files /dev/null and b/examples/example1.jpg differ diff --git a/examples/example2.jpg b/examples/example2.jpg new file mode 100644 index 0000000..a5831cd Binary files /dev/null and b/examples/example2.jpg differ diff --git a/examples/example4.gif b/examples/example4.gif new file mode 100644 index 0000000..a9cc244 Binary files /dev/null and b/examples/example4.gif differ diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 0000000..e69de29 diff --git a/index.html b/index.html new file mode 100644 index 0000000..a8faf57 --- /dev/null +++ b/index.html @@ -0,0 +1,85 @@ + + + + + Image Viwer + + + + + + + + Expand directory list > + Hide directory list < +
+

Directories

+ {{ range .Dirs }} + {{.}}
+ {{ end }} +
+ +
+ {{ range .Files }} +
+ + +
+ {{ end }} +
+ + + + diff --git a/main.go b/main.go new file mode 100644 index 0000000..9cbd614 --- /dev/null +++ b/main.go @@ -0,0 +1,95 @@ +package main + +import ( + "fmt" + "html/template" + "io/ioutil" + "log" + "net/http" + "os" + "path/filepath" +) + +type Page struct { + Title string + Body []byte + Files []string + Dirs []string + CurrDir string +} + +func serveImage(w http.ResponseWriter, r *http.Request) { + filename := r.URL.Path[len("/image"):] + //body := r.FormValue("fn") + log.Printf("Filename: %s", filename) + file, err := os.Open(filename) + if err != nil { + log.Printf("Error: %s", err) + } + file.Close() + //image, err := ioutil.ReadFile(filename) + //if err != nil { + //fmt.Fprintf(w, "

%s

", err) + //} + http.ServeFile(w, r, filename) +} + +func loadPage(title, directory string) (*Page, error) { + searchDir := directory //"/home/syn/Dropbox/Dev/2017/ImageLight" + //searchDir := "/home/syn/images/test11" + fileList := []string{} + dirList := []string{} + dirList = append(dirList, filepath.Dir(directory)) + err := filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error { + ext := filepath.Ext(path) + if f.IsDir() { + dirList = append(dirList, path) + //log.Printf("Directory: %s", path) + //log.Printf("Dirdir: %s", filepath.Dir(path)) + } + if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".mp4" { + fileList = append(fileList, path) + } + return nil + }) + if err != nil { + fmt.Println(err) + } + + filename := title + ".html" + body, err := ioutil.ReadFile(filename) + if err != nil { + return nil, err + } + return &Page{Title: title, Body: body, Files: fileList, Dirs: dirList}, nil +} + +func indexHandler(w http.ResponseWriter, r *http.Request) { + directory := r.URL.Path + log.Printf(directory) + p, err := loadPage("index", directory) + if err != nil { + p = &Page{Title: "index"} + } + t, _ := template.ParseFiles("index.html") + t.Execute(w, p) +} + +func handler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello %s!", r.URL.Path[1:]) +} + +// http://www.alexedwards.net/blog/serving-static-sites-with-go +func main() { + + http.HandleFunc("/", indexHandler) + http.HandleFunc("/image/", serveImage) + + fs := http.FileServer(http.Dir("static")) + http.Handle("/static/", http.StripPrefix("/static/", fs)) + //http.HandleFunc() + + log.Println("Listening...") + + http.ListenAndServe(":8080", nil) +}