database, config, home page, start of upload endpoint
This commit is contained in:
+98
@@ -0,0 +1,98 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"mime"
|
||||
"net/http"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type HomePageData struct {
|
||||
Title string
|
||||
Description string
|
||||
}
|
||||
|
||||
func health(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
fmt.Fprintln(w, "OK")
|
||||
}
|
||||
|
||||
type uploadInput struct {
|
||||
Reader io.Reader
|
||||
Filename string
|
||||
MimeType string
|
||||
Type ContentType
|
||||
}
|
||||
|
||||
func upload(db *gorm.DB, cfg Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "invalid method", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// use the largest of the sizes, and then do another size check per type down the line
|
||||
r.Body = http.MaxBytesReader(w, r.Body, max(cfg.MaxFileBytes, cfg.MaxPasteBytes))
|
||||
|
||||
mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if err != nil {
|
||||
// not sure whether this should be a bad request or server error
|
||||
http.Error(w, "failed to find or parse Content-Type", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var input uploadInput
|
||||
switch mediaType {
|
||||
case "multipart/form-data": // file uploads
|
||||
file, header, err := r.FormFile("file")
|
||||
if r.MultipartForm != nil {
|
||||
defer r.MultipartForm.RemoveAll()
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, "invalid file or upload too large", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
if header.Size > cfg.MaxFileBytes {
|
||||
http.Error(w, "invalid file size, file too large", http.StatusRequestEntityTooLarge)
|
||||
return
|
||||
}
|
||||
|
||||
input.MimeType, err = detectMimeType(file)
|
||||
if err != nil {
|
||||
log.Printf("detect file mime type: %v", err)
|
||||
http.Error(w, "failed to detect mime type", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
input.Reader = file
|
||||
input.Filename = header.Filename
|
||||
input.Type = ContentFile
|
||||
case "text/plain", "application/x-www-form-urlencoded": // paste uploads
|
||||
input = uploadInput{
|
||||
Reader: r.Body,
|
||||
Filename: "paste.txt",
|
||||
MimeType: "text/plain; charset=utf-8",
|
||||
Type: ContentPaste,
|
||||
}
|
||||
default:
|
||||
http.Error(w, "unsupported Content-Type", http.StatusUnsupportedMediaType)
|
||||
return
|
||||
}
|
||||
// todo: generate id -> derive storage path -> hash + save to disk -> insert to database
|
||||
}
|
||||
}
|
||||
|
||||
func detectMimeType(file io.ReadSeeker) (string, error) {
|
||||
var buffer [512]byte
|
||||
n, readErr := io.ReadFull(file, buffer[:])
|
||||
if _, err := file.Seek(0, io.SeekStart); err != nil {
|
||||
return "", fmt.Errorf("rewind file: %w", err)
|
||||
}
|
||||
if readErr != nil && readErr != io.EOF && readErr != io.ErrUnexpectedEOF {
|
||||
return "", fmt.Errorf("read file header: %w", readErr)
|
||||
}
|
||||
return http.DetectContentType(buffer[:n]), nil
|
||||
}
|
||||
Reference in New Issue
Block a user