Skip to content

Commit f575411

Browse files
committed
simplify int form
1 parent 8818201 commit f575411

File tree

7 files changed

+33
-33
lines changed

7 files changed

+33
-33
lines changed

handlers/download.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ func Download(app *config.App) http.HandlerFunc {
1414
return
1515
}
1616

17-
fileName := getRequestParameter(r, len(app.Download), "name")
18-
if fileName == "" {
17+
filename := getRequestParameter(r, len(app.Download), "name")
18+
if filename == "" {
1919
writeJSON(w, http.StatusNotFound, errorJSON(app.NoFilename))
2020
app.Log.Error(app.NoFilename, "user", req)
2121
return
2222
}
23-
app.Log.Debug("file requested", "filename", fileName, "user", req)
23+
app.Log.Debug("file requested", "filename", filename, "user", req)
2424

25-
file := app.FindFile(fileName)
25+
file := app.FindFile(filename)
2626
if file == nil {
2727
writeJSON(w, http.StatusNotFound, errorJSON(app.NotFound))
28-
app.Log.Error(app.NotFound, "filename", fileName, "user", req)
28+
app.Log.Error(app.NotFound, "filename", filename, "user", req)
2929
return
3030
}
3131

handlers/form.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handlers
22

33
import (
4-
"math"
54
"net/http"
65
"strconv"
76
"strings"
@@ -11,21 +10,18 @@ import (
1110
)
1211

1312
// parseFormInt reads an integer form value or returns the default.
14-
func parseFormInt(r *http.Request, field string, def int, maximum int) int {
13+
func parseFormInt(r *http.Request, field string, def, maximum int) int {
1514
input := r.FormValue(field)
1615
if input != "" {
1716
input = strings.TrimSpace(input)
18-
if v, err := strconv.ParseUint(input, 10, 64); err == nil {
19-
if v == 0 {
17+
if v, err := strconv.Atoi(input); err == nil {
18+
if v <= 0 {
2019
return def
2120
}
22-
if v > uint64(maximum) {
21+
if v > maximum {
2322
return maximum
2423
}
25-
if v > math.MaxInt {
26-
return maximum
27-
}
28-
return int(v)
24+
return v
2925
}
3026
}
3127
return def

handlers/index.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,39 @@ import (
88
"github.com/drduh/gone/templates"
99
)
1010

11-
// Index handles requests for the main index page
12-
// with all available application features.
11+
const templateData = "data/*.tmpl"
12+
13+
// Index handles requests to load and render the index page.
1314
func Index(app *config.App) http.HandlerFunc {
1415
return func(w http.ResponseWriter, r *http.Request) {
1516
req := authRequest(w, r, app)
1617
if req == nil {
1718
return
1819
}
19-
2020
app.Log.Info("serving index", "user", req)
2121

2222
theme := getDefaultTheme(app.Style.Theme)
23-
app.Log.Debug("got theme", "default", theme)
2423
if app.Style.AllowPick {
2524
theme = getTheme(w, r, theme,
2625
app.Cookie.Id,
2726
app.Cookie.Time.GetDuration(),
2827
app.Style.Available)
29-
app.Log.Debug("got theme", "selected", theme)
3028
}
3129

32-
tmpl, err := template.New("index").ParseFS(templates.All, "data/*.tmpl")
30+
tmpl, err := template.New("index").ParseFS(templates.All, templateData)
3331
if err != nil {
3432
writeJSON(w, http.StatusInternalServerError, errorJSON(app.TmplParse))
3533
app.Log.Error(app.TmplParse, "error", err.Error(), "user", req)
3634
return
3735
}
3836

3937
app.UpdateTimeRemaining()
40-
4138
response := templates.Index{
4239
Auth: app.Auth,
40+
Default: app.Default,
4341
DefaultDuration: app.Expiration.String(),
4442
Hostname: app.Hostname,
4543
Index: app.Index,
46-
Default: app.Default,
4744
Limit: app.Limit,
4845
NoFiles: app.NoFiles,
4946
Paths: app.Paths,

handlers/random.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/drduh/gone/util"
88
)
99

10-
// Random serves a random string of specified type.
10+
// Random handles requests to generate a random string.
1111
func Random(app *config.App) http.HandlerFunc {
1212
return func(w http.ResponseWriter, r *http.Request) {
1313
req := authRequest(w, r, app)

storage/config.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import (
66
"time"
77
)
88

9-
const storageVersion = "1"
9+
const (
10+
storageVersion = "1"
11+
12+
filenameMessages = "messages.txt"
13+
filenameWall = "wall.txt"
14+
)
1015

1116
// Storage represents content uploaded by users.
1217
type Storage struct {

storage/sanitize_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ func TestSanitizeName(t *testing.T) {
8080
{"@#$%^&*", 10, extraChars, defaultName},
8181
}
8282
for _, test := range tests {
83-
result := SanitizeName(test.input, test.maxLength, test.extraChars)
84-
if result != test.want {
85-
t.Fatalf("name: '%s', length: %d, special: '%s', got: '%s', want: '%s'",
86-
test.input, test.maxLength, test.extraChars, result, test.want)
87-
}
83+
t.Run(test.input, func(t *testing.T) {
84+
result := SanitizeName(test.input, test.maxLength, test.extraChars)
85+
if result != test.want {
86+
t.Fatalf("length: %d, special: '%s', got: '%s', want: '%s'",
87+
test.maxLength, test.extraChars, result, test.want)
88+
}
89+
})
8890
}
8991
}

storage/serve.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ func (f *File) Serve(w http.ResponseWriter) {
2222

2323
// ServeMessages writes all Messages as a text file.
2424
func (s *Storage) ServeMessages(w http.ResponseWriter) {
25-
disposition := "attachment; filename=\"messages.txt\""
25+
disposition := "attachment; filename=\"" + filenameMessages + "\""
2626
w.Header().Set("Content-Disposition", disposition)
2727
w.Header().Set("Content-Type", "text/plain")
28-
msgFormat := "%d (%s) - %s\n"
28+
msgFmt := "%d (%s) - %s\n"
2929
for _, msg := range s.Messages {
30-
_, err := fmt.Fprintf(w, msgFormat, msg.Count, msg.Allow, msg.Data)
30+
_, err := fmt.Fprintf(w, msgFmt, msg.Count, msg.Allow, msg.Data)
3131
if err != nil {
3232
return
3333
}
@@ -36,7 +36,7 @@ func (s *Storage) ServeMessages(w http.ResponseWriter) {
3636

3737
// ServeWall writes all Wall content as a text file.
3838
func (s *Storage) ServeWall(w http.ResponseWriter) {
39-
disposition := "attachment; filename=\"wall.txt\""
39+
disposition := "attachment; filename=\"" + filenameWall + "\""
4040
w.Header().Set("Content-Disposition", disposition)
4141
w.Header().Set("Content-Type", "text/plain")
4242
_, err := fmt.Fprintf(w, "%s", s.WallContent)

0 commit comments

Comments
 (0)