-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (84 loc) · 2.63 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/tuotoo/qrcode"
)
type QRCodeResult struct {
URL string `json:"url"`
}
func DecodeImage(imagePath string) ([]QRCodeResult, error) {
file, err := os.Open(imagePath)
if err != nil {
return nil, fmt.Errorf("error opening image file: %w", err)
}
defer file.Close()
// Decode the QR code directly from the file
qrCode, err := qrcode.Decode(file)
if err != nil {
return nil, fmt.Errorf("error decoding QR code: %w", err)
}
qrCodes := []QRCodeResult{
{URL: qrCode.Content},
}
return qrCodes, nil
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
// CORS headers
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
// Handle preflight requests (OPTIONS method)
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
if r.Method != "POST" {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
file, _, err := r.FormFile("image")
if err != nil {
http.Error(w, "Error reading file: "+err.Error(), http.StatusBadRequest)
return
}
defer file.Close()
tempFile, err := ioutil.TempFile("images", "upload-*.png")
if err != nil {
http.Error(w, "Error saving file: "+err.Error(), http.StatusInternalServerError)
return
}
defer os.Remove(tempFile.Name())
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
http.Error(w, "Error reading file: "+err.Error(), http.StatusInternalServerError)
return
}
if _, err = tempFile.Write(fileBytes); err != nil {
http.Error(w, "Error writing file: "+err.Error(), http.StatusInternalServerError)
return
}
qrCodes, err := DecodeImage(tempFile.Name())
if err != nil {
http.Error(w, "Error decoding QR code: "+err.Error(), http.StatusInternalServerError)
return
}
jsonResponse, err := json.Marshal(qrCodes)
if err != nil {
http.Error(w, "Error encoding JSON: "+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonResponse)
}
func main() {
http.HandleFunc("/upload", uploadHandler)
fmt.Println("Server started on :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Println("Error starting server:", err)
}
}