-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.ts
169 lines (157 loc) · 4.81 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { NativeModules } from "react-native";
const { RNScopedStorage } = NativeModules;
export type FileType = {
/**Document Tree Uri for the file or directory*/
uri: string;
/** Name of the file or directory*/
name: string;
/**Storage path for the file*/
path: string;
type: "file" | "directory";
/** Last modified date of the file or directory */
lastModified: number;
/** data read from the file */
data: string;
/** mime type of the file */
mime: string;
};
/**
* Open the Document Picker to select a folder. Read/Write Permission will be granted to the selected folder.
*/
export async function openDocumentTree(persist = false): Promise<FileType> {
return await RNScopedStorage.openDocumentTree(persist);
}
/**
* Open Document picker to create a file at the user specified location.
* @param fileName Name of the file to create.
* @param mime mime of the file to create. eg image/jpeg
* @param data Data to write to the file once it is created.
* @param encoding Encoding of the dat you are writing.
*/
export async function createDocument(
fileName: string,
mime: string,
data: string,
encoding?: "utf8" | "base64" | "ascii"
): Promise<FileType> {
return await RNScopedStorage.createDocument(
fileName,
mime,
data,
encoding || "utf8"
);
}
/**
* Open Document picker for the user to select a file.
* @param readData Do you want to read data from the user specified file?
* @param encoding Encoding for the file you are reading.
*/
export async function openDocument(
readData = false,
encoding?: "utf8" | "base64" | "ascii"
): Promise<FileType> {
return RNScopedStorage.openDocument(readData, encoding || "utf8");
}
/**
* There is a limit to the number of uri permissions your app can persist. Get a list of all the persisted document tree uris so you are remove the ones you are not using or perform other operations.
*/
export async function getPersistedUriPermissions(): Promise<string[]> {
return RNScopedStorage.getPersistedUriPermissions();
}
/**
* Remove a uri from persisted uri list.
* @param uri The uri you want to remove from persisted uri permissions.
*/
export async function releasePersistableUriPermission(uri: string) {
RNScopedStorage.releasePersistableUriPermission(uri);
}
/**
* List all files and folders in a directory uri
* @param uri Path to a directory.
*/
export async function listFiles(uri: string): Promise<FileType[]> {
return await RNScopedStorage.listFiles(uri);
}
/**
* Read file at a given path. The path of the file must be a document tree uri.
* @param uri Path to the file you want to read.
* @param encoding Encoding for the file you are reading.
*/
export async function readFile(
uri: string,
encoding?: "utf8" | "base64" | "ascii"
): Promise<string> {
return await RNScopedStorage.readFile(uri, encoding || "utf8");
}
/**
* Rename a file or directory at the given path.
* @param uri Path to the file or directory to rename
* @param name New name for the file or directory
*/
export async function rename(uri: string, name: string): Promise<string> {
return await RNScopedStorage.rename(uri, name);
}
/**
* Delete a file or directory at the given path.
* @param uri Path to the file or directory to delete
*/
export async function deleteFile(uri: string): Promise<boolean> {
return await RNScopedStorage.delete(uri);
}
/**
* Create a directory at the given path.
* @param path Uri of the parent directory
* @param dirName Name of the new directory
*/
export async function createDirectory(
uri: string,
dirName: string
): Promise<FileType> {
return await RNScopedStorage.createDirectory(uri, dirName);
}
/**
* Write to a file at the give directory. If the file does not exist, it will be created.
* @param path Uri of the directory
* @param fileName Name of the file
* @param mime Mime of the file. eg image/jpeg
* @param data Data you want to write
* @param encoding Encoding of the data you are writing.
* @param append Should the data be appended to the existing data in file?
*/
export async function writeFile(
uri: string,
fileName: string,
mime: string,
data: string,
encoding?: "utf8" | "base64" | "ascii",
append = false
): Promise<string> {
return await RNScopedStorage.writeFile(
uri,
fileName,
mime,
data,
encoding || "utf8",
append
);
}
/**
* Create a new file at the given directory.
* @param path Uri of the parent directory
* @param fileName Name of the new file.
* @param mime Mime type of the file, e.g. image/jpeg
*/
export async function createFile(
uri: string,
fileName: string,
mime: string
): Promise<FileType> {
return await RNScopedStorage.createFile(uri, fileName, mime);
}
/**
* Get details for a file/directory at a given uri.
* @param path Uri of the parent directory
*/
export async function stat(uri: string) {
return await RNScopedStorage.stat(uri);
}