Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(teamhub): send stored files as they are under response compression

muterC326c4839a830
diff --git a/apps/teamhub/internal/app/compression.go b/apps/teamhub/internal/app/compression.go
index 92686f89..dc2d634a 100644
--- a/apps/teamhub/internal/app/compression.go
+++ b/apps/teamhub/internal/app/compression.go
@@ -3,14 +3,23 @@ package app
import (
"net/http"
+ "github.com/gin-gonic/gin"
"github.com/klauspost/compress/gzhttp"
)
// compressedContentTypes are the API bodies worth compressing: JSON and the
// Wiki graph's HTML shrink ten times or more, which matters on slow links.
-// Event streams, files and ranged reads go out as they are.
+// Event streams, ranged reads and stored files (sendAsStored) go out as they
+// are.
var compressedContentTypes = []string{"application/json", "application/problem+json", "text/html"}
+// sendAsStored keeps a stored file out of compression whatever its media
+// type, such as an uploaded JSON or HTML file: it goes out with its own length
+// and ETag, so downloads show progress and validators match its bytes.
+func sendAsStored(c *gin.Context) {
+ c.Header(gzhttp.HeaderNoCompression, "1")
+}
+
// compressResponses compresses JSON and HTML responses, with zstd or gzip as
// the client accepts.
func compressResponses(handler http.Handler) http.Handler {
diff --git a/apps/teamhub/internal/app/compression_test.go b/apps/teamhub/internal/app/compression_test.go
index d8adc3a2..c7302902 100644
--- a/apps/teamhub/internal/app/compression_test.go
+++ b/apps/teamhub/internal/app/compression_test.go
@@ -6,11 +6,13 @@ import (
"io"
"net/http"
"net/http/httptest"
+ "strconv"
"strings"
"testing"
"time"
"github.com/gin-gonic/gin"
+ "github.com/klauspost/compress/gzhttp"
)
func TestCompressResponsesCompressesLargeJSONOnly(t *testing.T) {
@@ -24,6 +26,13 @@ func TestCompressResponsesCompressesLargeJSONOnly(t *testing.T) {
c.Data(http.StatusPartialContent, "application/json", []byte(large))
})
engine.GET("/pdf", func(c *gin.Context) { c.Data(http.StatusOK, "application/pdf", []byte(large)) })
+ // An uploaded JSON file is a stored file, not an API body.
+ engine.GET("/stored", func(c *gin.Context) {
+ sendAsStored(c)
+ c.Header("ETag", `"sha"`)
+ c.Header("Content-Length", strconv.Itoa(len(large)))
+ c.Data(http.StatusOK, "application/json", []byte(large))
+ })
handler := compressResponses(engine)
get := func(path string) *httptest.ResponseRecorder {
request := httptest.NewRequest(http.MethodGet, path, nil)
@@ -52,12 +61,17 @@ func TestCompressResponsesCompressesLargeJSONOnly(t *testing.T) {
if recorder.Header().Get("Content-Encoding") != "zstd" {
t.Fatalf("zstd offered: encoding %q", recorder.Header().Get("Content-Encoding"))
}
- // A short body, a ranged read and a file keep their bytes as they are.
- for _, path := range []string{"/small", "/file", "/pdf"} {
+ // A short body, a ranged read and files keep their bytes as they are.
+ for _, path := range []string{"/small", "/file", "/pdf", "/stored"} {
if encoding := get(path).Header().Get("Content-Encoding"); encoding != "" {
t.Fatalf("%s is encoded %q", path, encoding)
}
}
+ stored := get("/stored")
+ if stored.Header().Get("Content-Length") != strconv.Itoa(len(large)) || stored.Header().Get("ETag") != `"sha"` ||
+ stored.Header().Get(gzhttp.HeaderNoCompression) != "" || stored.Body.String() != large {
+ t.Fatalf("stored file headers=%v", stored.Header())
+ }
// A client that does not ask for it gets the plain body.
request = httptest.NewRequest(http.MethodGet, "/large", nil)
recorder = httptest.NewRecorder()
diff --git a/apps/teamhub/internal/app/contract_server_personal_wiki_worker.go b/apps/teamhub/internal/app/contract_server_personal_wiki_worker.go
index ac4a5bd8..453017b1 100644
--- a/apps/teamhub/internal/app/contract_server_personal_wiki_worker.go
+++ b/apps/teamhub/internal/app/contract_server_personal_wiki_worker.go
@@ -80,6 +80,7 @@ func (a *App) downloadPersonalWikiDocumentVersion(c *gin.Context) {
return
}
defer content.Close()
+ sendAsStored(c)
c.Header("ETag", `"`+selected.SHA256+`"`)
c.Header("Content-Type", selected.MediaType)
c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename=%q`,
@@ -106,6 +107,7 @@ func (a *App) fetchSession(c *gin.Context) {
return
}
defer object.Close()
+ sendAsStored(c)
c.Header("ETag", `"`+content.Version.SHA256+`"`)
c.Header("X-Content-SHA256", content.Version.SHA256)
c.Header("Content-Type", content.Version.MediaType)
diff --git a/apps/teamhub/internal/app/handlers_document_preview.go b/apps/teamhub/internal/app/handlers_document_preview.go
index 60ad47f1..08791a80 100644
--- a/apps/teamhub/internal/app/handlers_document_preview.go
+++ b/apps/teamhub/internal/app/handlers_document_preview.go
@@ -65,6 +65,7 @@ func writeDocumentPreviewHeaders(c *gin.Context, content immutableObjectContent)
if disposition == "" {
disposition = "inline"
}
+ sendAsStored(c)
c.Header("Accept-Ranges", "bytes")
c.Header("ETag", `"`+content.sha256+`"`)
previewMediaType := content.mediaType
diff --git a/apps/teamhub/internal/app/handlers_immutable_object_content.go b/apps/teamhub/internal/app/handlers_immutable_object_content.go
index 7823a475..a520e835 100644
--- a/apps/teamhub/internal/app/handlers_immutable_object_content.go
+++ b/apps/teamhub/internal/app/handlers_immutable_object_content.go
@@ -126,6 +126,7 @@ func (a *App) writeArtifactStorageError(c *gin.Context, err error) {
}
func (a *App) writeImmutableObjectContentHeaders(c *gin.Context, content immutableObjectContent) {
+ sendAsStored(c)
c.Header("Accept-Ranges", "bytes")
// Revalidate every use so logout or revoked access cannot be served from a shared browser cache.
c.Header("Cache-Control", "private, no-cache")
diff --git a/apps/teamhub/internal/app/handlers_room_document_assets.go b/apps/teamhub/internal/app/handlers_room_document_assets.go
index 073beb0e..980cfe3d 100644
--- a/apps/teamhub/internal/app/handlers_room_document_assets.go
+++ b/apps/teamhub/internal/app/handlers_room_document_assets.go
@@ -70,6 +70,7 @@ func (a *App) downloadRoomDocumentAssetContent(c *gin.Context) {
return
}
defer content.Reader.Close()
+ sendAsStored(c)
c.Header("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": content.Asset.Filename}))
c.Header("X-Content-Type-Options", "nosniff")
c.DataFromReader(http.StatusOK, content.Asset.SizeBytes, content.Asset.MediaType, content.Reader, map[string]string{