Fix PDF loading by preserving Nextcloud headers

- Revert to synchronous download but keep 5-minute timeout
- Copy Content-Length and other headers from Nextcloud response
- Ensures PDF viewer gets proper content metadata
- Maintains streaming for audio/video with headers
This commit is contained in:
Leon Bösche
2026-01-25 15:42:10 +01:00
parent 387f39cbcc
commit 532848ebdf

View File

@@ -3178,37 +3178,39 @@ func publicFileViewHandler(w http.ResponseWriter, r *http.Request, db *database.
downloadCtx, cancel := context.WithTimeout(r.Context(), 5*time.Minute)
defer cancel()
// Create a pipe for streaming
pipeReader, pipeWriter := io.Pipe()
// Start download in goroutine
go func() {
defer pipeWriter.Close()
resp, err := client.Download(downloadCtx, file.Path, r.Header.Get("Range"))
if err != nil {
pipeWriter.CloseWithError(err)
return
}
defer resp.Body.Close()
io.Copy(pipeWriter, resp.Body)
}()
// Stream file
resp, err := client.Download(downloadCtx, file.Path, r.Header.Get("Range"))
if err != nil {
errors.LogError(r, err, "Failed to download file")
errors.WriteError(w, errors.CodeInternal, "Server error", http.StatusInternalServerError)
return
}
defer resp.Body.Close()
// Add CORS headers for public access
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Range")
// Set status code (200 or 206 for partial)
w.WriteHeader(200) // Assume 200 for now, or check if Range was requested
// Copy headers from Nextcloud response, but skip Content-Type to ensure correct MIME type
for k, v := range resp.Header {
if k != "Content-Type" {
w.Header()[k] = v
}
}
// Set correct Content-Type based on file extension
w.Header().Set("Content-Type", mimeType)
// Ensure inline viewing behavior (no Content-Disposition attachment)
w.Header().Del("Content-Disposition")
w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", file.Name))
// Copy body from pipe
io.Copy(w, pipeReader)
// Set status code (200 or 206 for partial)
w.WriteHeader(resp.StatusCode)
// Copy body
io.Copy(w, resp.Body)
}
func getUserFileShareLinkHandler(w http.ResponseWriter, r *http.Request, db *database.DB) {