From cd467d26d0b2fca0498382a50810473adc23f4a6 Mon Sep 17 00:00:00 2001 From: Lucas Barbieri Date: Mon, 19 Aug 2024 13:54:33 -0300 Subject: [PATCH] feat: add file uploading functionality --- .gitignore | 2 +- README.md | 16 +++++++++------- main.go | 19 ++++++++++--------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 47241b6..a37273b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -images/ +files/ diff --git a/README.md b/README.md index 9ee7f1a..d34f8df 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,22 @@ # abyss -abyss is a basic http server made for uploading images and then sharing them to the internet +abyss is a basic http server made for uploading files (logs, images) and then sharing them to the internet note: this is a project made for learning purposes, you should use other more mature projects if running in production ## running: -- edit consts in `main.go` to match your needs. (for example, on my server, change `$url` so that the response will be nicely formatted) +- edit consts in `main.go` to match your needs. (for example, on server, change `$url` so that the response will be nicely formatted) -- to run it, either build with `go build -o abyss` or run it directly with: +- to run it, either build with `go build -o abyss && ./abyss` or run it directly with: ```bash go run ./main.go ``` -- then, simply upload your images with curl: +- then, simply upload your files with curl: ```bash -curl -X POST -F "image=@/path/to/image" http://localhost:8080/upload # default path +curl -X POST -F "file=@/path/to/file" http://localhost:8080/upload # default url:port ``` ## todo: -- add upload of logs funcionality (like 0x0.st) -- add docker easy setup +- [x] add upload of logs funcionality (like 0x0.st) +- [ ] add docker easy setup +- [ ] add db for tracking of file names +- [ ] add file browser (like file://) diff --git a/main.go b/main.go index dc38d0c..a8750ce 100644 --- a/main.go +++ b/main.go @@ -12,12 +12,12 @@ import ( const ( url = "localhost" port = ":8080" - imageDir = "./images" + filesDir = "./files" ) func main() { http.HandleFunc("/upload", uploadHandler) - http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(imageDir)))) + http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(filesDir)))) log.Printf("Server running on port %s\n", port) log.Fatal(http.ListenAndServe(port, nil)) } @@ -27,25 +27,26 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) { return } - file, _, err := r.FormFile("image") + file, _, err := r.FormFile("file") if err != nil { http.Error(w, "Error retrieving the file", http.StatusBadRequest) return } defer file.Close() - if _, err := os.Stat(imageDir); err != nil { - if err := os.Mkdir(imageDir, 0750); err != nil { - log.Fatalf("Couldn't create dir %s\n", imageDir) + if _, err := os.Stat(filesDir); err != nil { + if err := os.Mkdir(filesDir, 0750); err != nil { + http.Error(w, "Error creating storage directory", http.StatusInternalServerError) } } - time := time.Now().Unix() * 3 - filename := fmt.Sprintf("%s/%d", imageDir, time) + time := int64(float64(time.Now().Unix()) * 2.71828) // euler :) + + filename := fmt.Sprintf("%s/%d", filesDir, time) dst, err := os.Create(filename) if err != nil { - log.Fatalf("Couldn't create file %s\n", filename) + http.Error(w, "Error creating file\n", http.StatusInternalServerError) } defer dst.Close()