-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhandlers.go
150 lines (130 loc) · 3.47 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"html/template"
"io"
"mime/multipart"
"net/http"
"strings"
"image/gif"
"image/png"
"github.com/jamesandersen/gosudoku/sudokuparser"
)
type page struct {
Title string
Image template.URL
Success bool
Body string
Values map[string]CellValue
Points []sudokuparser.Point2d
Error string
}
func sudokuFormHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
p := &page{Title: "TestPage", Body: "--Solution goes here--."}
t, _ := template.ParseFiles("web/sudoku.html")
t.Execute(w, p)
case "POST":
solveHandler(w, r)
default:
// Give an error message.
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
}
func gifToPng(file multipart.File) ([]byte, error) {
img, err := gif.Decode(file)
if err != nil {
return nil, err
}
var b bytes.Buffer
writer := bufio.NewWriter(&b)
err = png.Encode(writer, img)
if err != nil {
return nil, err
}
err = writer.Flush()
if err != nil {
return nil, err
}
fmt.Printf("Encoded %v byte PNG image\n", b.Len())
return b.Bytes(), nil
}
func solveHandler(w http.ResponseWriter, r *http.Request) {
var p *page
isGif := false
t, _ := template.ParseFiles("web/sudoku.html")
defer func() {
if err := recover(); err != nil {
fmt.Println("Recovering from panic in solveHandler...", err)
p = &page{Title: "Error", Error: fmt.Sprintf("%v", err), Success: false}
if r.Header.Get("X-Requested-With") == "XMLHttpRequest" {
js, jerr := json.Marshal(p)
if jerr != nil {
http.Error(w, jerr.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
http.Error(w, string(js), http.StatusInternalServerError)
} else {
t.Execute(w, p)
}
}
}()
var Buf bytes.Buffer
file, header, err := r.FormFile("sudokuFile")
defer file.Close()
fmt.Printf("File name %s\n", header.Filename)
for key, value := range header.Header {
for _, val := range value {
if strings.ToLower(key) == "content-type" {
if strings.ToLower(val) == "image/gif" {
isGif = true
} else if !(strings.ToLower(val) == "image/png" ||
strings.ToLower(val) == "image/jpg" ||
strings.ToLower(val) == "image/jpeg") {
err := fmt.Sprintf("Invalid image format %s; only gif, png and jpg supported", val)
p = &page{Title: "Invalid Image", Success: false, Error: err}
}
}
}
}
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
var bytes []byte
if isGif {
fmt.Print("Gif image detected, converting to PNG\n")
bytes, err = gifToPng(file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
} else {
// Copy the file data to my buffer
io.Copy(&Buf, file)
bytes = Buf.Bytes()
}
if p == nil {
parsed, points := sudokuparser.ParseSudokuFromByteArray(bytes)
fmt.Println("Parsed sudoku: " + parsed)
board := NewSudoku(parsed, STANDARD)
fmt.Print("Attempting to solve Sudoku...\n")
board.Print()
finalBoard, success := board.Solve()
if success {
p = &page{Title: "Solved", Body: finalBoard.ToString(), Success: true, Values: finalBoard.values, Points: points, Error: ""}
} else {
p = &page{Title: "Not Solved", Body: finalBoard.ToString(), Success: false, Error: ""}
}
}
js, err := json.Marshal(p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}