aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go34
-rw-r--r--static/style.css9
-rw-r--r--templates/user.html27
3 files changed, 61 insertions, 9 deletions
diff --git a/main.go b/main.go
index 1194c50..1eacfa9 100644
--- a/main.go
+++ b/main.go
@@ -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)
diff --git a/static/style.css b/static/style.css
index fb580ac..4db3552 100644
--- a/static/style.css
+++ b/static/style.css
@@ -5,3 +5,12 @@
div {
padding: 5px;
}
+
+table {
+ border-collapse: collapse;
+}
+
+td, th {
+ border: 1px solid black;
+ padding: 10px;
+}
diff --git a/templates/user.html b/templates/user.html
index 7cb0d11..5e0aa57 100644
--- a/templates/user.html
+++ b/templates/user.html
@@ -7,12 +7,29 @@
<input type="file" name="files" multiple />
<input type="submit" value="Upload" />
</form>
- <ul>
+ <table>
+ <tr>
+ <th></th>
+ <th>Nom du fichier</th>
+ <th>Date de modification</th>
+ <th>Taille</th>
+ </tr>
{{range .Docs}}
- <li>
- <a href="{{.Link}}">{{.Name}}</a>
- </li>
+ <tr>
+ <td>
+ <input type="checkbox">
+ </td>
+ <td>
+ <a href="{{.Link}}">{{.Name}}</a>
+ </td>
+ <td>
+ {{.ModTime}}
+ </td>
+ <td>
+ {{.Size}}
+ </td>
+ </tr>
{{end}}
- </ul>
+ </table>
</div>
{{end}}