diff --git a/b0esche_cloud/lib/pages/public_file_viewer.dart b/b0esche_cloud/lib/pages/public_file_viewer.dart index 1db6078..ff06df1 100644 --- a/b0esche_cloud/lib/pages/public_file_viewer.dart +++ b/b0esche_cloud/lib/pages/public_file_viewer.dart @@ -1,9 +1,6 @@ import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; -import 'package:http/http.dart' as http; -import 'dart:js_interop'; import 'package:web/web.dart' as web; -import 'dart:typed_data'; import '../theme/app_theme.dart'; import '../services/api_client.dart'; import '../injection.dart'; @@ -47,27 +44,11 @@ class _PublicFileViewerState extends State { void _downloadFile() { if (_fileData != null && _fileData!['downloadUrl'] != null) { - // Use http package to download - http.get(Uri.parse(_fileData!['downloadUrl'])).then((response) { - if (response.statusCode == 200) { - // Trigger download in web - final uint8List = Uint8List.fromList(response.bodyBytes); - final jsUint8Array = JSUint8Array( - uint8List.buffer.toJS, - uint8List.offsetInBytes, - uint8List.length, - ); - final jsArray = JSArray.withLength(1); - jsArray[0] = jsUint8Array; - final blob = web.Blob(jsArray); - final url = web.URL.createObjectURL(blob); - final anchor = web.HTMLAnchorElement() - ..href = url - ..download = _fileData!['fileName'] ?? 'download'; - anchor.click(); - web.URL.revokeObjectURL(url); - } - }); + // Trigger download directly in browser + final anchor = web.HTMLAnchorElement() + ..href = _fileData!['downloadUrl'] + ..download = _fileData!['fileName'] ?? 'download'; + anchor.click(); } } diff --git a/go_cloud/api b/go_cloud/api index 4ae50de..a9015df 100755 Binary files a/go_cloud/api and b/go_cloud/api differ diff --git a/go_cloud/internal/http/routes.go b/go_cloud/internal/http/routes.go index cdb97ee..3e4a8df 100644 --- a/go_cloud/internal/http/routes.go +++ b/go_cloud/internal/http/routes.go @@ -2979,8 +2979,13 @@ func publicFileDownloadHandler(w http.ResponseWriter, r *http.Request, db *datab return } - // Get WebDAV client for org - client, err := getUserWebDAVClient(r.Context(), db, uuid.Nil, "https://of.b0esche.cloud", cfg.NextcloudUser, cfg.NextcloudPass) + if file.UserID == nil { + errors.WriteError(w, errors.CodeNotFound, "File not accessible", http.StatusNotFound) + return + } + + // Get WebDAV client for the file's owner + client, err := getUserWebDAVClient(r.Context(), db, *file.UserID, cfg.NextcloudURL, cfg.NextcloudUser, cfg.NextcloudPass) if err != nil { errors.LogError(r, err, "Failed to get WebDAV client") errors.WriteError(w, errors.CodeInternal, "Server error", http.StatusInternalServerError) @@ -3001,6 +3006,9 @@ func publicFileDownloadHandler(w http.ResponseWriter, r *http.Request, db *datab w.Header()[k] = v } + // Ensure download behavior + w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", file.Name)) + // Copy body io.Copy(w, resp.Body) }