forked from RobinBobin/react-native-google-drive-api-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFiles.js
137 lines (106 loc) · 3.86 KB
/
Files.js
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
import RNFS from "react-native-fs";
import utf8 from "utf8";
import { StaticUtils } from "simple-common-utils";
import GDrive from "./GDrive";
const uploadUrl = "https://www.googleapis.com/upload/drive/v3/files";
export default class Files {
static mimeFolder = "application/vnd.google-apps.folder";
constructor(params = {}) {
this.params = params;
[
[ "boundary", "foo_bar_baz" ]
].forEach(nameValue => this.params[nameValue[0]] =
this.params[nameValue[0]] || nameValue[1]);
}
createFileMultipart(media, mediaType, metadata, isBase64) {
const ddb = `--${this.params.boundary}`;
const ending = `\n${ddb}--`;
let body = `\n${ddb}\n` +
`Content-Type: ${GDrive._contentTypeJson}\n\n` +
`${JSON.stringify(metadata)}\n\n${ddb}\n` +
(isBase64 ? "Content-Transfer-Encoding: base64\n" : '') +
`Content-Type: ${mediaType}\n\n`;
if (media.constructor == String) {
body += `${media}${ending}`;
} else {
body = new Uint8Array(
StaticUtils.encodedUtf8ToByteArray(utf8.encode(body))
.concat(media)
.concat(StaticUtils.encodedUtf8ToByteArray(utf8.encode(ending))));
}
return fetch(
`${uploadUrl}?uploadType=multipart`, {
method: "POST",
headers: GDrive._createHeaders(
`multipart/related; boundary=${this.params.boundary}`,
body.length
),
body
});
}
delete(fileId) {
return fetch(`${GDrive._urlFiles}/${fileId}`, {
method: "DELETE",
headers: GDrive._createHeaders()
});
}
async safeCreateFolder(metadata) {
let id = await this.getId(metadata.name, metadata.parents, Files.mimeFolder);
if (!id) {
metadata.mimeType = Files.mimeFolder;
const body = JSON.stringify(metadata);
result = await fetch(GDrive._urlFiles, {
method: "POST",
headers: GDrive._createHeaders(
GDrive._contentTypeJson,
body.length),
body
});
if (!result.ok) {
throw result;
}
id = (await result.json()).id;
}
return id;
}
async getId(name, parents, mimeType, trashed = false) {
const queryParams = {name, trashed};
if (mimeType) {
queryParams.mimeType = mimeType;
}
let result = await this.list({
q: GDrive._stringifyQueryParams(queryParams, "",
" and ", true) + ` and '${parents[0]}' in parents`
});
if (!result.ok) {
throw result;
}
const file = (await result.json()).files[0];
return file ? file.id : file;
}
get(fileId, queryParams) {
const parameters = GDrive._stringifyQueryParams(queryParams);
return fetch(`${GDrive._urlFiles}/${fileId}${parameters}`, {
headers: GDrive._createHeaders()
});
}
download(fileId, downloadFileOptions, queryParams = {}) {
queryParams.alt = "media";
const parameters = GDrive._stringifyQueryParams(queryParams);
downloadFileOptions.fromUrl = `${GDrive._urlFiles}/${fileId}${parameters}`;
downloadFileOptions.headers = Object.assign({
"Authorization": `Bearer ${GDrive.accessToken}`
}, downloadFileOptions.headers);
return RNFS.downloadFile(downloadFileOptions);
}
list(queryParams) {
return fetch(`${GDrive._urlFiles}${GDrive._stringifyQueryParams(queryParams)}`, {
headers: GDrive._createHeaders()
});
}
export(fileId, mimeType) {
return fetch(`${GDrive._urlFiles}/${fileId}/export?mimeType=${mimeType}`, {
headers: GDrive._createHeaders()
});
}
}