Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
2025-05-01 04:53:33 +03:00
parent ea387d48b5
commit 4e98ae5334
2 changed files with 40 additions and 25 deletions

View File

@@ -15,7 +15,7 @@
packages = rec { packages = rec {
slenpaste = pkgs.buildGoModule { slenpaste = pkgs.buildGoModule {
pname = "slenpaste"; pname = "slenpaste";
version = "0.1.0"; version = "0.1.1";
src = ./.; src = ./.;
goPackagePath = "github.com/slendidev/slenpaste"; goPackagePath = "github.com/slendidev/slenpaste";
vendorHash = null; vendorHash = null;

63
main.go
View File

@@ -73,45 +73,50 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
} }
var reader io.Reader var reader io.Reader
var ext string
// handle multipart file upload
contentType := r.Header.Get("Content-Type") contentType := r.Header.Get("Content-Type")
if strings.HasPrefix(contentType, "multipart/form-data") { if strings.HasPrefix(contentType, "multipart/form-data") {
if err := r.ParseMultipartForm(10 << 20); err == nil { if err := r.ParseMultipartForm(10 << 20); err == nil {
if file, _, err := r.FormFile("file"); err == nil { if file, header, err := r.FormFile("file"); err == nil {
defer file.Close() defer file.Close()
reader = file reader = file
ext = filepath.Ext(header.Filename)
} }
} }
} }
// fallback to body upload
if reader == nil { if reader == nil {
reader = r.Body reader = r.Body
defer r.Body.Close() defer r.Body.Close()
} }
expVal := r.URL.Query().Get("expiry") // default extension
var dur time.Duration if ext == "" {
var onView bool ext = ".txt"
switch expVal {
case "view":
onView = true
case "0":
// no expiry
default:
dur, _ = time.ParseDuration(expVal)
} }
id := randomID(6) // generate ID and filename
id := randomID(6)
filename := id + ext
// ensure storage dir
if err := os.MkdirAll(staticDir, 0755); err != nil { if err := os.MkdirAll(staticDir, 0755); err != nil {
http.Error(w, "Server error", http.StatusInternalServerError) http.Error(w, "Server error", http.StatusInternalServerError)
return return
} }
path := filepath.Join(staticDir, id) path := filepath.Join(staticDir, filename)
// save file
out, err := os.Create(path) out, err := os.Create(path)
if err != nil { if err != nil {
http.Error(w, "Save error", http.StatusInternalServerError) http.Error(w, "Save error", http.StatusInternalServerError)
return return
} }
defer out.Close() defer out.Close()
n, err := io.Copy(out, reader) n, err := io.Copy(out, reader)
if err != nil { if err != nil {
http.Error(w, "Write error", http.StatusInternalServerError) http.Error(w, "Write error", http.StatusInternalServerError)
@@ -123,16 +128,26 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
if dur > 0 || onView { // write metadata if needed
m := meta{ExpireOnView: onView} expVal := r.URL.Query().Get("expiry")
if dur > 0 { var m meta
m.Expiry = time.Now().Add(dur) switch expVal {
case "view":
m.ExpireOnView = true
case "0":
// no expiry
default:
if d, err := time.ParseDuration(expVal); err == nil {
m.Expiry = time.Now().Add(d)
} }
}
if !m.Expiry.IsZero() || m.ExpireOnView {
metaBytes, _ := json.Marshal(m) metaBytes, _ := json.Marshal(m)
_ = os.WriteFile(path+".json", metaBytes, 0644) _ = os.WriteFile(path+".json", metaBytes, 0644)
} }
fmt.Fprintf(w, "http://%s/%s\n", domain, id) // respond with URL including extension
fmt.Fprintf(w, "http://%s/%s\n", domain, filename)
} }
func viewHandler(w http.ResponseWriter, r *http.Request) { func viewHandler(w http.ResponseWriter, r *http.Request) {
@@ -141,7 +156,7 @@ func viewHandler(w http.ResponseWriter, r *http.Request) {
indexHandler(w, r) indexHandler(w, r)
return return
} }
path := filepath.Join(staticDir, id) path := filepath.Join(staticDir, id)
metaPath := path + ".json" metaPath := path + ".json"
// load and enforce metadata // load and enforce metadata
@@ -172,11 +187,11 @@ func viewHandler(w http.ResponseWriter, r *http.Request) {
} }
func main() { func main() {
flag.StringVar(&domain, "domain", "localhost:8080", "domain name for URLs") flag.StringVar(&domain, "domain", "localhost:8080", "domain name for URLs")
flag.StringVar(&listenAddr, "listen", "0.0.0.0:8080", "listen address") flag.StringVar(&listenAddr, "listen", "0.0.0.0:8080", "listen address")
flag.StringVar(&staticDir, "static", "static", "directory to save pastes") flag.StringVar(&staticDir, "static", "static", "directory to save pastes")
flag.DurationVar(&expireDur, "expire", 0, "time after which paste expires (e.g. 5m, 1h)") flag.DurationVar(&expireDur, "expire", 0, "time after which paste expires (e.g. 5m, 1h)")
flag.BoolVar(&expireOnView, "expire-on-view", false, "delete paste after it's viewed once") flag.BoolVar(&expireOnView, "expire-on-view", false, "delete paste after it's viewed once")
flag.Parse() flag.Parse()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {