Files
pages/internal/server/archive_handler.go
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

122 lines
3.5 KiB
Go

package server
import (
"archive/zip"
"bytes"
"context"
"io"
"mime"
"net/http"
"os/exec"
"path/filepath"
"strings"
"time"
"pageswiki/internal/store"
)
func (s *Server) handleArchiveEntry(w http.ResponseWriter, r *http.Request) {
path := r.URL.Query().Get("path")
entry := r.URL.Query().Get("entry")
metadataOnly := r.URL.Query().Get("metadata") == "1"
if path == "" || (!metadataOnly && entry == "") || len(entry) > 4096 || strings.ContainsAny(entry, "\x00\r\n") {
http.Error(w, "path and entry are required", http.StatusBadRequest)
return
}
if !s.requestCanReadPath(r, path) {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
objectUUID, err := s.store.ResolvePathInDomain(s.domainUUIDForRequest(r), path)
if err != nil {
http.NotFound(w, r)
return
}
var revision *store.Revision
if revisionUUID := r.URL.Query().Get("rev"); revisionUUID != "" {
revision, err = s.store.GetRevisionMetadata(revisionUUID)
} else {
revision, err = s.latestRevisionMetadataForRequest(r, objectUUID)
}
if err != nil || revision.ObjectUUID != objectUUID {
http.NotFound(w, r)
return
}
if metadataOnly {
s.serveMetadataProjection(w, r, revision.DocumentHash, "archive", path, revision.UUID)
return
}
if strings.EqualFold(filepath.Ext(revision.Filename), ".zip") {
hydrated, err := s.store.GetRevision(revision.UUID)
if err != nil {
http.NotFound(w, r)
return
}
s.serveZIPEntry(w, r, hydrated.Body, entry)
return
}
source, err := s.store.DocumentPath(revision.DocumentHash)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
defer cancel()
command := exec.CommandContext(ctx, "nice", "-n", "10",
"7z", "x", "-so", "-y", "-spd", "-mmt1", "-p-", "--", source, entry)
stdout, err := command.StdoutPipe()
if err != nil || command.Start() != nil {
http.Error(w, "cannot extract archive entry", http.StatusUnprocessableEntity)
return
}
w.Header().Set("Content-Type", archiveEntryContentType(entry, nil))
w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": filepath.Base(entry)}))
w.Header().Set("Cache-Control", "private, max-age=60")
w.Header().Set("Vary", "Authorization, Cookie")
w.Header().Set("X-Content-Type-Options", "nosniff")
n, copyErr := io.Copy(w, io.LimitReader(stdout, 128<<20))
if copyErr != nil || n == 128<<20 {
// Never wait for a decoder whose output is no longer being drained.
cancel()
}
waitErr := command.Wait()
if copyErr != nil || waitErr != nil {
return
}
}
func (s *Server) serveZIPEntry(
w http.ResponseWriter,
r *http.Request,
body string,
entry string,
) {
reader, err := zip.NewReader(
bytes.NewReader([]byte(body)),
int64(len(body)),
)
if err != nil {
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}
for _, file := range reader.File {
if file.Name != entry || file.FileInfo().IsDir() {
continue
}
source, err := file.Open()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer source.Close()
w.Header().Set("Content-Type", archiveEntryContentType(file.Name, nil))
w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": filepath.Base(file.Name)}))
w.Header().Set("Cache-Control", "private, max-age=60")
w.Header().Set("Vary", "Authorization, Cookie")
w.Header().Set("X-Content-Type-Options", "nosniff")
_, _ = io.Copy(w, io.LimitReader(source, 128<<20))
return
}
http.NotFound(w, r)
}