Files
garage/backend/internal/handlers/public.go
T
2026-08-06 18:26:45 +12:00

137 lines
3.9 KiB
Go

// Public, unauthenticated read-only endpoints for a car whose owner has
// turned on sharing (POST /api/cars/{id}/share) — e.g. a QR code stuck in
// the glovebox a mechanic can scan, or a link sent to a buyer. Deliberately
// a separate, narrow surface (not just "the normal endpoints minus auth")
// so there's no risk of it ever exposing more than intended: car identity,
// service history, and its attachments — no reminders, no knowledgebase, no
// owner info.
package handlers
import (
"database/sql"
"errors"
"io"
"net/http"
"vlog/internal/models"
)
func (h *Handlers) loadCarByShareToken(w http.ResponseWriter, r *http.Request) (*models.Car, bool) {
token := r.PathValue("shareToken")
if token == "" {
writeError(w, http.StatusNotFound, "not found")
return nil, false
}
var car models.Car
err := h.dbGet(r.Context(), &car, `SELECT * FROM cars WHERE share_token = ?`, token)
if errors.Is(err, sql.ErrNoRows) {
writeError(w, http.StatusNotFound, "not found")
return nil, false
}
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to load car")
return nil, false
}
return &car, true
}
// PublicCar returns just enough to identify the car — no owner/user info.
func (h *Handlers) PublicCar(w http.ResponseWriter, r *http.Request) {
car, ok := h.loadCarByShareToken(w, r)
if !ok {
return
}
writeJSON(w, http.StatusOK, map[string]interface{}{
"rego": car.Rego,
"make": car.Make,
"model": car.Model,
"year": car.Year,
"color": car.Color,
"hasPhoto": car.PhotoS3Key != nil,
})
}
// PublicCarPhoto streams the car's photo — helps confirm it's the right car.
func (h *Handlers) PublicCarPhoto(w http.ResponseWriter, r *http.Request) {
car, ok := h.loadCarByShareToken(w, r)
if !ok {
return
}
if car.PhotoS3Key == nil {
writeError(w, http.StatusNotFound, "no photo set")
return
}
body, err := h.Storage.Get(r.Context(), *car.PhotoS3Key)
if err != nil {
writeError(w, http.StatusNotFound, "photo not found")
return
}
defer body.Close()
w.Header().Set("Content-Type", contentTypeForExtension(*car.PhotoS3Key))
io.Copy(w, body)
}
// PublicServiceEntries returns one page of service history, including
// attachments — read-only, no edit/delete affordance on this surface.
func (h *Handlers) PublicServiceEntries(w http.ResponseWriter, r *http.Request) {
car, ok := h.loadCarByShareToken(w, r)
if !ok {
return
}
limit, offset := parsePagination(r)
var entries []models.ServiceEntry
err := h.dbSelect(r.Context(), &entries, `
SELECT * FROM service_entries WHERE car_id = ?
ORDER BY entry_date DESC, created_at DESC
LIMIT ? OFFSET ?
`, car.ID, limit+1, offset)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to list service entries")
return
}
entries, hasMore := paginate(limit, entries)
writeJSON(w, http.StatusOK, page[ServiceEntryWithFiles]{
Items: h.attachFiles(r, entries),
HasMore: hasMore,
})
}
// PublicFile streams a service-entry attachment, verified to belong to a
// car reachable via this exact share token (not just "any file the caller
// asks for").
func (h *Handlers) PublicFile(w http.ResponseWriter, r *http.Request) {
car, ok := h.loadCarByShareToken(w, r)
if !ok {
return
}
fileID := r.PathValue("fileId")
var file models.ServiceEntryFile
err := h.dbGet(r.Context(), &file, `
SELECT sef.*
FROM service_entry_files sef
JOIN service_entries se ON se.id = sef.service_entry_id
WHERE sef.id = ? AND se.car_id = ?
`, fileID, car.ID)
if errors.Is(err, sql.ErrNoRows) {
writeError(w, http.StatusNotFound, "file not found")
return
}
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to load file")
return
}
body, err := h.Storage.Get(r.Context(), file.S3Key)
if err != nil {
writeError(w, http.StatusNotFound, "file not found in storage")
return
}
defer body.Close()
w.Header().Set("Content-Type", file.ContentType)
io.Copy(w, body)
}