Skip to content

Commit

Permalink
initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
viktomas committed Aug 14, 2023
0 parents commit 0fb7f81
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
SHELL = /bin/bash
.SILENT: # comment this one out if you need to debug the Makefile
APP=picsort

.PHONY: run
run:
go run . ~/Downloads/tmp/pictures/

.PHONY: build
build:
go build
# go build -o ${APP} main.go

.PHONY: test
test:
go test ./...

.PHONY: watch-test
watch-test:
fswatch \
--exclude '.git' \
-o . | \
xargs -n1 -I{} go test ./...

.PHONY: clean
clean:
go clean


.PHONY: watch-run
watch-run:
fswatch \
--exclude '.git' \
-xn . | \
while read file event; do \
echo "File $$file has changed, Event: $$event"; \
$(MAKE) run ; \
done
15 changes: 15 additions & 0 deletions assets/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>PicSort</title>
<link rel="stylesheet" href="/assets/style.css">
</head>
<body>
<h1>Images</h1>
<ul>
{{range .}}
<li><img src="/static/{{.}}" alt="{{.}}"></li>
{{end}}
</ul>
</body>
</html>
29 changes: 29 additions & 0 deletions assets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}

h1 {
text-align: center;
margin: 20px;
}

ul {
list-style-type: none;
display: flex;
flex-wrap: wrap;
justify-content: center;
padding: 0;
}

li {
margin: 10px;
}

img {
display: block;
max-width: 200px;
max-height: 200px;
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/viktomas/picsort

go 1.20
69 changes: 69 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
_ "embed"
"fmt"
"html/template"
"io"
"net/http"
"os"
"path/filepath"
)

//go:embed assets/index.html
var indexTemplate string

//go:embed assets/style.css
var css string

func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: picsort <folder>")
return
}

rootFolder := os.Args[1]

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.New("index").Parse(indexTemplate))
imageFiles := getImageFiles(rootFolder)

err := tmpl.Execute(w, imageFiles)
if err != nil {
http.Error(w, err.Error(), 500)
}
})

http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(rootFolder))))

http.HandleFunc("/assets/style.css", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/css")
io.WriteString(w, css)
})

addr := "localhost:8080"
fmt.Printf("Starting server at http://%s\n", addr)
err := http.ListenAndServe(addr, nil)
if err != nil {
fmt.Printf("Error: %s\n", err)
}
}

func getImageFiles(folderPath string) []string {
var imageFiles []string
_ = filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && isImageFile(path) {
imageFiles = append(imageFiles, filepath.Base(path))
}
return nil
})
return imageFiles
}

func isImageFile(filename string) bool {
ext := filepath.Ext(filename)
return ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".gif"
}

0 comments on commit 0fb7f81

Please sign in to comment.