@capacitor/file-viewer 1.0.0-dev.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/CapacitorFileViewer.podspec +18 -0
- package/LICENSE +21 -0
- package/Package.swift +34 -0
- package/README.md +221 -0
- package/android/build.gradle +68 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/com/capacitorjs/plugins/fileviewer/FileViewerErrors.kt +68 -0
- package/android/src/main/java/com/capacitorjs/plugins/fileviewer/FileViewerPlugin.kt +96 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/definitions.d.ts +74 -0
- package/dist/index.d.ts +4 -0
- package/dist/plugin.cjs +1 -0
- package/dist/plugin.js +1 -0
- package/dist/plugin.mjs +53 -0
- package/dist/web-CLhv9W7r.cjs +1 -0
- package/dist/web-R-VO1Dq5.js +24 -0
- package/dist/web.d.ts +10 -0
- package/ios/Sources/FileViewerPlugin/FileViewerError.swift +42 -0
- package/ios/Sources/FileViewerPlugin/FileViewerPlugin.swift +136 -0
- package/ios/Tests/FileViewerPluginTests/FileViewerPluginTests.swift +15 -0
- package/package.json +102 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = 'CapacitorFileViewer'
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.license = package['license']
|
|
10
|
+
s.homepage = package['repository']['url']
|
|
11
|
+
s.author = package['author']
|
|
12
|
+
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
|
|
13
|
+
s.source_files = 'ios/Sources/FileViewerPlugin/*.{swift,h,m,c,cc,mm,cpp}'
|
|
14
|
+
s.ios.deployment_target = '14.0'
|
|
15
|
+
s.dependency 'IONFileViewerLib', spec='~> 1.0'
|
|
16
|
+
s.dependency 'Capacitor'
|
|
17
|
+
s.swift_version = '5.1'
|
|
18
|
+
end
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Ionic
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Package.swift
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// swift-tools-version: 5.9
|
|
2
|
+
import PackageDescription
|
|
3
|
+
|
|
4
|
+
let package = Package(
|
|
5
|
+
name: "CapacitorFileViewer",
|
|
6
|
+
platforms: [.iOS(.v14)],
|
|
7
|
+
products: [
|
|
8
|
+
.library(
|
|
9
|
+
name: "CapacitorFileViewer",
|
|
10
|
+
targets: ["FileViewerPlugin"])
|
|
11
|
+
],
|
|
12
|
+
dependencies: [
|
|
13
|
+
.package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", branch: "7.0.0")
|
|
14
|
+
],
|
|
15
|
+
targets: [
|
|
16
|
+
.binaryTarget(
|
|
17
|
+
name: "IONFileViewerLib",
|
|
18
|
+
url: "https://github.com/ionic-team/ion-ios-fileviewer/releases/download/1.0.0/IONFileViewerLib.zip",
|
|
19
|
+
checksum: "02728411a63b4dcb630251d6b37f0ea173804a6408d916a0ba3f1ea9d4301b1a" // sha-256
|
|
20
|
+
),
|
|
21
|
+
.target(
|
|
22
|
+
name: "FileViewerPlugin",
|
|
23
|
+
dependencies: [
|
|
24
|
+
.product(name: "Capacitor", package: "capacitor-swift-pm"),
|
|
25
|
+
.product(name: "Cordova", package: "capacitor-swift-pm"),
|
|
26
|
+
"IONFileViewerLib"
|
|
27
|
+
],
|
|
28
|
+
path: "ios/Sources/FileViewerPlugin"),
|
|
29
|
+
.testTarget(
|
|
30
|
+
name: "FileViewerPluginTests",
|
|
31
|
+
dependencies: ["FileViewerPlugin"],
|
|
32
|
+
path: "ios/Tests/FileViewerPluginTests")
|
|
33
|
+
]
|
|
34
|
+
)
|
package/README.md
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
# @capacitor/file-viewer
|
|
2
|
+
|
|
3
|
+
The FileViewer API provides mechanisms for opening files and previewing media. Not available on web.
|
|
4
|
+
|
|
5
|
+
The media preview methods are currently only supported on iOS. It uses a built-in player.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @capacitor/file-viewer
|
|
11
|
+
npx cap sync
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Example
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { FileViewer } from "@capacitor/file-viewer";
|
|
18
|
+
|
|
19
|
+
// can use a plugin like @capacitor/filesystem to get the full path to the file
|
|
20
|
+
const openDocument = async () => {
|
|
21
|
+
await FileViewer.openDocumentFromLocalPath({
|
|
22
|
+
path: "path/to/file.pdf"
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// ios-specific
|
|
27
|
+
const previewMedia = async () => {
|
|
28
|
+
await FileViewer.previewMediaContentFromUrl({
|
|
29
|
+
path: "https://url_hosting_media/file.mp4"
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## API
|
|
35
|
+
|
|
36
|
+
<docgen-index>
|
|
37
|
+
|
|
38
|
+
* [`openDocumentFromLocalPath(...)`](#opendocumentfromlocalpath)
|
|
39
|
+
* [`openDocumentFromResources(...)`](#opendocumentfromresources)
|
|
40
|
+
* [`openDocumentFromUrl(...)`](#opendocumentfromurl)
|
|
41
|
+
* [`previewMediaContentFromLocalPath(...)`](#previewmediacontentfromlocalpath)
|
|
42
|
+
* [`previewMediaContentFromResources(...)`](#previewmediacontentfromresources)
|
|
43
|
+
* [`previewMediaContentFromUrl(...)`](#previewmediacontentfromurl)
|
|
44
|
+
* [Interfaces](#interfaces)
|
|
45
|
+
* [Type Aliases](#type-aliases)
|
|
46
|
+
|
|
47
|
+
</docgen-index>
|
|
48
|
+
|
|
49
|
+
For list of existing error codes, see [Errors](#errors).
|
|
50
|
+
|
|
51
|
+
<docgen-api>
|
|
52
|
+
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
|
|
53
|
+
|
|
54
|
+
File Viewer API
|
|
55
|
+
|
|
56
|
+
Only available in Native Android and iOS; not available for Web / PWAs.
|
|
57
|
+
|
|
58
|
+
### openDocumentFromLocalPath(...)
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
openDocumentFromLocalPath(options: OpenFromLocalPathOptions) => Promise<void>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Open a file stored in the local file system
|
|
65
|
+
|
|
66
|
+
| Param | Type |
|
|
67
|
+
| ------------- | ----------------------------------------------------------------------------- |
|
|
68
|
+
| **`options`** | <code><a href="#openfromlocalpathoptions">OpenFromLocalPathOptions</a></code> |
|
|
69
|
+
|
|
70
|
+
**Since:** 1.0.0
|
|
71
|
+
|
|
72
|
+
--------------------
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
### openDocumentFromResources(...)
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
openDocumentFromResources(options: OpenFromResourcesOptions) => Promise<void>
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Open an app resource file
|
|
82
|
+
|
|
83
|
+
| Param | Type |
|
|
84
|
+
| ------------- | ----------------------------------------------------------------------------- |
|
|
85
|
+
| **`options`** | <code><a href="#openfromresourcesoptions">OpenFromResourcesOptions</a></code> |
|
|
86
|
+
|
|
87
|
+
**Since:** 1.0.0
|
|
88
|
+
|
|
89
|
+
--------------------
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
### openDocumentFromUrl(...)
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
openDocumentFromUrl(options: OpenFromUrlOptions) => Promise<void>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Open a file from a remote url
|
|
99
|
+
|
|
100
|
+
| Param | Type |
|
|
101
|
+
| ------------- | ----------------------------------------------------------------- |
|
|
102
|
+
| **`options`** | <code><a href="#openfromurloptions">OpenFromUrlOptions</a></code> |
|
|
103
|
+
|
|
104
|
+
**Since:** 1.0.0
|
|
105
|
+
|
|
106
|
+
--------------------
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
### previewMediaContentFromLocalPath(...)
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
previewMediaContentFromLocalPath(options: PreviewMediaFromLocalPathOptions) => Promise<void>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Preview a media file (namely, video) stored in the local file system.
|
|
116
|
+
Only implemented in iOS. Android defaults to `openDocumentFromLocalPath`.
|
|
117
|
+
|
|
118
|
+
| Param | Type |
|
|
119
|
+
| ------------- | ----------------------------------------------------------------------------- |
|
|
120
|
+
| **`options`** | <code><a href="#openfromlocalpathoptions">OpenFromLocalPathOptions</a></code> |
|
|
121
|
+
|
|
122
|
+
**Since:** 1.0.0
|
|
123
|
+
|
|
124
|
+
--------------------
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
### previewMediaContentFromResources(...)
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
previewMediaContentFromResources(options: PreviewMediaFromResourcesOptions) => Promise<void>
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Preview a media file (namely, video) from the app's resources.
|
|
134
|
+
Only implemented in iOS. Android defaults to `openDocumentFromResources`.
|
|
135
|
+
|
|
136
|
+
| Param | Type |
|
|
137
|
+
| ------------- | ----------------------------------------------------------------------------- |
|
|
138
|
+
| **`options`** | <code><a href="#openfromresourcesoptions">OpenFromResourcesOptions</a></code> |
|
|
139
|
+
|
|
140
|
+
**Since:** 1.0.0
|
|
141
|
+
|
|
142
|
+
--------------------
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
### previewMediaContentFromUrl(...)
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
previewMediaContentFromUrl(options: PreviewMediaFromUrlOptions) => Promise<void>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Preview a media file (namely, video) from a remote url.
|
|
152
|
+
Only implemented in iOS. Android defaults to `openDocumentFromUrl`.
|
|
153
|
+
|
|
154
|
+
| Param | Type |
|
|
155
|
+
| ------------- | ----------------------------------------------------------------- |
|
|
156
|
+
| **`options`** | <code><a href="#openfromurloptions">OpenFromUrlOptions</a></code> |
|
|
157
|
+
|
|
158
|
+
**Since:** 1.0.0
|
|
159
|
+
|
|
160
|
+
--------------------
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
### Interfaces
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
#### OpenFromLocalPathOptions
|
|
167
|
+
|
|
168
|
+
| Prop | Type | Description | Since |
|
|
169
|
+
| ---------- | ------------------- | ------------------------------------------ | ----- |
|
|
170
|
+
| **`path`** | <code>string</code> | The full absolute path to the file to open | 1.0.0 |
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
#### OpenFromResourcesOptions
|
|
174
|
+
|
|
175
|
+
| Prop | Type | Description | Since |
|
|
176
|
+
| ---------- | ------------------- | ---------------------------------------------- | ----- |
|
|
177
|
+
| **`path`** | <code>string</code> | The relative path to the resource file to open | 1.0.0 |
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
#### OpenFromUrlOptions
|
|
181
|
+
|
|
182
|
+
| Prop | Type | Description | Since |
|
|
183
|
+
| --------- | ------------------- | ------------------------------------------- | ----- |
|
|
184
|
+
| **`url`** | <code>string</code> | The remote url pointing to the file to open | 1.0.0 |
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
### Type Aliases
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
#### PreviewMediaFromLocalPathOptions
|
|
191
|
+
|
|
192
|
+
<code><a href="#openfromlocalpathoptions">OpenFromLocalPathOptions</a></code>
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
#### PreviewMediaFromResourcesOptions
|
|
196
|
+
|
|
197
|
+
<code><a href="#openfromresourcesoptions">OpenFromResourcesOptions</a></code>
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
#### PreviewMediaFromUrlOptions
|
|
201
|
+
|
|
202
|
+
<code><a href="#openfromurloptions">OpenFromUrlOptions</a></code>
|
|
203
|
+
|
|
204
|
+
</docgen-api>
|
|
205
|
+
|
|
206
|
+
### Errors
|
|
207
|
+
|
|
208
|
+
The plugin returns the following errors with specific codes on native Android and iOS:
|
|
209
|
+
|
|
210
|
+
| Error code | Platform(s) | Message |
|
|
211
|
+
|-------------------|------------------|------------------------------|
|
|
212
|
+
| OS-PLUG-FLVW-0004 | Android, iOS | The file you are trying to open does not exist. |
|
|
213
|
+
| OS-PLUG-FLVW-0005 | Android, iOS | The URL you are trying to open is malformed. |
|
|
214
|
+
| OS-PLUG-FLVW-0006 | Android, iOS | Path of the file to open is either null or empty. |
|
|
215
|
+
| OS-PLUG-FLVW-0007 | Android, iOS | URL to open is either null or empty. |
|
|
216
|
+
| OS-PLUG-FLVW-0008 | Android, iOS | Could not open the file. |
|
|
217
|
+
| OS-PLUG-FLVW-0009 | Android, iOS | Invalid parameters. |
|
|
218
|
+
| OS-PLUG-FLVW-0010 | Android | There is no app to open this file. |
|
|
219
|
+
| OS-PLUG-FLVW-0011 | iOS | Cordova / Capacitor bridge isn’t initialized. |
|
|
220
|
+
| OS-PLUG-FLVW-0012 | iOS | The download failed. |
|
|
221
|
+
| OS-PLUG-FLVW-0013 | iOS | The file has no extension. |
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
ext {
|
|
2
|
+
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
|
|
3
|
+
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.0'
|
|
4
|
+
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.2.1'
|
|
5
|
+
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.6.1'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
buildscript {
|
|
9
|
+
ext.kotlin_version = project.hasProperty("kotlin_version") ? rootProject.ext.kotlin_version : '1.9.24'
|
|
10
|
+
repositories {
|
|
11
|
+
google()
|
|
12
|
+
mavenCentral()
|
|
13
|
+
}
|
|
14
|
+
dependencies {
|
|
15
|
+
classpath 'com.android.tools.build:gradle:8.7.3'
|
|
16
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
apply plugin: 'com.android.library'
|
|
21
|
+
apply plugin: 'kotlin-android'
|
|
22
|
+
|
|
23
|
+
android {
|
|
24
|
+
namespace "com.capacitorjs.plugins.fileviewer"
|
|
25
|
+
compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 35
|
|
26
|
+
defaultConfig {
|
|
27
|
+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 23
|
|
28
|
+
targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 35
|
|
29
|
+
versionCode 1
|
|
30
|
+
versionName "1.0"
|
|
31
|
+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
|
32
|
+
}
|
|
33
|
+
buildTypes {
|
|
34
|
+
release {
|
|
35
|
+
minifyEnabled false
|
|
36
|
+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
lintOptions {
|
|
40
|
+
abortOnError false
|
|
41
|
+
}
|
|
42
|
+
compileOptions {
|
|
43
|
+
sourceCompatibility JavaVersion.VERSION_21
|
|
44
|
+
targetCompatibility JavaVersion.VERSION_21
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
kotlin {
|
|
49
|
+
jvmToolchain(21)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
repositories {
|
|
53
|
+
google()
|
|
54
|
+
mavenCentral()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
dependencies {
|
|
59
|
+
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
|
60
|
+
implementation project(':capacitor-android')
|
|
61
|
+
implementation "io.ionic.libs:ionfileviewer-android:1.0.0"
|
|
62
|
+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
63
|
+
|
|
64
|
+
testImplementation "junit:junit:$junitVersion"
|
|
65
|
+
|
|
66
|
+
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
|
|
67
|
+
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|
|
68
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<manifest />
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
package com.capacitorjs.plugins.fileviewer
|
|
2
|
+
|
|
3
|
+
import io.ionic.libs.ionfileviewerlib.model.IONFLVWException
|
|
4
|
+
|
|
5
|
+
object FileViewerErrors {
|
|
6
|
+
private fun formatErrorCode(number: Int): String {
|
|
7
|
+
return "OS-PLUG-FLVW-" + number.toString().padStart(4, '0')
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
data class ErrorInfo(
|
|
11
|
+
val code: String,
|
|
12
|
+
val message: String
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
val fileDoesNotExist = ErrorInfo(
|
|
16
|
+
code = formatErrorCode(4),
|
|
17
|
+
message = "The file you are trying to open does not exist."
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
fun urlMalformed(url: String) = if (url.isBlank()) {
|
|
21
|
+
urlEmpty
|
|
22
|
+
} else {
|
|
23
|
+
ErrorInfo(
|
|
24
|
+
code = formatErrorCode(5),
|
|
25
|
+
message = "The URL you are trying to open is malformed - $url"
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
fun filePathInvalid(path: String?) = if (path.isNullOrBlank()) {
|
|
30
|
+
filePathEmpty
|
|
31
|
+
} else {
|
|
32
|
+
invalidParameters
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
val filePathEmpty = ErrorInfo(
|
|
36
|
+
code = formatErrorCode(6),
|
|
37
|
+
message = "Path of the file to open is either null or empty."
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
val urlEmpty = ErrorInfo(
|
|
41
|
+
code = formatErrorCode(7),
|
|
42
|
+
message = "URL to open is either null or empty."
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
val genericError = ErrorInfo(
|
|
46
|
+
code = formatErrorCode(8),
|
|
47
|
+
message = "Could not open the document."
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
val invalidParameters: ErrorInfo = ErrorInfo(
|
|
51
|
+
code = formatErrorCode(9),
|
|
52
|
+
message = "Invalid parameters."
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
val noAppToOpen = ErrorInfo(
|
|
56
|
+
code = formatErrorCode(10),
|
|
57
|
+
message = "There is no app to open this document."
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
fun Throwable.toFileViewerError(): FileViewerErrors.ErrorInfo = when (this) {
|
|
62
|
+
is IONFLVWException.FileDoesNotExist -> FileViewerErrors.fileDoesNotExist
|
|
63
|
+
is IONFLVWException.InvalidURL -> FileViewerErrors.urlMalformed(url)
|
|
64
|
+
is IONFLVWException.InvalidPath -> FileViewerErrors.filePathInvalid(path)
|
|
65
|
+
is IONFLVWException.EmptyURL -> FileViewerErrors.urlEmpty
|
|
66
|
+
is IONFLVWException.NoApp -> FileViewerErrors.noAppToOpen
|
|
67
|
+
else -> FileViewerErrors.genericError
|
|
68
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
package com.capacitorjs.plugins.fileviewer
|
|
2
|
+
|
|
3
|
+
import android.app.Activity
|
|
4
|
+
import com.getcapacitor.Plugin
|
|
5
|
+
import com.getcapacitor.PluginCall
|
|
6
|
+
import com.getcapacitor.PluginMethod
|
|
7
|
+
import com.getcapacitor.annotation.CapacitorPlugin
|
|
8
|
+
import io.ionic.libs.ionfileviewerlib.IONFLVWController
|
|
9
|
+
import kotlinx.coroutines.CoroutineScope
|
|
10
|
+
import kotlinx.coroutines.Dispatchers
|
|
11
|
+
import kotlinx.coroutines.cancel
|
|
12
|
+
import kotlinx.coroutines.launch
|
|
13
|
+
|
|
14
|
+
@CapacitorPlugin(name = "FileViewer")
|
|
15
|
+
class FileViewerPlugin : Plugin() {
|
|
16
|
+
|
|
17
|
+
private val ioScope: CoroutineScope by lazy { CoroutineScope(Dispatchers.IO) }
|
|
18
|
+
private val controller: IONFLVWController by lazy { IONFLVWController() }
|
|
19
|
+
|
|
20
|
+
override fun handleOnDestroy() {
|
|
21
|
+
super.handleOnDestroy()
|
|
22
|
+
ioScope.cancel()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@PluginMethod
|
|
26
|
+
fun openDocumentFromLocalPath(call: PluginCall) {
|
|
27
|
+
val filePath: String? = call.getString("path")
|
|
28
|
+
checkPreconditionsAndRun(call) { activity ->
|
|
29
|
+
controller.openDocumentFromLocalPath(activity, filePath ?: "")
|
|
30
|
+
.handleResult(call)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
@PluginMethod
|
|
35
|
+
fun openDocumentFromResources(call: PluginCall) {
|
|
36
|
+
val resourcePath: String? = call.getString("path")
|
|
37
|
+
checkPreconditionsAndRun(call) { activity ->
|
|
38
|
+
// this method needs to be called from a non-UI thread, because it can involve I/O operations.
|
|
39
|
+
ioScope.launch {
|
|
40
|
+
controller.openDocumentFromResources(activity, assetPath = resourcePath ?: "")
|
|
41
|
+
.handleResult(call)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
@PluginMethod
|
|
47
|
+
fun openDocumentFromUrl(call: PluginCall) {
|
|
48
|
+
val url: String? = call.getString("url")
|
|
49
|
+
checkPreconditionsAndRun(call) { activity ->
|
|
50
|
+
controller.openDocumentFromUrl(activity, url ?: "")
|
|
51
|
+
.handleResult(call)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
@PluginMethod
|
|
56
|
+
fun previewMediaContentFromLocalPath(call: PluginCall) = openDocumentFromLocalPath(call)
|
|
57
|
+
|
|
58
|
+
@PluginMethod
|
|
59
|
+
fun previewMediaContentFromResources(call: PluginCall) = openDocumentFromResources(call)
|
|
60
|
+
|
|
61
|
+
@PluginMethod
|
|
62
|
+
fun previewMediaContentFromUrl(call: PluginCall) = openDocumentFromUrl(call)
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Handle a result from the native library
|
|
66
|
+
*
|
|
67
|
+
* @param call the capacitor plugin call - to notify of success or error
|
|
68
|
+
*/
|
|
69
|
+
private fun Result<Unit>.handleResult(call: PluginCall) = onSuccess {
|
|
70
|
+
call.resolve()
|
|
71
|
+
}.onFailure {
|
|
72
|
+
call.sendError(it.toFileViewerError())
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* check if preconditions match before running the capacitor plugin method
|
|
78
|
+
*
|
|
79
|
+
* @param call the capacitor plugin call to notify of errors in case preconditions are not met
|
|
80
|
+
* @param runner callback that is run in case preconditions are met
|
|
81
|
+
*/
|
|
82
|
+
private fun checkPreconditionsAndRun(call: PluginCall, runner: (Activity) -> Unit) {
|
|
83
|
+
activity?.also {
|
|
84
|
+
runner(it)
|
|
85
|
+
} ?: run {
|
|
86
|
+
call.sendError(FileViewerErrors.genericError)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Extension function to return a unsuccessful plugin result
|
|
92
|
+
* @param error error class representing the error to return, containing a code and message
|
|
93
|
+
*/
|
|
94
|
+
private fun PluginCall.sendError(error: FileViewerErrors.ErrorInfo) =
|
|
95
|
+
this.reject(error.message, error.code)
|
|
96
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export interface OpenFromLocalPathOptions {
|
|
2
|
+
/**
|
|
3
|
+
* The full absolute path to the file to open
|
|
4
|
+
* @since 1.0.0
|
|
5
|
+
*/
|
|
6
|
+
path: string;
|
|
7
|
+
}
|
|
8
|
+
export interface OpenFromResourcesOptions {
|
|
9
|
+
/**
|
|
10
|
+
* The relative path to the resource file to open
|
|
11
|
+
* @since 1.0.0
|
|
12
|
+
*/
|
|
13
|
+
path: string;
|
|
14
|
+
}
|
|
15
|
+
export interface OpenFromUrlOptions {
|
|
16
|
+
/**
|
|
17
|
+
* The remote url pointing to the file to open
|
|
18
|
+
* @since 1.0.0
|
|
19
|
+
*/
|
|
20
|
+
url: string;
|
|
21
|
+
}
|
|
22
|
+
export type PreviewMediaFromLocalPathOptions = OpenFromLocalPathOptions;
|
|
23
|
+
export type PreviewMediaFromResourcesOptions = OpenFromResourcesOptions;
|
|
24
|
+
export type PreviewMediaFromUrlOptions = OpenFromUrlOptions;
|
|
25
|
+
/**
|
|
26
|
+
* File Viewer API
|
|
27
|
+
*
|
|
28
|
+
* Only available in Native Android and iOS; not available for Web / PWAs.
|
|
29
|
+
*/
|
|
30
|
+
export interface FileViewerPlugin {
|
|
31
|
+
/**
|
|
32
|
+
* Open a file stored in the local file system
|
|
33
|
+
*
|
|
34
|
+
* @since 1.0.0
|
|
35
|
+
*/
|
|
36
|
+
openDocumentFromLocalPath(options: OpenFromLocalPathOptions): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Open an app resource file
|
|
39
|
+
*
|
|
40
|
+
* @since 1.0.0
|
|
41
|
+
*/
|
|
42
|
+
openDocumentFromResources(options: OpenFromResourcesOptions): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Open a file from a remote url
|
|
45
|
+
*
|
|
46
|
+
* @since 1.0.0
|
|
47
|
+
*/
|
|
48
|
+
openDocumentFromUrl(options: OpenFromUrlOptions): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Preview a media file (namely, video) stored in the local file system.
|
|
51
|
+
* Only implemented in iOS. Android defaults to `openDocumentFromLocalPath`.
|
|
52
|
+
*
|
|
53
|
+
* @since 1.0.0
|
|
54
|
+
*/
|
|
55
|
+
previewMediaContentFromLocalPath(options: PreviewMediaFromLocalPathOptions): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Preview a media file (namely, video) from the app's resources.
|
|
58
|
+
* Only implemented in iOS. Android defaults to `openDocumentFromResources`.
|
|
59
|
+
*
|
|
60
|
+
* @since 1.0.0
|
|
61
|
+
*/
|
|
62
|
+
previewMediaContentFromResources(options: PreviewMediaFromResourcesOptions): Promise<void>;
|
|
63
|
+
/**
|
|
64
|
+
* Preview a media file (namely, video) from a remote url.
|
|
65
|
+
* Only implemented in iOS. Android defaults to `openDocumentFromUrl`.
|
|
66
|
+
*
|
|
67
|
+
* @since 1.0.0
|
|
68
|
+
*/
|
|
69
|
+
previewMediaContentFromUrl(options: PreviewMediaFromUrlOptions): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
export type PluginError = {
|
|
72
|
+
code: string;
|
|
73
|
+
message: string;
|
|
74
|
+
};
|
package/dist/index.d.ts
ADDED
package/dist/plugin.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("@capacitor/core");function s(e){e.CapacitorUtils.Synapse=new Proxy({},{get(a,i){return new Proxy({},{get(f,o){return(w,c,r)=>{const n=e.Capacitor.Plugins[i];if(n===void 0){r(new Error(`Capacitor plugin ${i} not found`));return}if(typeof n[o]!="function"){r(new Error(`Method ${o} not found in Capacitor plugin ${i}`));return}(async()=>{try{const t=await n[o](w);c(t)}catch(t){r(t)}})()}}})}})}function l(e){e.CapacitorUtils.Synapse=new Proxy({},{get(a,i){return e.cordova.plugins[i]}})}function d(e=!1){window.CapacitorUtils=window.CapacitorUtils||{},window.Capacitor!==void 0&&!e?s(window):window.cordova!==void 0&&l(window)}const p=u.registerPlugin("FileViewer",{web:()=>Promise.resolve().then(()=>require("./web-CLhv9W7r.cjs")).then(e=>new e.FileViewerWeb)});d();exports.FileViewer=p;
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(o,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("@capacitor/core")):typeof define=="function"&&define.amd?define(["exports","@capacitor/core"],i):(o=typeof globalThis<"u"?globalThis:o||self,i(o.CapacitorFileViewer={},o.capacitorExports))})(this,function(o,i){"use strict";function a(e){e.CapacitorUtils.Synapse=new Proxy({},{get(t,n){return new Proxy({},{get(b,r){return(f,P,c)=>{const p=e.Capacitor.Plugins[n];if(p===void 0){c(new Error(`Capacitor plugin ${n} not found`));return}if(typeof p[r]!="function"){c(new Error(`Method ${r} not found in Capacitor plugin ${n}`));return}(async()=>{try{const s=await p[r](f);P(s)}catch(s){c(s)}})()}}})}})}function u(e){e.CapacitorUtils.Synapse=new Proxy({},{get(t,n){return e.cordova.plugins[n]}})}function d(e=!1){window.CapacitorUtils=window.CapacitorUtils||{},window.Capacitor!==void 0&&!e?a(window):window.cordova!==void 0&&u(window)}const l=i.registerPlugin("FileViewer",{web:()=>Promise.resolve().then(()=>m).then(e=>new e.FileViewerWeb)});d();class w extends i.WebPlugin{openDocumentFromLocalPath(t){return Promise.reject("Not implemented in web")}openDocumentFromResources(t){return Promise.reject("Not implemented in web")}openDocumentFromUrl(t){return Promise.reject("Not implemented in web")}previewMediaContentFromLocalPath(t){return Promise.reject("Not implemented in web")}previewMediaContentFromResources(t){return Promise.reject("Not implemented in web")}previewMediaContentFromUrl(t){return Promise.reject("Not implemented in web")}}const m=Object.freeze(Object.defineProperty({__proto__:null,FileViewerWeb:w},Symbol.toStringTag,{value:"Module"}));o.FileViewer=l,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
|
package/dist/plugin.mjs
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { registerPlugin as p } from "@capacitor/core";
|
|
2
|
+
function u(i) {
|
|
3
|
+
i.CapacitorUtils.Synapse = new Proxy(
|
|
4
|
+
{},
|
|
5
|
+
{
|
|
6
|
+
get(a, o) {
|
|
7
|
+
return new Proxy({}, {
|
|
8
|
+
get(l, n) {
|
|
9
|
+
return (w, c, r) => {
|
|
10
|
+
const t = i.Capacitor.Plugins[o];
|
|
11
|
+
if (t === void 0) {
|
|
12
|
+
r(new Error(`Capacitor plugin ${o} not found`));
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (typeof t[n] != "function") {
|
|
16
|
+
r(new Error(`Method ${n} not found in Capacitor plugin ${o}`));
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
(async () => {
|
|
20
|
+
try {
|
|
21
|
+
const e = await t[n](w);
|
|
22
|
+
c(e);
|
|
23
|
+
} catch (e) {
|
|
24
|
+
r(e);
|
|
25
|
+
}
|
|
26
|
+
})();
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
function s(i) {
|
|
35
|
+
i.CapacitorUtils.Synapse = new Proxy(
|
|
36
|
+
{},
|
|
37
|
+
{
|
|
38
|
+
get(a, o) {
|
|
39
|
+
return i.cordova.plugins[o];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
function d(i = !1) {
|
|
45
|
+
window.CapacitorUtils = window.CapacitorUtils || {}, window.Capacitor !== void 0 && !i ? u(window) : window.cordova !== void 0 && s(window);
|
|
46
|
+
}
|
|
47
|
+
const g = p("FileViewer", {
|
|
48
|
+
web: () => import("./web-R-VO1Dq5.js").then((i) => new i.FileViewerWeb())
|
|
49
|
+
});
|
|
50
|
+
d();
|
|
51
|
+
export {
|
|
52
|
+
g as FileViewer
|
|
53
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("@capacitor/core");class t extends o.WebPlugin{openDocumentFromLocalPath(e){return Promise.reject("Not implemented in web")}openDocumentFromResources(e){return Promise.reject("Not implemented in web")}openDocumentFromUrl(e){return Promise.reject("Not implemented in web")}previewMediaContentFromLocalPath(e){return Promise.reject("Not implemented in web")}previewMediaContentFromResources(e){return Promise.reject("Not implemented in web")}previewMediaContentFromUrl(e){return Promise.reject("Not implemented in web")}}exports.FileViewerWeb=t;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { WebPlugin as o } from "@capacitor/core";
|
|
2
|
+
class n extends o {
|
|
3
|
+
openDocumentFromLocalPath(e) {
|
|
4
|
+
return Promise.reject("Not implemented in web");
|
|
5
|
+
}
|
|
6
|
+
openDocumentFromResources(e) {
|
|
7
|
+
return Promise.reject("Not implemented in web");
|
|
8
|
+
}
|
|
9
|
+
openDocumentFromUrl(e) {
|
|
10
|
+
return Promise.reject("Not implemented in web");
|
|
11
|
+
}
|
|
12
|
+
previewMediaContentFromLocalPath(e) {
|
|
13
|
+
return Promise.reject("Not implemented in web");
|
|
14
|
+
}
|
|
15
|
+
previewMediaContentFromResources(e) {
|
|
16
|
+
return Promise.reject("Not implemented in web");
|
|
17
|
+
}
|
|
18
|
+
previewMediaContentFromUrl(e) {
|
|
19
|
+
return Promise.reject("Not implemented in web");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
n as FileViewerWeb
|
|
24
|
+
};
|
package/dist/web.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { WebPlugin } from '@capacitor/core';
|
|
2
|
+
import { FileViewerPlugin, OpenFromLocalPathOptions, OpenFromResourcesOptions, OpenFromUrlOptions, PreviewMediaFromLocalPathOptions, PreviewMediaFromResourcesOptions, PreviewMediaFromUrlOptions } from './definitions';
|
|
3
|
+
export declare class FileViewerWeb extends WebPlugin implements FileViewerPlugin {
|
|
4
|
+
openDocumentFromLocalPath(_options: OpenFromLocalPathOptions): Promise<void>;
|
|
5
|
+
openDocumentFromResources(_options: OpenFromResourcesOptions): Promise<void>;
|
|
6
|
+
openDocumentFromUrl(_options: OpenFromUrlOptions): Promise<void>;
|
|
7
|
+
previewMediaContentFromLocalPath(_options: PreviewMediaFromLocalPathOptions): Promise<void>;
|
|
8
|
+
previewMediaContentFromResources(_options: PreviewMediaFromResourcesOptions): Promise<void>;
|
|
9
|
+
previewMediaContentFromUrl(_options: PreviewMediaFromUrlOptions): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
enum FileViewerError: Error {
|
|
2
|
+
case fileDoesNotExist
|
|
3
|
+
case urlMalformed(url: String)
|
|
4
|
+
case pathEmpty
|
|
5
|
+
case urlEmpty
|
|
6
|
+
case couldNotOpenDocument
|
|
7
|
+
case bridgeNotInitialised
|
|
8
|
+
case downloadFailed
|
|
9
|
+
case missingFileExtension
|
|
10
|
+
|
|
11
|
+
func toCodeMessagePair() -> (code: String, message: String) {
|
|
12
|
+
("OS-PLUG-FLVW-\(String(format: "%04d", code))", description)
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
private extension FileViewerError {
|
|
17
|
+
var code: Int {
|
|
18
|
+
switch self {
|
|
19
|
+
case .fileDoesNotExist: 4
|
|
20
|
+
case .urlMalformed: 5
|
|
21
|
+
case .pathEmpty: 6
|
|
22
|
+
case .urlEmpty: 7
|
|
23
|
+
case .couldNotOpenDocument: 8
|
|
24
|
+
case .bridgeNotInitialised: 11
|
|
25
|
+
case .downloadFailed: 12
|
|
26
|
+
case .missingFileExtension: 13
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
var description: String {
|
|
31
|
+
switch self {
|
|
32
|
+
case .fileDoesNotExist: "The file you are trying to open does not exist."
|
|
33
|
+
case .urlMalformed(let url): "The URL you are trying to open is malformed - \(url)"
|
|
34
|
+
case .pathEmpty: "Path of the file to open is either null or empty."
|
|
35
|
+
case .urlEmpty: "URL to open is either null or empty."
|
|
36
|
+
case .couldNotOpenDocument: "Could not open the document."
|
|
37
|
+
case .downloadFailed: "The download failed."
|
|
38
|
+
case .missingFileExtension: "The file has no extension."
|
|
39
|
+
case .bridgeNotInitialised: "Cordova bridge isn't initialized."
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import Capacitor
|
|
2
|
+
import IONFileViewerLib
|
|
3
|
+
import UIKit
|
|
4
|
+
|
|
5
|
+
typealias FileViewerService = any IONFLVWOpenDocumentManager & IONFLVWPreviewMediaManager
|
|
6
|
+
|
|
7
|
+
@objc(FileViewerPlugin)
|
|
8
|
+
public class FileViewerPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
9
|
+
public let identifier = "FileViewerPlugin"
|
|
10
|
+
public let jsName = "FileViewer"
|
|
11
|
+
public let pluginMethods: [CAPPluginMethod] = [
|
|
12
|
+
CAPPluginMethod(name: "openDocumentFromLocalPath", returnType: CAPPluginReturnPromise),
|
|
13
|
+
CAPPluginMethod(name: "openDocumentFromResources", returnType: CAPPluginReturnPromise),
|
|
14
|
+
CAPPluginMethod(name: "openDocumentFromUrl", returnType: CAPPluginReturnPromise),
|
|
15
|
+
CAPPluginMethod(name: "previewMediaContentFromLocalPath", returnType: CAPPluginReturnPromise),
|
|
16
|
+
CAPPluginMethod(name: "previewMediaContentFromResources", returnType: CAPPluginReturnPromise),
|
|
17
|
+
CAPPluginMethod(name: "previewMediaContentFromUrl", returnType: CAPPluginReturnPromise)
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
private var fileViewer: FileViewerService?
|
|
21
|
+
|
|
22
|
+
override public func load() {
|
|
23
|
+
if let viewController = self.bridge?.viewController as? UIViewController {
|
|
24
|
+
fileViewer = IONFLVWManager(viewController: viewController)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
func getService() -> Result<FileViewerService, FileViewerError> {
|
|
29
|
+
if fileViewer == nil { load() }
|
|
30
|
+
return fileViewer.map(Result.success) ?? .failure(.bridgeNotInitialised)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// MARK: - Public API Methods
|
|
35
|
+
private extension FileViewerPlugin {
|
|
36
|
+
@objc func openDocumentFromLocalPath(_ call: CAPPluginCall) {
|
|
37
|
+
let filePath = call.getString("path")
|
|
38
|
+
executeOperation(call, operationRunner: { service in
|
|
39
|
+
do {
|
|
40
|
+
try service.openDocumentFromLocalPath(filePath: filePath ?? "", completion: { call.resolve() })
|
|
41
|
+
} catch {
|
|
42
|
+
self.sendError(call, self.mapError(error))
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@objc func openDocumentFromResources(_ call: CAPPluginCall) {
|
|
48
|
+
let resourcePath = call.getString("path")
|
|
49
|
+
executeOperation(call, operationRunner: { service in
|
|
50
|
+
do {
|
|
51
|
+
try service.openDocumentFromResources(assetPath: resourcePath ?? "", completion: { call.resolve() })
|
|
52
|
+
} catch {
|
|
53
|
+
self.sendError(call, self.mapError(error))
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
@objc func openDocumentFromUrl(_ call: CAPPluginCall) {
|
|
59
|
+
let url = call.getString("url")
|
|
60
|
+
executeOperation(call, operationRunner: { service in
|
|
61
|
+
do {
|
|
62
|
+
try service.openDocumentFromUrl(url: url ?? "", completion: { err in
|
|
63
|
+
if let error = err {
|
|
64
|
+
self.sendError(call, self.mapError(error))
|
|
65
|
+
} else {
|
|
66
|
+
call.resolve()
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
} catch {
|
|
70
|
+
self.sendError(call, self.mapError(error))
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
@objc func previewMediaContentFromLocalPath(_ call: CAPPluginCall) {
|
|
76
|
+
let filePath = call.getString("path")
|
|
77
|
+
executeOperation(call, operationRunner: { service in
|
|
78
|
+
do {
|
|
79
|
+
try service.previewMediaContentFromLocalPath(filePath: filePath ?? "")
|
|
80
|
+
call.resolve()
|
|
81
|
+
} catch {
|
|
82
|
+
self.sendError(call, self.mapError(error))
|
|
83
|
+
}
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
@objc func previewMediaContentFromResources(_ call: CAPPluginCall) {
|
|
88
|
+
let resourcePath = call.getString("path")
|
|
89
|
+
executeOperation(call, operationRunner: { service in
|
|
90
|
+
do {
|
|
91
|
+
try service.previewMediaContentFromResources(assetPath: resourcePath ?? "")
|
|
92
|
+
call.resolve()
|
|
93
|
+
} catch {
|
|
94
|
+
self.sendError(call, self.mapError(error))
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
@objc func previewMediaContentFromUrl(_ call: CAPPluginCall) {
|
|
100
|
+
let url = call.getString("url")
|
|
101
|
+
executeOperation(call, operationRunner: { service in
|
|
102
|
+
do {
|
|
103
|
+
try service.previewMediaContentFromUrl(url: url ?? "")
|
|
104
|
+
call.resolve()
|
|
105
|
+
} catch {
|
|
106
|
+
self.sendError(call, self.mapError(error))
|
|
107
|
+
}
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
func executeOperation(_ call: CAPPluginCall, operationRunner: @escaping (FileViewerService) -> Void) {
|
|
112
|
+
switch getService() {
|
|
113
|
+
case .success(let service): DispatchQueue.main.async {
|
|
114
|
+
operationRunner(service)
|
|
115
|
+
}
|
|
116
|
+
case .failure(let error): sendError(call, error)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
func sendError(_ call: CAPPluginCall, _ error: FileViewerError) {
|
|
121
|
+
let errorPair = error.toCodeMessagePair()
|
|
122
|
+
call.reject(errorPair.message, errorPair.code)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
func mapError(_ error: Error, url: String = "") -> FileViewerError {
|
|
126
|
+
return switch error {
|
|
127
|
+
case IONFLVWError.fileDoesNotExist: .fileDoesNotExist
|
|
128
|
+
case IONFLVWError.invalidURL: .urlMalformed(url: url)
|
|
129
|
+
case IONFLVWError.emptyFilePath: .pathEmpty
|
|
130
|
+
case IONFLVWError.invalidEmptyURL: .urlEmpty
|
|
131
|
+
case IONFLVWError.downloadFailed: .downloadFailed
|
|
132
|
+
case IONFLVWError.missingFileExtension: .missingFileExtension
|
|
133
|
+
default: .couldNotOpenDocument
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import XCTest
|
|
2
|
+
@testable import FileViewerPlugin
|
|
3
|
+
|
|
4
|
+
class FileViewerTests: XCTestCase {
|
|
5
|
+
func testEcho() {
|
|
6
|
+
// This is an example of a functional test case for a plugin.
|
|
7
|
+
// Use XCTAssert and related functions to verify your tests produce the correct results.
|
|
8
|
+
|
|
9
|
+
let implementation = FileViewer()
|
|
10
|
+
let value = "Hello, World!"
|
|
11
|
+
let result = implementation.echo(value)
|
|
12
|
+
|
|
13
|
+
XCTAssertEqual(value, result)
|
|
14
|
+
}
|
|
15
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@capacitor/file-viewer",
|
|
3
|
+
"version": "1.0.0-dev.1",
|
|
4
|
+
"description": "The FileViewer API provides mechanisms for opening files and previewing media. Not available on web.",
|
|
5
|
+
"main": "./dist/plugin.cjs",
|
|
6
|
+
"module": "./dist/plugin.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/plugin.mjs",
|
|
13
|
+
"require": "./dist/plugin.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"unpkg": "dist/plugin.js",
|
|
17
|
+
"files": [
|
|
18
|
+
"android/src/main/",
|
|
19
|
+
"android/build.gradle",
|
|
20
|
+
"dist/",
|
|
21
|
+
"ios/Sources",
|
|
22
|
+
"ios/Tests",
|
|
23
|
+
"Package.swift",
|
|
24
|
+
"CapacitorFileViewer.podspec"
|
|
25
|
+
],
|
|
26
|
+
"author": "Ionic",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/ionic-team/capacitor-file-viewer.git"
|
|
31
|
+
},
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/ionic-team/capacitor-file-viewer/issues"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"capacitor",
|
|
37
|
+
"plugin",
|
|
38
|
+
"native"
|
|
39
|
+
],
|
|
40
|
+
"scripts": {
|
|
41
|
+
"verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
|
|
42
|
+
"verify:ios": "xcodebuild -scheme CapacitorFileViewer -destination generic/platform=iOS",
|
|
43
|
+
"verify:android": "cd android && ./gradlew clean build test && cd ..",
|
|
44
|
+
"verify:web": "npm run build",
|
|
45
|
+
"lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
|
|
46
|
+
"fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
|
|
47
|
+
"eslint": "eslint",
|
|
48
|
+
"prettier": "prettier \"src/**/*.{css,html,ts,js,java}\"",
|
|
49
|
+
"swiftlint": "node-swiftlint",
|
|
50
|
+
"docgen": "docgen --api FileViewerPlugin --output-readme README.md --output-json dist/docs/docs.json",
|
|
51
|
+
"build": "npm run clean && npm run docgen && vite build",
|
|
52
|
+
"clean": "rimraf ./dist",
|
|
53
|
+
"watch": "tsc --watch",
|
|
54
|
+
"prepublishOnly": "npm run build",
|
|
55
|
+
"semantic-release": "semantic-release"
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"@capacitor/synapse": "^1.0.2"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@capacitor/android": "^7.0.0",
|
|
62
|
+
"@capacitor/core": "^7.0.0",
|
|
63
|
+
"@capacitor/docgen": "^0.2.2",
|
|
64
|
+
"@capacitor/ios": "^7.0.0",
|
|
65
|
+
"@eslint/js": "^8.56.0",
|
|
66
|
+
"@rollup/wasm-node": "~4.19.0",
|
|
67
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
68
|
+
"@semantic-release/git": "^10.0.1",
|
|
69
|
+
"@semantic-release/github": "^10.1.2",
|
|
70
|
+
"@semantic-release/npm": "^12.0.1",
|
|
71
|
+
"@typescript-eslint/eslint-plugin": "~7.17.0",
|
|
72
|
+
"@typescript-eslint/parser": "~7.17.0",
|
|
73
|
+
"eslint": "^8.56.0",
|
|
74
|
+
"eslint-config-prettier": "^9.1.0",
|
|
75
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
76
|
+
"mustache": "^4.2.0",
|
|
77
|
+
"prettier": "~3.3.3",
|
|
78
|
+
"prettier-plugin-java": "~2.6.4",
|
|
79
|
+
"rimraf": "^3.0.2",
|
|
80
|
+
"semantic-release": "^24.0.0",
|
|
81
|
+
"swiftlint": "^1.0.2",
|
|
82
|
+
"typescript": "~5.4.5",
|
|
83
|
+
"vite": "^5.2.11",
|
|
84
|
+
"vite-plugin-dts": "^4.4.0"
|
|
85
|
+
},
|
|
86
|
+
"peerDependencies": {
|
|
87
|
+
"@capacitor/core": "^7.0.0"
|
|
88
|
+
},
|
|
89
|
+
"overrides": {
|
|
90
|
+
"vite": {
|
|
91
|
+
"rollup": "npm:@rollup/wasm-node"
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
"capacitor": {
|
|
95
|
+
"ios": {
|
|
96
|
+
"src": "ios"
|
|
97
|
+
},
|
|
98
|
+
"android": {
|
|
99
|
+
"src": "android"
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|