Refactor file download handling to simplify browser download process and ensure proper content disposition for file downloads

This commit is contained in:
Leon Bösche
2026-01-24 21:11:25 +01:00
parent 6bbdc157cb
commit 828b63c8c8
3 changed files with 15 additions and 26 deletions

View File

@@ -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<PublicFileViewer> {
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<JSAny>.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();
}
}

Binary file not shown.

View File

@@ -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)
}