Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions config/storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"mime"
"net/http"
"path/filepath"
Expand Down Expand Up @@ -158,3 +159,36 @@ func (f *File) TimeRemaining() time.Duration {
return time.Until(
f.Time.Upload.Add(f.Time.Duration)).Round(time.Second)
}

// Returns File, if found by name
func (s *Storage) FindFile(name string) *File {
var file *File
for _, f := range s.Files {
if f.Name == name {
file = f
break
}
}
return file
}

// Serves File as HTTP response
func (f *File) Serve(w http.ResponseWriter) {
w.Header().Set("Content-Type", f.MimeType())
w.Header().Set("Content-Disposition", "attachment; filename="+f.Name)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(f.Data)
f.Total++
}

// Serves Messages as HTTP response
func (s *Storage) ServeMessages(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Content-Disposition", "attachment; filename=messages.txt")
for _, msg := range s.Messages {
_, err := fmt.Fprintf(w, "%d. %s\n", msg.Count, msg.Data)
if err != nil {
return
}
}
}
17 changes: 3 additions & 14 deletions handlers/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,14 @@ func Download(app *config.App) http.HandlerFunc {
return
}

var file *config.File
var found bool

for _, f := range app.Files {
if f.Name == fileName {
file = f
found = true
break
}
}

if !found {
file := app.FindFile(fileName)
if file == nil {
writeJSON(w, http.StatusNotFound, errorJSON(app.NotFound))
app.Log.Error(app.NotFound, "filename", fileName, "user", req)
return
}

writeFile(w, file)
file.Total++
file.Serve(w)
app.Log.Info("served file",
"filename", file.Name, "size", file.Size,
"downloads", file.Total, "user", req)
Expand Down
2 changes: 1 addition & 1 deletion handlers/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Index(app *config.App) http.HandlerFunc {
app.Cookie.Id, app.Cookie.Time.GetDuration())
}

tmplName := "index.tmpl"
tmplName := "index"
tmpl, err := template.New(tmplName).ParseFS(templates.All, "data/*.tmpl")
if err != nil {
writeJSON(w, http.StatusInternalServerError, errorJSON(app.TmplParse))
Expand Down
13 changes: 11 additions & 2 deletions handlers/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ func Message(app *config.App) http.HandlerFunc {

if r.Method == http.MethodPost {
if r.FormValue("clear") != "" {
app.Log.Debug("clearing messages",
"count", app.CountMessages(), "user", req)
app.ClearMessages()
app.Log.Debug("cleared messages", "user", req)
}

message := config.Message{
Expand All @@ -38,12 +39,20 @@ func Message(app *config.App) http.HandlerFunc {
message.Data = content
app.Messages[message.Count] = &message
app.Log.Debug("added message",
"message", content, "user", req)
"count", message.Count,
"content", message.Data, "user", req)
}

http.Redirect(w, r, "/", http.StatusSeeOther)
}

if r.URL.Query().Get("download") == "all" {
app.Log.Debug("serving all messages",
"count", app.CountMessages(), "user", req)
app.ServeMessages(w)
return
}

writeJSON(w, http.StatusOK, app.Messages)
}
}
10 changes: 1 addition & 9 deletions handlers/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func deny(w http.ResponseWriter, app *config.App, r *Request) {
func isAllowed(app *config.App, r *http.Request) bool {
reqs := map[string]bool{
app.Download: app.Require.Download,
app.Message: app.Require.Message,
app.List: app.Require.List,
app.Message: app.Require.Message,
app.Upload: app.Require.Upload,
}
app.Log.Debug("checking auth", "path", r.URL.Path)
Expand Down Expand Up @@ -84,11 +84,3 @@ func writeJSON(w http.ResponseWriter, code int, data interface{}) {
return
}
}

// Writes File response with content
func writeFile(w http.ResponseWriter, f *config.File) {
w.Header().Set("Content-Type", f.MimeType())
w.Header().Set("Content-Disposition", "attachment; filename="+f.Name)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(f.Data)
}
21 changes: 8 additions & 13 deletions templates/data/assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
box-sizing: border-box;
}

button, input, select {
transition: all var(--animation) ease;
}

body {
background: var(--bg-light);
background: linear-gradient(0deg, var(--bg-light) 0%, var(--bg-dark) 100%);
background-attachment: fixed;
color: var(--fg-dark);
Expand All @@ -16,14 +19,12 @@ body {
padding: 0;
}

a:link,
a:visited {
a:link, a:visited {
color: var(--fg-light);
text-decoration: none;
}

a:active,
a:hover {
a:active, a:hover {
color: var(--fg-light);
text-decoration: underline;
}
Expand Down Expand Up @@ -57,10 +58,6 @@ a:hover {
width: 100%;
}

*, button, input, select, a {
transition: all var(--animation) ease;
}

button[type="submit"],
input[type="checkbox"],
input[type="file"],
Expand Down Expand Up @@ -115,15 +112,13 @@ input[type="number"] {
border-radius: var(--box-radius);
bottom: 2rem;
color: var(--fg-light);
left: 50%;
margin-left: -12rem;
opacity: 0;
padding: 6px 0;
position: absolute;
text-align: center;
transform: translateX(-55%);
visibility: hidden;
width: 24em;
z-index: 1;
z-index: 10;
}

.message .owner::after {
Expand Down
4 changes: 2 additions & 2 deletions templates/data/header.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="{{ .Version.id }}-{{ .Version.vers }}">
{{ if .Index.CSP }}
{{ template "csp" . }}
{{- template "csp" . -}}
{{ end }}
{{ if .Theme }}
<link rel="stylesheet" type="text/css" href="/assets/style.css">
<style>
:root {
{{ template "theme" . }}
{{- template "theme" . }}
}
</style>
{{ end }}
Expand Down
24 changes: 8 additions & 16 deletions templates/data/index.tmpl
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
<!-- index.tmpl -->
{{ template "header" . }}
{{ define "index" }}
{{- template "header" . -}}
<div class="container">
{{ if .Paths.Upload }}
{{ template "upload" . }}
{{ end }}

{{ if .Paths.Download }}
{{ template "download" . }}
{{ end }}

{{ if .Paths.List }}
{{ template "list" . }}
{{ end }}
{{ if .Paths.Upload }} {{- template "upload" . -}} {{ end }}
{{ if .Paths.Download }} {{- template "download" . -}} {{ end }}
{{ if .Paths.List }} {{- template "list" . -}} {{ end }}
</div>
<div class="container">
{{ if .Paths.Message }}
{{ template "message" . }}
{{ end }}
{{ if .Paths.Message }} {{- template "message" . -}} {{ end }}
</div>
{{ template "footer" . }}
{{- template "footer" . -}}
{{ end }}
13 changes: 10 additions & 3 deletions templates/data/message.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
name="clear"
type="checkbox">
<br>
{{ if .Auth.Require.Message }}
{{ template "auth" . }}
{{ end }}
{{ if .Auth.Require.Message }} {{ template "auth" . }} {{ end }}
<input type="submit"
class="selectSubmit"
value="post">
Expand All @@ -34,5 +32,14 @@
<button class="selectRefresh"
type="submit">refresh</button>
</form>
{{ if .Storage.Messages }}
<form action="{{ .Paths.Message }}">
<input type="hidden"
name="download"
value="all">
<button class="selectRefresh"
type="submit">download</button>
</form>
{{ end }}
</div>
{{ end }}
Loading
Loading