Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto naming incrémentation when duplicate filename after extension #1669

Open
baptistesx opened this issue Jan 3, 2025 · 2 comments
Open
Labels
new issue An issue that hasn't yet been seen from the maintainer

Comments

@baptistesx
Copy link

baptistesx commented Jan 3, 2025

Hi,

i am using the last version of the package 8.1.7.

When I use saveFile() and a file with the same filename already exists, it increments but after the actual filename (containing its extension).

Example: "filename.pdf" already exists

The new file will be saved under "filename.pdf (1)". So it breaks the extension and the filesystem will not recognize the file as a pdf.

The solution would be to put the increment before the extension.

Temporary solution: add a uuid between the name and the extension (I used a 5 chars length uuid, it should be fine)

 Future<void> _saveFileToDownloads() async {
    if (localPath == null) return;

    try {
      // Generate a 5-character UUID
      final String uuid = DateTime.now().microsecondsSinceEpoch.toString();
      final String shortUuid = uuid.substring(uuid.length - 5);

      // Separate the file name and the extension
      final int lastDot = widget.filename.lastIndexOf('.');
      final String nameWithoutExt = widget.filename.substring(0, lastDot);
      final String extension = widget.filename.substring(lastDot);

      // Create the new file name with the UUID
      final String newFilename = '${nameWithoutExt}_$uuid$extension';

      final HearingAttachmentType fileType =
          HearingAttachment.getTypeFromFilename(widget.filename);
      FileType pickerFileType;
      List<String>? allowedExtensions;

      // Configure the type and extensions based on the file type
      switch (fileType) {
        case HearingAttachmentType.image:
          pickerFileType = FileType.image;
          allowedExtensions = <String>['jpg', 'jpeg', 'png'];
        case HearingAttachmentType.pdf:
          pickerFileType = FileType.custom;
          allowedExtensions = <String>['pdf'];
        case HearingAttachmentType.document:
        case HearingAttachmentType.other:
          pickerFileType = FileType.any;
          allowedExtensions = null;
      }

      final String? outputFile = await FilePicker.platform.saveFile(
        dialogTitle: 'Save File',
        fileName: newFilename,
        allowedExtensions: allowedExtensions,
        type: pickerFileType,
        bytes: await File(localPath!).readAsBytes(),
        lockParentWindow: true,
      );

      if (outputFile != null) {
        setState(() {
          fileSaved = true;
        });

        if (context.mounted) {
          showSuccessToast(
            text: 'File saved successfully',
            title: 'File Saved',
          );
        }
      }
    } catch (e) {
      print('Save error: $e');
      if (context.mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(
            content: Text('Error while downloading the file'),
          ),
        );
      }
    }
  }
flutter doctor -v
[✓] Flutter (Channel stable, 3.27.1, on macOS 14.5 23F79 darwin-arm64,
    locale fr-ES)
    • Flutter version 3.27.1 on channel stable at
      /Users/baptisteseux/.asdf/installs/flutter/3.27.1-stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 17025dd882 (3 weeks ago), 2024-12-17 03:23:09
      +0900
    • Engine revision cb4b5fff73
    • Dart version 3.6.0
    • DevTools version 2.40.2

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/baptisteseux/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0
    • ANDROID_HOME = /Users/baptisteseux/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] VS Code (version 1.93.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.96.0

[✓] Connected device (5 available)
    • sdk gphone64 arm64 (mobile)     • emulator-5554             • android-arm64  • Android 14 (API 34) (emulator)
    • iPhone de Baptiste (mobile)     • 00008130-001074640AC2001C • ios            • iOS 18.2 22C5142a
    • macOS (desktop)                 • macos                     • darwin-arm64   • macOS 14.5 23F79 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad     • darwin         • macOS 14.5 23F79 darwin-arm64
    • Chrome (web)                    • chrome                    • web-javascript • Google Chrome 131.0.6778.205

[✓] Network resources
    • All expected network resources are available.

• No issues found!
@baptistesx baptistesx added the new issue An issue that hasn't yet been seen from the maintainer label Jan 3, 2025
@EArminjon
Copy link

EArminjon commented Jan 14, 2025

Example :

File got broken if already exist, major issue :

image

@EArminjon
Copy link

EArminjon commented Jan 14, 2025

This issue is for Android, it works fine on iOS.

I don't understand why saveFile need to define a FileType type, so I can't create a PR.
SaveFile should take a String mimeType and pass this mimeType to native API (intent.setType(mimeType). By doing that, issue is fixed.

@miguelpruivo can you please take a look ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new issue An issue that hasn't yet been seen from the maintainer
Projects
None yet
Development

No branches or pull requests

2 participants