file_picker_writable icon indicating copy to clipboard operation
file_picker_writable copied to clipboard

Implement a feature that covers usescases like limiting to "text/plain"

Open bradyt opened this issue 2 years ago • 1 comments

I have the following diffs on a fork:

modified   android/src/main/kotlin/codeux/design/filepicker/file_picker_writable/FilePickerWritableImpl.kt
@@ -53,6 +53,8 @@ class FilePickerWritableImpl(
     val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
       addCategory(Intent.CATEGORY_OPENABLE)
       type = "*/*"
+      val mimetypes = arrayOf<String>("text/plain", "application/octet-stream")
+      putExtra(Intent.EXTRA_MIME_TYPES, mimetypes)
     }
     val activity = requireActivity()
     try {
@@ -79,7 +81,7 @@ class FilePickerWritableImpl(
     val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
       addCategory(Intent.CATEGORY_OPENABLE)
 //      type = "application/x-keepass"
-      type = "*/*"
+      type = "text/plain"
       putExtra(Intent.EXTRA_TITLE, file.name)
     }
     val activity = requireActivity()
modified   ios/Classes/SwiftFilePickerWritablePlugin.swift
@@ -175,7 +175,7 @@ public class SwiftFilePickerWritablePlugin: NSObject, FlutterPlugin {
         }
         _filePickerResult = result
         _filePickerPath = nil
-        let ctrl = UIDocumentPickerViewController(documentTypes: [kUTTypeItem as String], in: UIDocumentPickerMode.open)
+        let ctrl = UIDocumentPickerViewController(documentTypes: [kUTTypePlainText as String], in: UIDocumentPickerMode.open)
         ctrl.delegate = self
         ctrl.modalPresentationStyle = .currentContext
         _viewController.present(ctrl, animated: true, completion: nil)

The Android implementation, for "text/plain", "application/octet-stream" seems to work for users to select files such as ledger.dat, hledger.journal, or ledger.txt. For iOS I think I only got it to work for files like ledger.txt.

My reason for limiting what files can be picked, is especially in apps where user can write to file, I don't want them to inadvertently corrupt a non text file somehow, such as wedding.jpg, or car-deed.pdf.

I am hoping something can be implemented at file_picker_writable, that would cover my usecase, and potentially others'.

I imagine the difficulty is in generalizing this from my usecase above, of something like plain text on iOS and Android, since each platform may cover a notion like MIME types or file extensions with a different approach in the exposed API. And this is just with iOS and Android, I imagine the problem gets more difficult as we discover what the different APIs will be on other platforms. But iOS and Android may be the most difficult cases, since we might imagine desktops expose more aspects of a file in an API, than do mobile OSes.

I completely acknowledge that the author has priorities, that the package is working great, and it is not difficult to maintain my changes on a fork.

bradyt avatar Dec 22 '21 23:12 bradyt