From a6588717db5075e0398b27601eb1e00dbdac621d Mon Sep 17 00:00:00 2001 From: Shlomo <78599753+ShlomoCode@users.noreply.github.com> Date: Fri, 17 Oct 2025 15:38:55 +0300 Subject: [PATCH] fix(android): handle content URIs in url sharing (#2756) --- .../server/controller/send_controller.dart | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/app/lib/provider/network/server/controller/send_controller.dart b/app/lib/provider/network/server/controller/send_controller.dart index 9350a11a..668ff75a 100644 --- a/app/lib/provider/network/server/controller/send_controller.dart +++ b/app/lib/provider/network/server/controller/send_controller.dart @@ -207,10 +207,12 @@ class SendController { request.response ..statusCode = 200 ..headers.set('content-type', 'application/octet-stream') - ..headers.set('content-disposition', 'attachment; filename="${Uri.encodeComponent(fileName)}"') - ..headers.set('content-length', '${file.file.size}'); + ..headers.set('content-disposition', 'attachment; filename="${Uri.encodeComponent(fileName)}"'); + + final isInlineContent = file.bytes != null; // text message, clipboard content + if (isInlineContent) { + request.response.headers.set('content-length', '${file.bytes!.length}'); - if (file.bytes != null) { final byteStream = Stream.fromIterable([file.bytes!]); final (streamController, subscription) = byteStream.digested(); @@ -222,10 +224,13 @@ class SendController { }); } else { final path = file.path!; - final tmpfile = File(file.path!); - request.response.headers.set('content-length', '${tmpfile.lengthSync()}'); + final isContentUri = path.startsWith('content://'); - final fileStream = path.startsWith('content://') ? UriContent().getContentStream(Uri.parse(file.path!)) : tmpfile.openRead(); + // Read file size at download time, since the file could have changed since it was selected (#2359, #2043) + final fileSize = isContentUri ? await UriContent().getContentLength(Uri.parse(path)) : File(path).lengthSync(); + request.response.headers.set('content-length', '$fileSize'); + + final fileStream = isContentUri ? UriContent().getContentStream(Uri.parse(path)) : File(path).openRead(); final (streamController, subscription) = fileStream.digested(); await request.response.addStream(streamController.stream).then((_) {