feat: add file uploading functionality
This commit is contained in:
parent
7ff26c5d93
commit
cd467d26d0
2
.gitignore
vendored
2
.gitignore
vendored
@ -1 +1 @@
|
||||
images/
|
||||
files/
|
||||
|
16
README.md
16
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://)
|
||||
|
19
main.go
19
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()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user