-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from TheWisePigeon/8-handle-file-uploads
Handle file uploads
- Loading branch information
Showing
4 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package handlers | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"visio/internal/types" | ||
"visio/pkg" | ||
) | ||
|
||
type FaceHandler struct { | ||
logger *slog.Logger | ||
} | ||
|
||
func NewFaceHandler(logger *slog.Logger) *FaceHandler { | ||
return &FaceHandler{ | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (h *FaceHandler) SaveFace(w http.ResponseWriter, r *http.Request) { | ||
filePath, err := pkg.HandleFileUpload(w, r) | ||
if err != nil { | ||
if errors.Is(err, types.ErrFileNotFound) { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(types.ErrFileNotFoundMessage)) | ||
return | ||
} | ||
if errors.Is(err, types.ErrUnsupportedFormat) { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(types.ErrUnsupportedFormatMessage)) | ||
return | ||
} | ||
if errors.Is(err, types.ErrBodyTooLarge) { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(types.ErrBodyTooLargeMessage)) | ||
return | ||
} | ||
h.logger.Error(err.Error()) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
fmt.Println(filePath) | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte("Ok")) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package pkg | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"visio/internal/types" | ||
) | ||
|
||
var imageMimeTypes = []string{ | ||
"image/jpeg", | ||
"image/png", | ||
} | ||
|
||
const MaxFileSize = 5 | ||
|
||
func HandleFileUpload(w http.ResponseWriter, r *http.Request) (string, error) { | ||
r.Body = http.MaxBytesReader(w, r.Body, MaxFileSize<<20) | ||
reader, err := r.MultipartReader() | ||
if err != nil { | ||
return "", fmt.Errorf("Error while reading multipart request body: %w", err) | ||
} | ||
p, err := reader.NextPart() | ||
if err != nil && err != io.EOF { | ||
return "", fmt.Errorf("Error while reading multipart body part: %w", err) | ||
} | ||
if p.FormName() != "face" { | ||
return "", types.ErrFileNotFound | ||
} | ||
buffer := bufio.NewReader(p) | ||
sniffedBytes, err := buffer.Peek(512) | ||
if err != nil { | ||
return "", fmt.Errorf("Error while peeking through bytes: %w", err) | ||
} | ||
contentType := http.DetectContentType(sniffedBytes) | ||
fileIsNotImage := true | ||
for _, mimeType := range imageMimeTypes { | ||
if contentType == mimeType { | ||
fileIsNotImage = false | ||
break | ||
} | ||
} | ||
if fileIsNotImage { | ||
return "", types.ErrUnsupportedFormat | ||
} | ||
f, err := os.CreateTemp("", "") | ||
if err != nil { | ||
return "", fmt.Errorf("Error while creating file: %w", err) | ||
} | ||
defer f.Close() | ||
var maxSize int64 = MaxFileSize << 20 | ||
lmt := io.MultiReader(buffer, io.LimitReader(p, maxSize-511)) | ||
written, err := io.Copy(f, lmt) | ||
if err != nil && err != io.EOF { | ||
return "", types.ErrBodyTooLarge | ||
} | ||
if written > maxSize { | ||
os.Remove(f.Name()) | ||
return "", fmt.Errorf("Error while deleting file: %w", err) | ||
} | ||
return f.Name(), nil | ||
} |