diff --git a/config.go b/config.go index fe49111..8cb7b56 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,8 @@ type Config struct { Expiries []Expiry DatabasePath string + // public host like "https://bin.zip", NOT the address minbin listens on + Host string } func loadConfig() (Config, error) { @@ -25,7 +27,6 @@ func loadConfig() (Config, error) { log.Fatal("Error loading .env file") } - uploadDir := envOrDefault("MINIBIN_UPLOAD_DIR", "uploads") pasteSizeSetting := envOrDefault("MINIBIN_MAX_PASTE_KIB", "1024") maxPasteKiB, err := strconv.ParseInt(pasteSizeSetting, 10, 64) if err != nil { @@ -44,8 +45,6 @@ func loadConfig() (Config, error) { return Config{}, fmt.Errorf("MINIBIN_MAX_FILE_KIB must be greater than 0") } - databasePath := envOrDefault("MINIBIN_DATABASE_PATH", "minibin.sqlite") - expiriesSetting := envOrDefault("MINIBIN_EXPIRIES", "1h,12h,1d,7d,2w,1mo,6mo,1y,5y,0") var expiries []Expiry for entry := range strings.SplitSeq(expiriesSetting, ",") { @@ -57,12 +56,13 @@ func loadConfig() (Config, error) { } return Config{ - UploadDir: uploadDir, + UploadDir: envOrDefault("MINIBIN_UPLOAD_DIR", "uploads"), MaxPasteBytes: maxPasteKiB * 1024, MaxFileBytes: maxFileKiB * 1024, Expiries: expiries, - DatabasePath: databasePath, + DatabasePath: envOrDefault("MINIBIN_DATABASE_PATH", "minibin.sqlite"), + Host: envOrDefault("MINIBIN_HOST", "http://localhost:8080"), }, nil } diff --git a/handlers.go b/handlers.go index 171e086..bc8eb33 100644 --- a/handlers.go +++ b/handlers.go @@ -62,7 +62,8 @@ func staticPage(pages *template.Template, name string) http.HandlerFunc { func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodPost { + if r.Method != http.MethodPost && r.Method != http.MethodPut { + w.Header().Set("Allow", "POST,PUT") http.Error(w, "invalid method", http.StatusMethodNotAllowed) return } @@ -70,10 +71,14 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template) http.Handl // use the largest of the sizes, and then do another size check per type down the line r.Body = http.MaxBytesReader(w, r.Body, max(cfg.MaxFileBytes, cfg.MaxPasteBytes)) - mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type")) - if err != nil { - http.Error(w, "failed to find or parse Content-Type", http.StatusBadRequest) - return + var err error + mediaType := "text/plain" + if r.Method == http.MethodPost || r.Header.Get("Content-Type") != "" { + mediaType, _, err = mime.ParseMediaType(r.Header.Get("Content-Type")) + if err != nil { + http.Error(w, "failed to find or parse Content-Type", http.StatusBadRequest) + return + } } idLength := 16 @@ -86,20 +91,6 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template) http.Handl returnType = "json" } - expLabel := r.URL.Query().Get("expiry") - if expLabel == "" { - expLabel = r.PostFormValue("expiry") // html form request - } - if expLabel == "" { - expLabel = cfg.Expiries[0].Label - } - - selected, found := findExpiry(cfg.Expiries, expLabel) - if !found { - http.Error(w, "invalid expiry", http.StatusBadRequest) - return - } - var input UploadInput switch mediaType { case "multipart/form-data": // file uploads @@ -132,13 +123,13 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template) http.Handl r.Body = http.MaxBytesReader(w, r.Body, cfg.MaxPasteBytes) var content string if mediaType == "application/x-www-form-urlencoded" { - err = r.ParseForm() + err := r.ParseForm() if err == nil { content = r.PostForm.Get("content") } } else { var body []byte - body, err = io.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) if err == nil { content = string(body) } @@ -170,6 +161,20 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template) http.Handl return } + expLabel := r.URL.Query().Get("expiry") + if expLabel == "" { + expLabel = r.PostForm.Get("expiry") // html form request + } + if expLabel == "" { + expLabel = cfg.Expiries[0].Label + } + + selected, found := findExpiry(cfg.Expiries, expLabel) + if !found { + http.Error(w, "invalid expiry", http.StatusBadRequest) + return + } + id := generateId(idLength) path := getStoragePath(cfg.UploadDir, id) if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { diff --git a/main.go b/main.go index c3ea2e1..be6d6db 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,7 @@ func main() { "templates/upload.html", "templates/tos.html", "templates/privacy.html", + "templates/faq.html", ) if err != nil { log.Fatal(err) @@ -99,13 +100,19 @@ func main() { data.PastePlaceholder = placeholders[rand.IntN(len(placeholders))] } - w.Header().Set("Content-Type", "text/html; charset=utf-8") render(pages, w, "home.html", data) }) mux.HandleFunc("GET /tos", staticPage(pages, "tos.html")) mux.HandleFunc("GET /privacy", staticPage(pages, "privacy.html")) + mux.HandleFunc("GET /faq", func(w http.ResponseWriter, r *http.Request) { + render(pages, w, "faq.html", map[string]any{ + "Expiries": config.Expiries, + "Host": config.Host, + }) + }) mux.HandleFunc("GET /{id}", contentHandler(db, config)) mux.HandleFunc("POST /upload", uploadHandler(db, config, pages)) + mux.HandleFunc("PUT /upload", uploadHandler(db, config, pages)) mux.HandleFunc("DELETE /{id}", deleteHandler(db, config)) mux.HandleFunc("POST /{id}/delete", deleteHandler(db, config)) mux.HandleFunc("GET /health", health) diff --git a/static/css/main.css b/static/css/main.css index 486fe19..e0314c5 100644 --- a/static/css/main.css +++ b/static/css/main.css @@ -35,6 +35,10 @@ h2 { font-weight: 400; } +h4 { + margin: 6px 0; +} + h1 { font-size: 18px; font-weight: 700; @@ -237,3 +241,8 @@ body.dragging::after { color: var(--text-muted); font-weight: bold; } + +code { + background-color: var(--surface); + padding-inline: 4px; +} \ No newline at end of file diff --git a/templates/faq.html b/templates/faq.html new file mode 100644 index 0000000..c0ca2b2 --- /dev/null +++ b/templates/faq.html @@ -0,0 +1,41 @@ + + + + + + faq | minibin + + + + {{template "site-header"}} +
+

frequently asked questions

+

how long are uploads/files kept for?

+

+ minibin will store them for as long as their set expiry, unless they + violate the TOS or have not been accessed in a long time. +

+

are uploads modified in any way?

+

+ no, all uploads are exactly the same as what you uploaded. uploads are + not compressed, transformed, or modified in any way, not even exif data. +

+

+ be wary of any metadata included in the files you are uploading, + especially photos from a phone as they may contain GPS data. +

+

how do i upload via the api?

+

send a POST request to /upload. for files, use multipart form data with a field named file. for pastes, send the text with Content-Type: text/plain.

+ +

+ to add an expiry, append ?expiry={value}.
supported values are: + {{range $i, $expiry := .Expiries}}{{if $i}},{{end}}{{$expiry.Label}}{{end}}. + omitting an expiry will use the first expiry listed. +

+

uploading the output of a command

+ cat super-cool-text.txt | curl -T - {{.Host}} +

upload a file via curl

+ curl -F "file=@very-cool-image.png" localhost:8080/upload +
+ + diff --git a/templates/header.html b/templates/header.html index 82584c9..0b58961 100644 --- a/templates/header.html +++ b/templates/header.html @@ -1,6 +1,6 @@ {{define "site-header"}} -{{end}} \ No newline at end of file +{{end}}