diff options
| author | Julian Hurst <ark@mansus.space> | 2023-01-13 10:40:11 +0100 |
|---|---|---|
| committer | Julian Hurst <ark@mansus.space> | 2023-01-13 10:40:11 +0100 |
| commit | c3de52dff91b81cfd07e00fb7fc6dd701bf8b7d7 (patch) | |
| tree | 4bf902ee59146a29bc6f60b33af0a4b9aea449c2 /main.go | |
| parent | 05a30c5ad503ff2bc25cf6f83f9054100f644eeb (diff) | |
| download | docspace-c3de52dff91b81cfd07e00fb7fc6dd701bf8b7d7.tar.gz | |
Add size and modtime to docs and redirect to login page by default
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 34 |
1 files changed, 30 insertions, 4 deletions
@@ -24,10 +24,13 @@ var db *sql.DB const baseDocDir string = "docs" +// Use uuid for session ids to prevent spoofing session cookies var sessionIds sync.Map type Doc struct { Name string + Size string + ModTime string Link string } @@ -73,7 +76,12 @@ func checkSession(w http.ResponseWriter, r *http.Request) (*User, error) { return nil, err } if sessionId, ok := sessionIds.Load(user.User.User); !ok || sessionId != user.SessionId { - return nil, errors.New("Invalid session ID") + http.SetCookie(w, &http.Cookie{ + Name: "session", + Value: "", + MaxAge: -1, + }) + return nil, nil } return &user.User, nil } @@ -97,6 +105,17 @@ func sendInvalidMethod(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusMethodNotAllowed) } +func humanize(i int64) string { + var sizes [4]string = [4]string {"O", "K", "M", "G"} + j := i + s := 0 + for j > 1024 && s < len(sizes) { + j = j / 1024.0 + s++ + } + return fmt.Sprintf("%v%s", j, sizes[s]) +} + func index(w http.ResponseWriter, r *http.Request) { u, err := checkSession(w, r) if u != nil && err == nil { @@ -122,8 +141,15 @@ func index(w http.ResponseWriter, r *http.Request) { return info1.ModTime().After(info2.ModTime()) }) for _, file := range files { + info, err := file.Info() + if err != nil { + sendError(w, r, err.Error(), http.StatusInternalServerError) + return + } docs = append(docs, Doc { file.Name(), + humanize(info.Size()), + info.ModTime().Format("2006-01-02"), path.Join(baseDocDir, u.User, file.Name()), }) } @@ -138,7 +164,7 @@ func index(w http.ResponseWriter, r *http.Request) { sendError(w, r, err.Error(), http.StatusInternalServerError) return } - serveTemplate(w, r, "templates/index.html", nil) + http.Redirect(w, r, "/login", http.StatusSeeOther) } func admin(w http.ResponseWriter, r *http.Request) { @@ -319,7 +345,7 @@ func handleFileServer(dir, prefix string) http.HandlerFunc { return } } - sendError(w, r, "Unauthorized", http.StatusUnauthorized) + http.Redirect(w, r, "/login", http.StatusSeeOther) } } @@ -357,7 +383,7 @@ func upload(w http.ResponseWriter, r *http.Request) { } http.Redirect(w, r, "/", http.StatusSeeOther) } else { - sendError(w, r, "Unauthorized", http.StatusUnauthorized) + http.Redirect(w, r, "/login", http.StatusSeeOther) } default: sendInvalidMethod(w, r) |
