Skip to content

Commit

Permalink
Merge pull request #2420 from acterglobal/ben-keep-file-extension-for…
Browse files Browse the repository at this point in the history
…-download-file

Keep the extension of the original file when none was given
  • Loading branch information
kumarpalsinh25 authored Dec 10, 2024
2 parents 50bd8fe + d8cb4f6 commit 8152995
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions .changes/2420-keep-file-extension.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix: when downloading as file, keep the extension even if the user removed it to ensure they can open the resulting file.
10 changes: 8 additions & 2 deletions app/lib/features/files/actions/download_file.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:io';
import 'package:path/path.dart';
import 'package:path/path.dart' as p;

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
Expand All @@ -8,7 +8,8 @@ import 'package:flutter_easyloading/flutter_easyloading.dart';

Future<bool> downloadFile(BuildContext context, File file) async {
final lang = L10n.of(context);
final filename = basename(file.path);
final filename = p.basename(file.path);
final extension = p.extension(filename);
String? outputFile = await FilePicker.platform.saveFile(
dialogTitle: lang.downloadFileDialogTitle,
fileName: filename,
Expand All @@ -18,6 +19,11 @@ Future<bool> downloadFile(BuildContext context, File file) async {
return false;
}

if (p.extension(outputFile).isEmpty) {
// the new file doesn't have an extension, we add the previous one again
outputFile = '$outputFile$extension';
}

await file.copy(outputFile);
EasyLoading.showToast(lang.downloadFileSuccess(outputFile));
return true;
Expand Down

0 comments on commit 8152995

Please sign in to comment.