Skip to content

Commit

Permalink
basic file uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
w3irdrobot committed Jan 27, 2023
1 parent 5a4965a commit d4dc63b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploads
62 changes: 62 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"io"
"net/http"
"os"
"path/filepath"
)

const MAX_UPLOAD_SIZE = 5 * 1024 * 1024 // 5MB

func upload(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}

r.Body = http.MaxBytesReader(w, r.Body, MAX_UPLOAD_SIZE)
if err := r.ParseMultipartForm(MAX_UPLOAD_SIZE); err != nil {
http.Error(w, "file too big. must be under 5MB", http.StatusBadRequest)
return
}

file, fileHeader, err := r.FormFile("file")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer file.Close()

// make buffer used to detect content type
buff := make([]byte, 512)
if _, err := file.Read(buff); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

filetype := http.DetectContentType(buff)
if filetype != "image/jpeg" && filetype != "image/png" {
http.Error(w, "invalid format. only jpeg and png are accepted", http.StatusBadRequest)
return
}

// seek back to beginning to get whole file
if _, err := file.Seek(0, io.SeekStart); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

dst, err := os.Create(filepath.Join(".", "uploads", fileHeader.Filename))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer dst.Close()

_, err = io.Copy(dst, file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
run:
go run ./cmd/uploadr
go run .
19 changes: 13 additions & 6 deletions cmd/uploadr/main.go → main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ import (
)

func main() {
logger := logrus.New()
// create directory to hold uploads
err := os.MkdirAll("./uploads", os.ModePerm)
if err != nil {
logrus.WithError(err).Fatal("unable to create the upload directory")
return
}

router := http.NewServeMux()
router.HandleFunc("/upload", upload)
server := http.Server{
Addr: ":8080",
Handler: router,
Expand All @@ -29,18 +36,18 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
logger.WithError(err).Fatal("graceful shutdown failed")
logrus.WithError(err).Fatal("graceful shutdown failed")
}
close(shutdownComplete)
}()

logger.Info("server started on :8080")
logrus.Info("server started on :8080")
if err := server.ListenAndServe(); err != http.ErrServerClosed {
logger.WithError(err).Fatal("server unable to start")
logrus.WithError(err).Fatal("server unable to start")
}
logger.Info("server exiting")
logrus.Info("server exiting")

<-shutdownComplete

logger.Info("server gracefully shut down")
logrus.Info("server gracefully shut down")
}

0 comments on commit d4dc63b

Please sign in to comment.