faq page; put uploads; config
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
+26
-21
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>faq | minibin</title>
|
||||
<link rel="stylesheet" href="/css/main.css" />
|
||||
</head>
|
||||
<body>
|
||||
{{template "site-header"}}
|
||||
<main>
|
||||
<h2>frequently asked questions</h2>
|
||||
<h3>how long are uploads/files kept for?</h3>
|
||||
<p>
|
||||
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.
|
||||
</p>
|
||||
<h3>are uploads modified in any way?</h3>
|
||||
<p>
|
||||
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.
|
||||
</p>
|
||||
<p>
|
||||
be wary of any metadata included in the files you are uploading,
|
||||
especially photos from a phone as they may contain GPS data.
|
||||
</p>
|
||||
<h3>how do i upload via the api?</h3>
|
||||
<p>send a POST request to <code>/upload</code>. for files, use multipart form data with a field named <code>file</code>. for pastes, send the text with <code>Content-Type: text/plain</code>.</p>
|
||||
|
||||
<p>
|
||||
to add an expiry, append <code>?expiry={value}</code>. <br>supported values are:
|
||||
<code>{{range $i, $expiry := .Expiries}}{{if $i}},{{end}}{{$expiry.Label}}{{end}}</code>.
|
||||
omitting an expiry will use the first expiry listed.
|
||||
</p>
|
||||
<h4>uploading the output of a command</h4>
|
||||
<code>cat super-cool-text.txt | curl -T - {{.Host}}</code>
|
||||
<h4>upload a file via curl</h4>
|
||||
<code>curl -F "file=@very-cool-image.png" localhost:8080/upload</code>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,6 +1,6 @@
|
||||
{{define "site-header"}}
|
||||
<div class="site-header">
|
||||
<h1><a style="text-decoration: none;" href="/">minibin</a></h1>
|
||||
<h1><a style="text-decoration: none" href="/">minibin</a></h1>
|
||||
<div>
|
||||
<a href="/tos">tos</a>
|
||||
<a href="/privacy">privacy</a>
|
||||
@@ -8,4 +8,4 @@
|
||||
<a href="https://git.whoswhip.dev/whoswhip/minibin">src</a>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
Reference in New Issue
Block a user