diff --git a/internal/config/config.go b/internal/config/config.go index 8971f98..7a7f45b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -13,6 +13,7 @@ type Config struct { Admin AdminConfig `toml:"admin"` Database DatabaseConfig `toml:"database"` Slugs SlugConfig `toml:"slugs"` + Upload UploadConfig `toml:"upload"` } type ServerConfig struct { @@ -36,6 +37,11 @@ type SlugConfig struct { Length int `toml:"length"` } +type UploadConfig struct { + Dir string `toml:"dir"` + MaxSize int64 `toml:"max_size_mb"` +} + func (c *ServerConfig) ReadTimeoutDuration() time.Duration { d, err := time.ParseDuration(c.ReadTimeout) if err != nil { @@ -70,6 +76,10 @@ func Load(path string) (*Config, error) { Slugs: SlugConfig{ Length: 3, }, + Upload: UploadConfig{ + Dir: "./data/uploads", + MaxSize: 50, + }, } data, err := os.ReadFile(path) diff --git a/internal/server/handlers.go b/internal/server/handlers.go index 5dd4a04..3934986 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -2,12 +2,26 @@ package server import ( "bytes" + "crypto/rand" "crypto/subtle" "database/sql" + "encoding/hex" + "encoding/json" "fmt" "html/template" + "image" + "image/color" + _ "image/gif" + "image/jpeg" + "image/png" + "io" + "math" "net/http" "net/url" + "os" + "path/filepath" + "regexp" + "sort" "strconv" "strings" "time" @@ -36,10 +50,22 @@ type pageData struct { Paste any Pastes any PasteBody template.HTML + Uploads any LoggedIn bool CSRFToken string } +type UploadInfo struct { + Filename string + SizeHuman string + Size int64 + URL string + Width int + Height int + CanResize bool + ModTime time.Time +} + var mdRenderer = goldmark.New( goldmark.WithExtensions(extension.GFM, extension.Table), goldmark.WithRendererOptions(html.WithUnsafe()), @@ -659,6 +685,302 @@ func (s *Server) handleAdminBinDelete(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/admin/bin", http.StatusSeeOther) } +// ---- Upload helpers ---- + +var validUploadRe = regexp.MustCompile(`^[0-9a-f]{32}\.(png|jpg|gif|webp)$`) + +func validUploadFilename(name string) bool { + return validUploadRe.MatchString(name) +} + +func formatBytes(n int64) string { + switch { + case n >= 1024*1024: + return fmt.Sprintf("%.1f MB", float64(n)/(1024*1024)) + case n >= 1024: + return fmt.Sprintf("%.1f KB", float64(n)/1024) + default: + return fmt.Sprintf("%d B", n) + } +} + +func jsonError(w http.ResponseWriter, msg string, code int) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(code) + json.NewEncoder(w).Encode(map[string]string{"error": msg}) +} + +func scaleImage(src image.Image, maxDim int) image.Image { + b := src.Bounds() + w, h := b.Dx(), b.Dy() + if w <= maxDim && h <= maxDim { + return src + } + var newW, newH int + if w >= h { + newW = maxDim + newH = int(math.Round(float64(h) * float64(maxDim) / float64(w))) + } else { + newH = maxDim + newW = int(math.Round(float64(w) * float64(maxDim) / float64(h))) + } + if newW < 1 { + newW = 1 + } + if newH < 1 { + newH = 1 + } + dst := image.NewNRGBA(image.Rect(0, 0, newW, newH)) + scaleX := float64(w) / float64(newW) + scaleY := float64(h) / float64(newH) + for y := 0; y < newH; y++ { + for x := 0; x < newW; x++ { + srcX := int(float64(x)*scaleX) + b.Min.X + srcY := int(float64(y)*scaleY) + b.Min.Y + r, g, bl, a := src.At(srcX, srcY).RGBA() + dst.SetNRGBA(x, y, color.NRGBA{ + R: uint8(r >> 8), + G: uint8(g >> 8), + B: uint8(bl >> 8), + A: uint8(a >> 8), + }) + } + } + return dst +} + +// ---- Upload management handlers ---- + +func (s *Server) handleAdminUploads(w http.ResponseWriter, r *http.Request) { + entries, err := os.ReadDir(s.cfg.Upload.Dir) + if err != nil && !os.IsNotExist(err) { + http.Error(w, "Failed to read uploads", http.StatusInternalServerError) + return + } + + var uploads []UploadInfo + for _, entry := range entries { + if entry.IsDir() { + continue + } + name := entry.Name() + if !validUploadFilename(name) { + continue + } + info, err := entry.Info() + if err != nil { + continue + } + ui := UploadInfo{ + Filename: name, + Size: info.Size(), + SizeHuman: formatBytes(info.Size()), + URL: "/uploads/" + name, + ModTime: info.ModTime(), + } + ext := strings.ToLower(filepath.Ext(name)) + if f, err := os.Open(filepath.Join(s.cfg.Upload.Dir, name)); err == nil { + if cfg, _, err := image.DecodeConfig(f); err == nil { + ui.Width = cfg.Width + ui.Height = cfg.Height + } + f.Close() + } + if ext == ".png" || ext == ".jpg" { + ui.CanResize = true + } + uploads = append(uploads, ui) + } + + sort.Slice(uploads, func(i, j int) bool { + return uploads[i].ModTime.After(uploads[j].ModTime) + }) + + d := s.adminPage(w, r) + d.Uploads = uploads + renderTemplate(w, "admin_uploads.html", d) +} + +func (s *Server) handleAdminUploadDelete(w http.ResponseWriter, r *http.Request) { + filename := r.PathValue("filename") + if !validUploadFilename(filename) { + http.Error(w, "Invalid filename", http.StatusBadRequest) + return + } + os.Remove(filepath.Join(s.cfg.Upload.Dir, filename)) + http.Redirect(w, r, "/admin/uploads", http.StatusSeeOther) +} + +func (s *Server) handleAdminUploadResize(w http.ResponseWriter, r *http.Request) { + filename := r.PathValue("filename") + if !validUploadFilename(filename) { + jsonError(w, "Invalid filename", http.StatusBadRequest) + return + } + ext := strings.ToLower(filepath.Ext(filename)) + if ext != ".png" && ext != ".jpg" { + jsonError(w, "Only PNG and JPEG can be resized", http.StatusBadRequest) + return + } + maxDim, err := strconv.Atoi(r.FormValue("max_dim")) + if err != nil || maxDim < 64 || maxDim > 8192 { + jsonError(w, "max_dim must be between 64 and 8192", http.StatusBadRequest) + return + } + + path := filepath.Join(s.cfg.Upload.Dir, filename) + f, err := os.Open(path) + if err != nil { + jsonError(w, "File not found", http.StatusNotFound) + return + } + src, _, err := image.Decode(f) + f.Close() + if err != nil { + jsonError(w, "Failed to decode image", http.StatusInternalServerError) + return + } + + dst := scaleImage(src, maxDim) + + tmpPath := path + ".tmp" + out, err := os.Create(tmpPath) + if err != nil { + jsonError(w, "Failed to write file", http.StatusInternalServerError) + return + } + var encErr error + if ext == ".jpg" { + encErr = jpeg.Encode(out, dst, &jpeg.Options{Quality: 85}) + } else { + encErr = png.Encode(out, dst) + } + out.Close() + if encErr != nil { + os.Remove(tmpPath) + jsonError(w, "Failed to encode image", http.StatusInternalServerError) + return + } + if err := os.Rename(tmpPath, path); err != nil { + os.Remove(tmpPath) + jsonError(w, "Failed to save resized image", http.StatusInternalServerError) + return + } + + info, _ := os.Stat(path) + var newSize int64 + if info != nil { + newSize = info.Size() + } + b := dst.Bounds() + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]any{ + "size_human": formatBytes(newSize), + "width": b.Dx(), + "height": b.Dy(), + }) +} + +// ---- Image upload handler ---- + +var allowedMIME = map[string]string{ + "image/png": ".png", + "image/jpeg": ".jpg", + "image/gif": ".gif", + "image/webp": ".webp", +} + +func (s *Server) handleUpload(w http.ResponseWriter, r *http.Request) { + maxBytes := s.cfg.Upload.MaxSize * 1024 * 1024 + r.Body = http.MaxBytesReader(w, r.Body, maxBytes) + + // Extend per-request deadlines for large uploads + rc := http.NewResponseController(w) + rc.SetReadDeadline(time.Now().Add(120 * time.Second)) + rc.SetWriteDeadline(time.Now().Add(120 * time.Second)) + + // Verify CSRF manually (must happen after MaxBytesReader is set) + if !verifyCSRF(r) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusForbidden) + json.NewEncoder(w).Encode(map[string]string{"error": "Invalid CSRF token"}) + return + } + + file, _, err := r.FormFile("file") + if err != nil { + w.Header().Set("Content-Type", "application/json") + if err.Error() == "http: request body too large" { + w.WriteHeader(http.StatusRequestEntityTooLarge) + json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("File too large (max %d MB)", s.cfg.Upload.MaxSize)}) + } else { + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode(map[string]string{"error": "No file provided"}) + } + return + } + defer file.Close() + + // Read first 512 bytes to detect MIME type + head := make([]byte, 512) + n, err := file.Read(head) + if err != nil && err != io.EOF { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(map[string]string{"error": "Failed to read file"}) + return + } + head = head[:n] + + mimeType := http.DetectContentType(head) + ext, ok := allowedMIME[mimeType] + if !ok { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusUnsupportedMediaType) + json.NewEncoder(w).Encode(map[string]string{"error": "Only PNG, JPEG, GIF, and WebP images are allowed"}) + return + } + + // Generate random filename + randBytes := make([]byte, 16) + rand.Read(randBytes) + filename := hex.EncodeToString(randBytes) + ext + + // Write file to disk + destPath := filepath.Join(s.cfg.Upload.Dir, filename) + out, err := os.Create(destPath) + if err != nil { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(map[string]string{"error": "Failed to save file"}) + return + } + defer out.Close() + + // Write the head bytes we already read, then copy the rest + if _, err := out.Write(head); err != nil { + os.Remove(destPath) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(map[string]string{"error": "Failed to save file"}) + return + } + if _, err := io.Copy(out, file); err != nil { + os.Remove(destPath) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(map[string]string{"error": "Failed to save file"}) + return + } + + imgURL := "/uploads/" + filename + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]string{ + "url": imgURL, + "markdown": "", + }) +} + var validPasteNameRe = strings.NewReplacer() // placeholder — use inline check below func validPasteName(name string) bool { if len(name) == 0 || len(name) > 64 { diff --git a/internal/server/server.go b/internal/server/server.go index f4d4ff8..b70db8c 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -5,6 +5,7 @@ import ( "io/fs" "log" "net/http" + "os" "strings" "time" @@ -44,7 +45,7 @@ func securityHeaders(next http.Handler) http.Handler { func lowercasePath(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { lower := strings.ToLower(r.URL.Path) - if lower != r.URL.Path && !strings.HasPrefix(r.URL.Path, "/static/") && !strings.HasPrefix(lower, "/admin") { + if lower != r.URL.Path && !strings.HasPrefix(r.URL.Path, "/static/") && !strings.HasPrefix(lower, "/admin") && !strings.HasPrefix(r.URL.Path, "/uploads/") { r.URL.Path = lower http.Redirect(w, r, r.URL.String(), http.StatusMovedPermanently) return @@ -56,12 +57,20 @@ func lowercasePath(next http.Handler) http.Handler { func (s *Server) Start() error { initTemplates() + // Ensure upload directory exists + if err := os.MkdirAll(s.cfg.Upload.Dir, 0755); err != nil { + return fmt.Errorf("creating upload directory: %w", err) + } + mux := http.NewServeMux() // Static files staticSub, _ := fs.Sub(staticFS, "static") mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticSub)))) + // Uploaded images (public — images appear in public pastes) + mux.Handle("GET /uploads/", http.StripPrefix("/uploads/", http.FileServer(http.Dir(s.cfg.Upload.Dir)))) + // Public routes mux.HandleFunc("GET /{$}", s.handleIndex) mux.HandleFunc("POST /shorten", s.handleShorten) @@ -86,6 +95,10 @@ func (s *Server) Start() error { mux.HandleFunc("POST /admin/bin/edit/{id}", s.requireAuth(s.requireCSRF(s.handleAdminBinSave))) mux.HandleFunc("POST /admin/bin/delete/{id}", s.requireAuth(s.requireCSRF(s.handleAdminBinDelete))) mux.HandleFunc("GET /admin/bin/qr/{name}", s.requireAuth(s.handleBinQR)) + mux.HandleFunc("POST /admin/upload", s.requireAuth(s.handleUpload)) + mux.HandleFunc("GET /admin/uploads", s.requireAuth(s.handleAdminUploads)) + mux.HandleFunc("POST /admin/uploads/delete/{filename}", s.requireAuth(s.requireCSRF(s.handleAdminUploadDelete))) + mux.HandleFunc("POST /admin/uploads/resize/{filename}", s.requireAuth(s.handleAdminUploadResize)) // Pastebin public routes (before catch-all) mux.HandleFunc("GET /bin/{name}", s.handleBinView) diff --git a/internal/server/static/style.css b/internal/server/static/style.css index 023fb5b..f4ded16 100644 --- a/internal/server/static/style.css +++ b/internal/server/static/style.css @@ -382,3 +382,23 @@ mark.badge-hidden { padding: 0.25rem 0.75rem; font-size: 0.85rem; } + +/* Image upload controls */ +.upload-controls { + display: flex; + align-items: center; + gap: 0.5rem; + margin-bottom: 0.25rem; +} + +.upload-controls button { + width: auto; + margin: 0; + padding: 0.3rem 0.75rem; + font-size: 0.85rem; +} + +#upload-status { + color: var(--pico-muted-color); + font-size: 0.8rem; +} diff --git a/internal/server/templates/admin.html b/internal/server/templates/admin.html index f30fde4..047b7a9 100644 --- a/internal/server/templates/admin.html +++ b/internal/server/templates/admin.html @@ -23,6 +23,7 @@