@zenkit-dev/cordova-plugin-simple-chooser 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +13 -0
- package/README.md +50 -0
- package/package.json +31 -0
- package/plugin.xml +35 -0
- package/src/android/Chooser.java +131 -0
- package/src/ios/Chooser.swift +160 -0
- package/www/chooser.js +15 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright (c) 2019, Zenkit
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Simple Chooser
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
A simple file chooser plugin for Cordova.
|
|
6
|
+
|
|
7
|
+
Based on [`cordova-plugin-simple-chooser`](https://github.com/cyph/cordova-plugin-chooser),
|
|
8
|
+
but without using `Promise` and without loading the file `data` (can be done with `cordova-plugin-file`).
|
|
9
|
+
|
|
10
|
+
Install with Cordova CLI:
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
cordova plugin add https://github.com/zenkit/cordova-plugin-simple-chooser#v2.0.1
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Supported Platforms:
|
|
17
|
+
|
|
18
|
+
- Android
|
|
19
|
+
|
|
20
|
+
- iOS
|
|
21
|
+
|
|
22
|
+
## API
|
|
23
|
+
|
|
24
|
+
### chooser.chooseFile(successCallback, errorCallback, [accept])
|
|
25
|
+
|
|
26
|
+
#### successCallback
|
|
27
|
+
|
|
28
|
+
Type: `function`
|
|
29
|
+
|
|
30
|
+
The success callback will be called with the information of the picked file
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
{
|
|
34
|
+
uri: 'string',
|
|
35
|
+
name: 'string',
|
|
36
|
+
mediaType: 'string',
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
#### errorCallback
|
|
41
|
+
|
|
42
|
+
Type: `function`
|
|
43
|
+
|
|
44
|
+
Will be called if an error occurs or the user cancels the picker
|
|
45
|
+
|
|
46
|
+
#### accept
|
|
47
|
+
|
|
48
|
+
Type: `string`
|
|
49
|
+
|
|
50
|
+
Optional MIME type filter (e.g. 'image/gif,video/\*').
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zenkit-dev/cordova-plugin-simple-chooser",
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"description": "A simple file chooser plugin for Cordova.",
|
|
5
|
+
"repository": "https://github.com/zenkit/cordova-plugin-simple-chooser.git",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"cordova",
|
|
8
|
+
"plugin",
|
|
9
|
+
"file",
|
|
10
|
+
"chooser",
|
|
11
|
+
"picker",
|
|
12
|
+
"file-chooser",
|
|
13
|
+
"android",
|
|
14
|
+
"ios"
|
|
15
|
+
],
|
|
16
|
+
"author": "Zenkit <info@zenkit.com>",
|
|
17
|
+
"license": "Apache-2.0",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/zenkit/cordova-plugin-simple-chooser/issues"
|
|
20
|
+
},
|
|
21
|
+
"cordova": {
|
|
22
|
+
"id": "cordova-plugin-simple-chooser",
|
|
23
|
+
"platforms": [
|
|
24
|
+
"android",
|
|
25
|
+
"ios"
|
|
26
|
+
]
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/plugin.xml
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
+
<plugin
|
|
3
|
+
xmlns="http://www.phonegap.com/ns/plugins/1.0"
|
|
4
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
5
|
+
id="cordova-plugin-simple-chooser"
|
|
6
|
+
version="2.0.1"
|
|
7
|
+
>
|
|
8
|
+
<name>Chooser</name>
|
|
9
|
+
<author>Zenkit</author>
|
|
10
|
+
|
|
11
|
+
<js-module src="www/chooser.js" name="Chooser">
|
|
12
|
+
<clobbers target="chooser" />
|
|
13
|
+
</js-module>
|
|
14
|
+
|
|
15
|
+
<platform name="android">
|
|
16
|
+
<source-file src="src/android/Chooser.java" target-dir="src/com/zenkit/cordova" />
|
|
17
|
+
|
|
18
|
+
<config-file target="config.xml" parent="/*">
|
|
19
|
+
<feature name="Chooser">
|
|
20
|
+
<param name="android-package" value="com.zenkit.cordova.Chooser" />
|
|
21
|
+
</feature>
|
|
22
|
+
</config-file>
|
|
23
|
+
</platform>
|
|
24
|
+
|
|
25
|
+
<platform name="ios">
|
|
26
|
+
<source-file src="src/ios/Chooser.swift" />
|
|
27
|
+
<dependency id="cordova-plugin-add-swift-support" version="*" />
|
|
28
|
+
|
|
29
|
+
<config-file target="config.xml" parent="/*">
|
|
30
|
+
<feature name="Chooser">
|
|
31
|
+
<param name="ios-package" value="Chooser" />
|
|
32
|
+
</feature>
|
|
33
|
+
</config-file>
|
|
34
|
+
</platform>
|
|
35
|
+
</plugin>
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
package com.zenkit.cordova;
|
|
2
|
+
|
|
3
|
+
import android.app.Activity;
|
|
4
|
+
import android.content.ContentResolver;
|
|
5
|
+
import android.content.Intent;
|
|
6
|
+
import android.database.Cursor;
|
|
7
|
+
import android.net.Uri;
|
|
8
|
+
import android.provider.MediaStore;
|
|
9
|
+
import android.util.Base64;
|
|
10
|
+
|
|
11
|
+
import java.io.ByteArrayOutputStream;
|
|
12
|
+
import java.io.InputStream;
|
|
13
|
+
import java.io.IOException;
|
|
14
|
+
import java.lang.Exception;
|
|
15
|
+
|
|
16
|
+
import org.apache.cordova.CallbackContext;
|
|
17
|
+
import org.apache.cordova.CordovaPlugin;
|
|
18
|
+
import org.apache.cordova.PluginResult;
|
|
19
|
+
import org.json.JSONArray;
|
|
20
|
+
import org.json.JSONException;
|
|
21
|
+
import org.json.JSONObject;
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
public class Chooser extends CordovaPlugin {
|
|
25
|
+
private static final String ACTION_OPEN = "chooseFile";
|
|
26
|
+
private static final int PICK_FILE_REQUEST = 1;
|
|
27
|
+
private static final String TAG = "Chooser";
|
|
28
|
+
|
|
29
|
+
/** @see https://stackoverflow.com/a/23270545/459881 */
|
|
30
|
+
public static String getDisplayName (ContentResolver contentResolver, Uri uri) {
|
|
31
|
+
String[] projection = {MediaStore.MediaColumns.DISPLAY_NAME};
|
|
32
|
+
Cursor metaCursor = contentResolver.query(uri, projection, null, null, null);
|
|
33
|
+
|
|
34
|
+
if (metaCursor != null) {
|
|
35
|
+
try {
|
|
36
|
+
if (metaCursor.moveToFirst()) {
|
|
37
|
+
return metaCursor.getString(0);
|
|
38
|
+
}
|
|
39
|
+
} finally {
|
|
40
|
+
metaCursor.close();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
private CallbackContext callback;
|
|
49
|
+
|
|
50
|
+
public void chooseFile (CallbackContext callbackContext, String accept) {
|
|
51
|
+
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
52
|
+
intent.setType("*/*");
|
|
53
|
+
if (!accept.equals("*/*")) {
|
|
54
|
+
intent.putExtra(Intent.EXTRA_MIME_TYPES, accept.split(","));
|
|
55
|
+
}
|
|
56
|
+
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
57
|
+
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
|
|
58
|
+
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
|
|
59
|
+
|
|
60
|
+
Intent chooser = Intent.createChooser(intent, "Select File");
|
|
61
|
+
cordova.startActivityForResult(this, chooser, Chooser.PICK_FILE_REQUEST);
|
|
62
|
+
|
|
63
|
+
PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
|
|
64
|
+
pluginResult.setKeepCallback(true);
|
|
65
|
+
this.callback = callbackContext;
|
|
66
|
+
callbackContext.sendPluginResult(pluginResult);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@Override
|
|
70
|
+
public boolean execute (
|
|
71
|
+
String action,
|
|
72
|
+
JSONArray args,
|
|
73
|
+
CallbackContext callbackContext
|
|
74
|
+
) {
|
|
75
|
+
try {
|
|
76
|
+
if (action.equals(Chooser.ACTION_OPEN)) {
|
|
77
|
+
this.chooseFile(callbackContext, args.getString(0));
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch (JSONException err) {
|
|
82
|
+
this.callback.error("Execute failed: " + err.toString());
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
@Override
|
|
89
|
+
public void onActivityResult (int requestCode, int resultCode, Intent data) {
|
|
90
|
+
try {
|
|
91
|
+
if (requestCode == Chooser.PICK_FILE_REQUEST && this.callback != null) {
|
|
92
|
+
if (resultCode == Activity.RESULT_OK) {
|
|
93
|
+
Uri uri = data.getData();
|
|
94
|
+
|
|
95
|
+
if (uri != null) {
|
|
96
|
+
ContentResolver contentResolver =
|
|
97
|
+
this.cordova.getActivity().getContentResolver()
|
|
98
|
+
;
|
|
99
|
+
|
|
100
|
+
String name = Chooser.getDisplayName(contentResolver, uri);
|
|
101
|
+
|
|
102
|
+
String mediaType = contentResolver.getType(uri);
|
|
103
|
+
if (mediaType == null || mediaType.isEmpty()) {
|
|
104
|
+
mediaType = "application/octet-stream";
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
JSONObject result = new JSONObject();
|
|
108
|
+
|
|
109
|
+
result.put("mediaType", mediaType);
|
|
110
|
+
result.put("name", name);
|
|
111
|
+
result.put("uri", uri.toString());
|
|
112
|
+
|
|
113
|
+
this.callback.success(result.toString());
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
this.callback.error("File URI was null.");
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
else if (resultCode == Activity.RESULT_CANCELED) {
|
|
120
|
+
this.callback.error("No File selected.");
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
this.callback.error(resultCode);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
catch (Exception err) {
|
|
128
|
+
this.callback.error("Failed to read file: " + err.toString());
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import UIKit
|
|
2
|
+
import MobileCoreServices
|
|
3
|
+
import Foundation
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@objc(Chooser)
|
|
7
|
+
class Chooser : CDVPlugin {
|
|
8
|
+
var commandCallback: String?
|
|
9
|
+
|
|
10
|
+
func callPicker (utis: [String]) {
|
|
11
|
+
let picker = UIDocumentPickerViewController(documentTypes: utis, in: .import)
|
|
12
|
+
picker.delegate = self
|
|
13
|
+
self.viewController.present(picker, animated: true, completion: nil)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
func detectMimeType (_ url: URL) -> String {
|
|
17
|
+
if let uti = UTTypeCreatePreferredIdentifierForTag(
|
|
18
|
+
kUTTagClassFilenameExtension,
|
|
19
|
+
url.pathExtension as CFString,
|
|
20
|
+
nil
|
|
21
|
+
)?.takeRetainedValue() {
|
|
22
|
+
if let mimetype = UTTypeCopyPreferredTagWithClass(
|
|
23
|
+
uti,
|
|
24
|
+
kUTTagClassMIMEType
|
|
25
|
+
)?.takeRetainedValue() as? String {
|
|
26
|
+
return mimetype
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return "application/octet-stream"
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
func documentWasSelected (url: URL) {
|
|
34
|
+
var error: NSError?
|
|
35
|
+
|
|
36
|
+
NSFileCoordinator().coordinate(
|
|
37
|
+
readingItemAt: url,
|
|
38
|
+
options: [],
|
|
39
|
+
error: &error
|
|
40
|
+
) { newURL in
|
|
41
|
+
do {
|
|
42
|
+
let result = [
|
|
43
|
+
"mediaType": self.detectMimeType(newURL),
|
|
44
|
+
"name": newURL.lastPathComponent,
|
|
45
|
+
"uri": newURL.absoluteString
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
if let message = try String(
|
|
49
|
+
data: JSONSerialization.data(
|
|
50
|
+
withJSONObject: result,
|
|
51
|
+
options: []
|
|
52
|
+
),
|
|
53
|
+
encoding: String.Encoding.utf8
|
|
54
|
+
) {
|
|
55
|
+
self.send(message)
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
self.sendError("Serializing result failed.")
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
newURL.stopAccessingSecurityScopedResource()
|
|
62
|
+
}
|
|
63
|
+
catch let error {
|
|
64
|
+
self.sendError(error.localizedDescription)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if let error = error {
|
|
69
|
+
self.sendError(error.localizedDescription)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
url.stopAccessingSecurityScopedResource()
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
@objc(chooseFile:)
|
|
76
|
+
func chooseFile (command: CDVInvokedUrlCommand) {
|
|
77
|
+
self.commandCallback = command.callbackId
|
|
78
|
+
|
|
79
|
+
let accept = command.arguments.first as! String
|
|
80
|
+
let mimeTypes = accept.components(separatedBy: ",")
|
|
81
|
+
|
|
82
|
+
let utis = mimeTypes.map { (mimeType: String) -> String in
|
|
83
|
+
switch mimeType {
|
|
84
|
+
case "audio/*":
|
|
85
|
+
return kUTTypeAudio as String
|
|
86
|
+
case "font/*":
|
|
87
|
+
return "public.font"
|
|
88
|
+
case "image/*":
|
|
89
|
+
return kUTTypeImage as String
|
|
90
|
+
case "text/*":
|
|
91
|
+
return kUTTypeText as String
|
|
92
|
+
case "video/*":
|
|
93
|
+
return kUTTypeVideo as String
|
|
94
|
+
default:
|
|
95
|
+
break
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if mimeType.range(of: "*") == nil {
|
|
99
|
+
let utiUnmanaged = UTTypeCreatePreferredIdentifierForTag(
|
|
100
|
+
kUTTagClassMIMEType,
|
|
101
|
+
mimeType as CFString,
|
|
102
|
+
nil
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
if let uti = (utiUnmanaged?.takeRetainedValue() as? String) {
|
|
106
|
+
if !uti.hasPrefix("dyn.") {
|
|
107
|
+
return uti
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return kUTTypeData as String
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
self.callPicker(utis: utis)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
func send (_ message: String, _ status: CDVCommandStatus = CDVCommandStatus_OK) {
|
|
119
|
+
if let callbackId = self.commandCallback {
|
|
120
|
+
self.commandCallback = nil
|
|
121
|
+
|
|
122
|
+
let pluginResult = CDVPluginResult(
|
|
123
|
+
status: status,
|
|
124
|
+
messageAs: message
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
self.commandDelegate!.send(
|
|
128
|
+
pluginResult,
|
|
129
|
+
callbackId: callbackId
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
func sendError (_ message: String) {
|
|
135
|
+
self.send(message, CDVCommandStatus_ERROR)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
extension Chooser : UIDocumentPickerDelegate {
|
|
140
|
+
@available(iOS 11.0, *)
|
|
141
|
+
func documentPicker (
|
|
142
|
+
_ controller: UIDocumentPickerViewController,
|
|
143
|
+
didPickDocumentsAt urls: [URL]
|
|
144
|
+
) {
|
|
145
|
+
if let url = urls.first {
|
|
146
|
+
self.documentWasSelected(url: url)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
func documentPicker (
|
|
151
|
+
_ controller: UIDocumentPickerViewController,
|
|
152
|
+
didPickDocumentAt url: URL
|
|
153
|
+
) {
|
|
154
|
+
self.documentWasSelected(url: url)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
func documentPickerWasCancelled (_ controller: UIDocumentPickerViewController) {
|
|
158
|
+
self.sendError("No File Selected")
|
|
159
|
+
}
|
|
160
|
+
}
|
package/www/chooser.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
chooseFile: function (successCallback, errorCallback, accept) {
|
|
3
|
+
accept = (typeof accept === 'string' ? accept.replace(/\s/g, '') : undefined) || '*/*'
|
|
4
|
+
var parseResult = function (json) {
|
|
5
|
+
try {
|
|
6
|
+
successCallback(JSON.parse(json));
|
|
7
|
+
}
|
|
8
|
+
catch (err) {
|
|
9
|
+
errorCallback(err);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
cordova.exec(parseResult, errorCallback, 'Chooser', 'chooseFile', [accept]);
|
|
14
|
+
}
|
|
15
|
+
};
|