@onekeyfe/react-native-cloud-fs 2.6.0
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/.github/workflows/package-publish.yml +19 -0
- package/README.md +146 -0
- package/android/build.gradle +55 -0
- package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +6 -0
- package/android/gradle.properties +16 -0
- package/android/gradlew +160 -0
- package/android/gradlew.bat +90 -0
- package/android/src/main/AndroidManifest.xml +6 -0
- package/android/src/main/java/org/rncloudfs/DriveServiceHelper.java +214 -0
- package/android/src/main/java/org/rncloudfs/RNCloudFsModule.java +508 -0
- package/android/src/main/java/org/rncloudfs/RNCloudFsPackage.java +28 -0
- package/docs/getting-started.md +61 -0
- package/docs/todo.md +34 -0
- package/docs/xcode.png +0 -0
- package/index.js +7 -0
- package/ios/RNCloudFs.h +10 -0
- package/ios/RNCloudFs.m +467 -0
- package/ios/RNCloudFs.xcodeproj/project.pbxproj +252 -0
- package/ios/RNCloudFs.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
- package/licesnse.txt +21 -0
- package/package.json +14 -0
- package/react-native-cloud-fs.podspec +17 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
package org.rncloudfs;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Copyright 2018 Google LLC
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import android.content.ContentResolver;
|
|
20
|
+
import android.content.Intent;
|
|
21
|
+
import android.database.Cursor;
|
|
22
|
+
import android.net.Uri;
|
|
23
|
+
import android.provider.OpenableColumns;
|
|
24
|
+
import android.util.Log;
|
|
25
|
+
|
|
26
|
+
import androidx.core.util.Pair;
|
|
27
|
+
|
|
28
|
+
import com.google.android.gms.tasks.Task;
|
|
29
|
+
import com.google.android.gms.tasks.Tasks;
|
|
30
|
+
|
|
31
|
+
import com.google.api.client.http.FileContent;
|
|
32
|
+
import com.google.api.services.drive.Drive;
|
|
33
|
+
import com.google.api.services.drive.model.File;
|
|
34
|
+
import com.google.api.services.drive.model.FileList;
|
|
35
|
+
import java.io.BufferedReader;
|
|
36
|
+
import java.io.IOException;
|
|
37
|
+
import java.io.InputStream;
|
|
38
|
+
import java.io.InputStreamReader;
|
|
39
|
+
import java.util.Collections;
|
|
40
|
+
import java.util.List;
|
|
41
|
+
import java.util.concurrent.ExecutionException;
|
|
42
|
+
import java.util.concurrent.Executor;
|
|
43
|
+
import java.util.concurrent.Executors;
|
|
44
|
+
import java.util.concurrent.atomic.AtomicReference;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A utility for performing read/write operations on Drive files via the REST API and opening a
|
|
48
|
+
* file picker UI via Storage Access Framework.
|
|
49
|
+
*/
|
|
50
|
+
public class DriveServiceHelper {
|
|
51
|
+
private final Executor mExecutor = Executors.newSingleThreadExecutor();
|
|
52
|
+
private final Drive mDriveService;
|
|
53
|
+
|
|
54
|
+
public DriveServiceHelper(Drive driveService) {
|
|
55
|
+
mDriveService = driveService;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public Task<String> saveFile(String sourcePath, String destinationPath, String mimeType, Boolean useDocumentsFolder) throws ExecutionException, InterruptedException {
|
|
59
|
+
String existingFileId = null;
|
|
60
|
+
FileList fileList = Tasks.await(queryFiles(useDocumentsFolder));
|
|
61
|
+
for (File file : fileList.getFiles()) {
|
|
62
|
+
if(file.getName().equalsIgnoreCase(destinationPath)){
|
|
63
|
+
existingFileId = file.getId();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return createFile(sourcePath, destinationPath, mimeType, useDocumentsFolder, existingFileId);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Creates a text file in the user's My Drive folder and returns its file ID.
|
|
71
|
+
*/
|
|
72
|
+
public Task<String> createFile(String sourcePath, String destinationPath, String mimeType, Boolean useDocumentsFolder, String fileId) {
|
|
73
|
+
|
|
74
|
+
return Tasks.call(mExecutor, () -> {
|
|
75
|
+
try{
|
|
76
|
+
java.io.File sourceFile = new java.io.File(sourcePath);
|
|
77
|
+
FileContent mediaContent = new FileContent(mimeType, sourceFile);
|
|
78
|
+
List<String> parentFolder = Collections.singletonList(useDocumentsFolder ? "root" : "appDataFolder");
|
|
79
|
+
File metadata = new File()
|
|
80
|
+
.setMimeType(mimeType)
|
|
81
|
+
.setName(destinationPath);
|
|
82
|
+
if(fileId == null){
|
|
83
|
+
metadata.setParents(parentFolder);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
File googleFile = null;
|
|
87
|
+
if(fileId != null){
|
|
88
|
+
googleFile = mDriveService.files().update(fileId, metadata, mediaContent).execute();
|
|
89
|
+
} else{
|
|
90
|
+
googleFile = mDriveService.files().create(metadata, mediaContent).execute();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (googleFile == null) {
|
|
94
|
+
throw new IOException("Null result when requesting file creation.");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return googleFile.getId();
|
|
98
|
+
} catch(Exception e){
|
|
99
|
+
Log.e("WTF", e.toString());
|
|
100
|
+
throw e;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
public Task<Boolean> checkIfFileExists(String fileId) {
|
|
108
|
+
return Tasks.call(mExecutor, () -> {
|
|
109
|
+
// Retrieve the metadata as a File object.
|
|
110
|
+
File metadata = mDriveService.files().get(fileId).execute();
|
|
111
|
+
if(metadata != null){
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
public Task<Boolean> deleteFile(String fileId) {
|
|
119
|
+
return Tasks.call(mExecutor, () -> {
|
|
120
|
+
// Retrieve the metadata as a File object.
|
|
121
|
+
mDriveService.files().delete(fileId).execute();
|
|
122
|
+
return true;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Opens the file identified by {@code fileId} and returns a {@link Pair} of its name and
|
|
128
|
+
* contents.
|
|
129
|
+
*/
|
|
130
|
+
public Task<String> readFile(String fileId) {
|
|
131
|
+
return Tasks.call(mExecutor, () -> {
|
|
132
|
+
// Retrieve the metadata as a File object.
|
|
133
|
+
File metadata = mDriveService.files().get(fileId).execute();
|
|
134
|
+
String name = metadata.getName();
|
|
135
|
+
|
|
136
|
+
// Stream the file contents to a String.
|
|
137
|
+
try (InputStream is = mDriveService.files().get(fileId).executeMediaAsInputStream();
|
|
138
|
+
BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
|
|
139
|
+
StringBuilder stringBuilder = new StringBuilder();
|
|
140
|
+
String line;
|
|
141
|
+
|
|
142
|
+
while ((line = reader.readLine()) != null) {
|
|
143
|
+
stringBuilder.append(line);
|
|
144
|
+
}
|
|
145
|
+
String contents = stringBuilder.toString();
|
|
146
|
+
|
|
147
|
+
return contents;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Returns a {@link FileList} containing all the visible files in the user's My Drive.
|
|
154
|
+
*
|
|
155
|
+
* <p>The returned list will only contain files visible to this app, i.e. those which were
|
|
156
|
+
* created by this app. To perform operations on files not created by the app, the project must
|
|
157
|
+
* request Drive Full Scope in the <a href="https://play.google.com/apps/publish">Google
|
|
158
|
+
* Developer's Console</a> and be submitted to Google for verification.</p>
|
|
159
|
+
*/
|
|
160
|
+
public Task<FileList> queryFiles(Boolean useDocumentsFolder) {
|
|
161
|
+
return Tasks.call(mExecutor, () ->
|
|
162
|
+
mDriveService.files().list().
|
|
163
|
+
setSpaces(useDocumentsFolder ? "drive" : "appDataFolder")
|
|
164
|
+
.setFields("nextPageToken, files(id, name, modifiedTime)")
|
|
165
|
+
.setPageSize(100)
|
|
166
|
+
.execute()
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Returns an {@link Intent} for opening the Storage Access Framework file picker.
|
|
172
|
+
*/
|
|
173
|
+
public Intent createFilePickerIntent() {
|
|
174
|
+
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
|
175
|
+
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
176
|
+
intent.setType("text/plain");
|
|
177
|
+
|
|
178
|
+
return intent;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Opens the file at the {@code uri} returned by a Storage Access Framework {@link Intent}
|
|
183
|
+
* created by {@link #createFilePickerIntent()} using the given {@code contentResolver}.
|
|
184
|
+
*/
|
|
185
|
+
public Task<Pair<String, String>> openFileUsingStorageAccessFramework(
|
|
186
|
+
ContentResolver contentResolver, Uri uri) {
|
|
187
|
+
return Tasks.call(mExecutor, () -> {
|
|
188
|
+
// Retrieve the document's display name from its metadata.
|
|
189
|
+
String name;
|
|
190
|
+
try (Cursor cursor = contentResolver.query(uri, null, null, null, null)) {
|
|
191
|
+
if (cursor != null && cursor.moveToFirst()) {
|
|
192
|
+
int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
|
|
193
|
+
name = cursor.getString(nameIndex);
|
|
194
|
+
} else {
|
|
195
|
+
throw new IOException("Empty cursor returned for file.");
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Read the document's contents as a String.
|
|
200
|
+
String content;
|
|
201
|
+
try (InputStream is = contentResolver.openInputStream(uri);
|
|
202
|
+
BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
|
|
203
|
+
StringBuilder stringBuilder = new StringBuilder();
|
|
204
|
+
String line;
|
|
205
|
+
while ((line = reader.readLine()) != null) {
|
|
206
|
+
stringBuilder.append(line);
|
|
207
|
+
}
|
|
208
|
+
content = stringBuilder.toString();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return Pair.create(name, content);
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
}
|