-
Notifications
You must be signed in to change notification settings - Fork 2
/
sheets.go
171 lines (139 loc) · 3.79 KB
/
sheets.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"gopkg.in/yaml.v2"
)
type Sheet struct {
Title string `json:"title"`
Artist string `json:"artist"`
BookArtist string `json:"bookartist"`
BookTitle string `json:"booktitle"`
URL string `json:"url"`
Pages []int `json:"pages"`
}
type SheetConfig struct {
BookPath string `yaml:"path"`
BookArtist string `yaml:"artist"`
BookTitle string `yaml:"title"`
Songs []struct {
Artist string `yaml:"artist"`
Title string `yaml:"title"`
Pages []int `yaml:",flow"`
}
}
func UploadSheet(sheetDir string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(32 << 20)
if err != nil {
panic(err)
}
clientFile, handler, err := r.FormFile("file")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Cannot fetch uploaded file!"))
return
}
defer clientFile.Close()
buff := make([]byte, 512)
if _, err = clientFile.Read(buff); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Cannot read uploaded file!"))
return
}
if http.DetectContentType(buff) != "application/pdf" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Uploaded file is not a PDF!"))
return
}
destinationPath := path.Join(sheetDir, handler.Filename)
_, err = os.Stat(destinationPath)
if os.IsNotExist(err) {
f, err := os.OpenFile(destinationPath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
w.WriteHeader(http.StatusOK)
}
defer f.Close()
clientFile.Seek(0, io.SeekStart)
io.Copy(f, clientFile)
} else {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - File already exists!"))
return
}
}
}
func GetSheets(sheetDir string, parseYaml bool) ([]Sheet, error) {
sheets := []Sheet{}
processed_pdfs := []string{}
// process yaml files
if parseYaml {
pattern := sheetDir + "/*.yml"
matches, err := filepath.Glob(pattern)
if err != nil {
return nil, err
}
for _, match := range matches {
yamlFile, err := os.ReadFile(match)
if err != nil {
return nil, err
}
sheetConfig := SheetConfig{}
err = yaml.Unmarshal(yamlFile, &sheetConfig)
if err != nil {
return nil, err
}
url := "/sheet/" + url.QueryEscape(sheetConfig.BookPath)
processed_pdfs = append(processed_pdfs, sheetConfig.BookPath)
if len(sheetConfig.Songs) > 0 {
for _, song := range sheetConfig.Songs {
if song.Artist == "" {
song.Artist = sheetConfig.BookArtist
}
sheets = append(sheets, Sheet{Title: song.Title, Artist: song.Artist, URL: url, Pages: song.Pages,
BookArtist: sheetConfig.BookArtist, BookTitle: sheetConfig.BookTitle})
}
} else {
sheets = append(sheets, Sheet{Title: sheetConfig.BookTitle, Artist: sheetConfig.BookArtist, URL: url})
}
}
}
// process pdf files
pattern := sheetDir + "/*_*.pdf"
matches, err := filepath.Glob(pattern)
if err != nil {
return nil, err
}
for _, match := range matches {
match = strings.ReplaceAll(match, (sheetDir)+"/", "")
// skip already processed files
skip := false
for _, file := range processed_pdfs {
if file == match {
skip = true
break
}
}
if skip {
continue
}
url := "/sheet/" + url.QueryEscape(match)
match = strings.ReplaceAll(match, ".pdf", "")
match = strings.ReplaceAll(match, "-", " ")
tokens := strings.Split(match, "_")
caser := cases.Title(language.English, cases.NoLower)
if len(tokens) == 2 {
artist := caser.String(strings.ToLower(tokens[0]))
title := caser.String(strings.ToLower(tokens[1]))
sheets = append(sheets, Sheet{Title: title, Artist: artist, URL: url})
}
}
return sheets, nil
}