diff --git a/go_cloud/internal/http/routes.go b/go_cloud/internal/http/routes.go index 0c22456..63fc004 100644 --- a/go_cloud/internal/http/routes.go +++ b/go_cloud/internal/http/routes.go @@ -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) {