46 lines
991 B
Go
46 lines
991 B
Go
package handler
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func (h *Handler) Info(w http.ResponseWriter, r *http.Request) {
|
|
id := r.PathValue("id")
|
|
if id == "" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
rec, err := h.store.Get(id)
|
|
if err == sql.ErrNoRows {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if err != nil {
|
|
log.Printf("db get error: %v", err)
|
|
http.Error(w, "internal error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
resp := map[string]any{
|
|
"id": rec.ID,
|
|
"filename": rec.Filename,
|
|
"size": rec.SizeBytes,
|
|
"content_type": rec.ContentType,
|
|
"uploaded_at": rec.UploadedAt.Format(time.RFC3339),
|
|
"download_count": rec.DownloadCount,
|
|
"password": rec.PasswordHash != nil,
|
|
}
|
|
if rec.ExpiresAt != nil {
|
|
resp["expires_at"] = rec.ExpiresAt.Format(time.RFC3339)
|
|
resp["expired"] = rec.ExpiresAt.Before(time.Now().UTC())
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(resp)
|
|
}
|