package server import ( "encoding/base64" "encoding/hex" "mime" "net/http" "strings" "time" "pageswiki/internal/store" ) func (s *Server) handleImmutableObject(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet && r.Method != http.MethodHead { w.Header().Set("Allow", "GET, HEAD") http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } address := strings.TrimPrefix(r.URL.Path, "/_/obj/") if !strings.HasPrefix(address, "sha256:") { http.NotFound(w, r) return } hash := strings.ToLower(strings.TrimSpace(strings.TrimPrefix(address, "sha256:"))) digestBytes, err := hex.DecodeString(hash) if err != nil || len(digestBytes) != 32 { http.NotFound(w, r) return } references, err := s.store.DocumentReferencesInDomain(s.domainUUIDForRequest(r), hash) if err != nil { references = nil } var metadata *store.Revision authorized := false for _, reference := range references { if s.authorizePathRequest(r, "read", reference.Path) == nil { metadata = reference.Revision authorized = true break } } if !authorized && s.canManageAssets(r) { asset, assetErr := s.store.GetAssetByHeadHash(s.domainUUIDForRequest(r), hash) if assetErr == nil { metadata = &store.Revision{ObjectUUID: asset.ObjectUUID, DocumentHash: hash, ContentType: asset.ContentType, Filename: asset.Filename, CreatedAtMS: asset.UpdatedAtMS} authorized = true } } if !authorized { http.NotFound(w, r) return } reader, err := s.store.OpenDocument(hash) if err != nil { http.NotFound(w, r) return } defer reader.Close() contentType := metadata.ContentType if contentType == "application/octet-stream" { probe := make([]byte, 512) count, _ := reader.Read(probe) _, _ = reader.Seek(0, 0) contentType = http.DetectContentType(probe[:count]) } mediaType, _, err := mime.ParseMediaType(contentType) if err != nil { contentType = "application/octet-stream" mediaType = contentType } filename := strings.TrimSpace(metadata.Filename) if filename == "" { filename = hash } disposition := "inline" if mediaType == "text/html" || mediaType == "image/svg+xml" || (!strings.HasPrefix(mediaType, "image/") && !strings.HasPrefix(mediaType, "audio/") && !strings.HasPrefix(mediaType, "video/") && mediaType != "application/pdf" && !strings.HasPrefix(mediaType, "text/")) { disposition = "attachment" } w.Header().Set("Content-Type", contentType) w.Header().Set("Content-Disposition", mime.FormatMediaType(disposition, map[string]string{"filename": filename})) w.Header().Set("Cache-Control", "private, max-age=31536000, immutable") w.Header().Set("Vary", "Authorization, Cookie") w.Header().Set("ETag", `"sha256:`+hash+`"`) w.Header().Set("Digest", "sha-256="+base64.StdEncoding.EncodeToString(digestBytes)) w.Header().Set("X-Content-Type-Options", "nosniff") setDocumentIdentityHeaders(w.Header(), metadata.ObjectUUID, metadata.UUID, hash) if mediaType == "image/svg+xml" { w.Header().Set("Content-Security-Policy", "sandbox") } if r.Header.Get("If-None-Match") == `"sha256:`+hash+`"` { w.WriteHeader(http.StatusNotModified) return } http.ServeContent(w, r, filename, time.UnixMilli(metadata.CreatedAtMS), reader) }