Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Syntox32 committed May 24, 2017
0 parents commit d07c0c1
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Anders Milje <git@milje.me>

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.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Binary file added examples/exampl3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/example0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/example1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/example2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/example4.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added favicon.ico
Empty file.
85 changes: 85 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Image Viwer</title>
<link href="https://fonts.googleapis.com/css?family=Roboto:400,700" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!--<link rel="stylesheet" href="https://unpkg.com/purecss@0.6.2/build/pure-min.css" integrity="sha384-UQiGfs9ICog+LwheBSRCt1o5cbyKIHbwjWscjemyBMT9YCUMZffs6UqUTd0hObXD" crossorigin="anonymous">-->
<style media="screen">

* {
margin: 0;
padding: 0;

font-family: "Helvetica";
/*background-color: #FFFDFD;*/
}

/* medium screens */
@media all and (min-width: 600px) {

}

.container {
display: flex;
flex-flow: row wrap;
justify-content: space-around;
align-content: flex-start;
align-items: center;
}

.image {
flex-basis: auto;
max-width: 400px;
/*background-color: blue;*/
vertical-align: middle;
height: 100%;
align-self: center;
margin: 3px;
}

.image img {
max-width: 400px;
border: 2px solid #E7E5E5;
margin: 0 auto;
}

.image .link a {
color: #b3b3b3;
text-align: center;
}

.dirs, #show, #hide:target {
display: none;
}

#hide:target + #show, #hide:target ~ .dirs {
display: inline;
}

</style>
<!--<link rel="stylesheet" href="/static/main.css">-->
</head>
<body>
<a href="#hide" id="hide">Expand directory list &gt;</a>
<a href="#show" id="show">Hide directory list &lt;</a>
<div class="dirs">
<p>Directories</p>
{{ range .Dirs }}
<a href="{{.}}">{{.}}</a><br>
{{ end }}
</div>

<div class="container">
{{ range .Files }}
<div class="image">
<a href="/image/{{.}}"><img src="/image/{{.}}" alt=""></a>
<p class="link"><a href="/image/{{.}}">{{.}}</a></p>
</div>
{{ end }}
</div>

<script src="/static/main.js"></script>
</body>
</html>
95 changes: 95 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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, "<h1>%s</h1>", 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)
}

0 comments on commit d07c0c1

Please sign in to comment.