summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarin Ivanov <[email protected]>2026-01-17 01:23:19 +0200
committerMarin Ivanov <[email protected]>2026-01-17 01:23:19 +0200
commit858c7e8f1ff1fd4878f0276881f6048043507f9f (patch)
tree22643c06b77e22c07c005b2449982e6e80eb0922
parent587087175a1f656cd126592d0b8987dd0cdfd001 (diff)
single endpoint for posting
-rw-r--r--form.html2
-rw-r--r--main.go103
2 files changed, 47 insertions, 58 deletions
diff --git a/form.html b/form.html
index 6e556d5..f305eed 100644
--- a/form.html
+++ b/form.html
@@ -144,7 +144,7 @@
<div id="error"></div>
- <form enctype="multipart/form-data" action="/post" method="post">
+ <form enctype="multipart/form-data" action="/" method="post">
<fieldset>
<legend>Text Posting</legend>
<textarea name="text" placeholder="Write something..."></textarea>
diff --git a/main.go b/main.go
index 6e36b82..1df5c3f 100644
--- a/main.go
+++ b/main.go
@@ -107,10 +107,8 @@ func (s *Server) randomFile(ext string) (*os.File, string, error) {
func (s *Server) createPosting(rd io.Reader, mimeType string) (string, error) {
var ext string
- if mimeType == "" || mimeType == "auto" || mimeType == "application/x-www-form-urlencoded" {
+ if mimeType == "" || mimeType == "auto" {
mimeType, ext, rd, _ = detectMimetype(rd)
- } else if mimeType == "text/plain" {
- ext = ".txt"
} else {
mime := mimetype.Lookup(mimeType)
if mime != nil {
@@ -154,42 +152,6 @@ func (s *Server) postFile(r *http.Request) (string, error) {
return s.createPosting(file, mimeType)
}
-func (s *Server) formPost(w http.ResponseWriter, r *http.Request) {
- r.ParseMultipartForm(10 << 23) // 83MB
-
- var textLink string
- var fileLink string
-
- textName, err := s.postText(r)
- if err != nil {
- log.Print("Error text upload: ", err)
- } else if textName != "" {
- log.Printf("Created posting: %s", textName)
- textLink = fmt.Sprintf("%s/%s", s.host, textName)
- }
-
- fileName, err := s.postFile(r)
- if err != nil {
- log.Print("Error file upload: ", err)
- } else if fileName != "" {
- log.Printf("Created posting: %s", fileName)
- fileLink = fmt.Sprintf("%s/%s", s.host, fileName)
- }
-
- if fileName == "" && textName == "" {
- w.WriteHeader(http.StatusInternalServerError)
- return
- }
-
- tmplData := map[string]string{
- "TextLink": textLink,
- "FileLink": fileLink,
- }
- if err := tmpl.ExecuteTemplate(w, "result.html", tmplData); err != nil {
- w.WriteHeader(http.StatusInternalServerError)
- }
-}
-
func (s *Server) servePosting(w http.ResponseWriter, r *http.Request, name string) {
filename := filepath.Join(s.data, name)
m, err := readMetadata(filename + MetadataExt)
@@ -226,15 +188,52 @@ func (s *Server) indexPost(w http.ResponseWriter, r *http.Request) {
return
}
mimeType := r.Header.Get("content-type")
- name, err := s.createPosting(r.Body, mimeType)
- if err != nil {
- log.Print("failed to create file: ", err)
- w.WriteHeader(http.StatusInternalServerError)
- return
+ if strings.HasPrefix(mimeType, "multipart/form-data;") {
+ r.ParseMultipartForm(10 << 23) // 83MB
+ textName, err := s.postText(r)
+ if err != nil {
+ log.Print("Error text upload: ", err)
+ }
+ fileName, err := s.postFile(r)
+ if err != nil {
+ log.Print("Error file upload: ", err)
+ }
+ if fileName == "" && textName == "" {
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+
+ var textLink string
+ var fileLink string
+ if textName != "" {
+ log.Printf("Created posting: %s", textName)
+ textLink = fmt.Sprintf("%s/%s", s.host, textName)
+ }
+ if fileName != "" {
+ log.Printf("Created posting: %s", fileName)
+ fileLink = fmt.Sprintf("%s/%s", s.host, fileName)
+ }
+ tmplData := map[string]string{
+ "TextLink": textLink,
+ "FileLink": fileLink,
+ }
+ if err := tmpl.ExecuteTemplate(w, "result.html", tmplData); err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ }
+ } else {
+ if mimeType == "application/x-www-form-urlencoded" {
+ mimeType = "auto"
+ }
+ name, err := s.createPosting(r.Body, mimeType)
+ if err != nil {
+ log.Print("failed to create file: ", err)
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ log.Printf("Created posting: %s", name)
+ w.WriteHeader(http.StatusCreated)
+ fmt.Fprintf(w, "%s/%s\n", s.host, name)
}
- log.Printf("Created posting: %s", name)
- w.WriteHeader(http.StatusCreated)
- fmt.Fprintf(w, "%s/%s\n", s.host, name)
}
func (s *Server) indexGet(w http.ResponseWriter, r *http.Request) {
@@ -249,15 +248,6 @@ func (s *Server) indexGet(w http.ResponseWriter, r *http.Request) {
}
}
-func (s *Server) Post(w http.ResponseWriter, r *http.Request) {
- switch r.Method {
- case http.MethodPost:
- s.formPost(w, r)
- default:
- w.WriteHeader(http.StatusBadRequest)
- }
-}
-
func (s *Server) Index(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
@@ -289,7 +279,6 @@ func main() {
host: host,
}
mux := http.NewServeMux()
- mux.HandleFunc("/post", s.Post)
mux.HandleFunc("/", s.Index)
log.Printf("Listening at '%s'...", addr)
if err := http.ListenAndServe(addr, mux); err != nil {