diff options
author | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-08-28 13:47:44 +0300 |
---|---|---|
committer | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-08-28 13:47:44 +0300 |
commit | 459b87480e76d9be275b0ee8a30e4b691af8aaba (patch) | |
tree | 6981c676d5c52c955240b464601d9311fb30a888 /upload.go |
initial
Diffstat (limited to 'upload.go')
-rw-r--r-- | upload.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/upload.go b/upload.go new file mode 100644 index 0000000..719e519 --- /dev/null +++ b/upload.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "io" + "log" + "net/http" + "net/url" + "os" + "path/filepath" + + "github.com/google/uuid" +) + +func uploadHandler(w http.ResponseWriter, r *http.Request) { + uid := uuid.New().String() + section := string(uid[len(uid)-1]) + if err := os.MkdirAll(filepath.Join(path, section), 0777); err != nil { + log.Println(err) + http.Error(w, "can't create storage dir", http.StatusInternalServerError) + return + } + fp, err := os.Create(filepath.Join(path, section, uid)) + if err != nil { + log.Println(err) + http.Error(w, "can't create storage file", http.StatusInternalServerError) + return + } + defer fp.Close() + if _, err := io.Copy(fp, r.Body); err != nil { + log.Println(err) + http.Error(w, "can't upload storage file", http.StatusInternalServerError) + return + } + h := w.Header() + h.Set("Content-Type", "text/plain; charset=utf-8") + h.Set("X-Content-Type-Options", "nosniff") + w.WriteHeader(http.StatusCreated) + u, err := url.JoinPath(host, section, uid) + if err != nil { + log.Println(err) + http.Error(w, "can't get file url", http.StatusInternalServerError) + return + } + fmt.Fprintln(w, u) +} |