forked from RobinBobin/react-native-google-drive-api-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFiles.js
More file actions
152 lines (121 loc) · 4.19 KB
/
Copy pathFiles.js
File metadata and controls
152 lines (121 loc) · 4.19 KB
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
import utf8 from "utf8";
import {
StaticUtils,
ArrayStringifier
} from "simple-common-utils";
import * as FileSystem from 'expo-file-system';
import GDrive from "./GDrive";
const uploadUrl = "https://www.googleapis.com/upload/drive/v3/files";
function _stringifyQueryParams(queryParams,
prefix = "?", separator = "&", quoteIfString)
{
const array = [];
Object.keys(queryParams).forEach(key => array.push(
`${key}=${StaticUtils.safeQuoteIfString(queryParams[key], quoteIfString)}`));
return new ArrayStringifier(array)
.setPrefix(prefix)
.setSeparator(separator)
.process();
}
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: _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 = _stringifyQueryParams(queryParams);
return fetch(`${GDrive._urlFiles}/${fileId}${parameters}`, {
headers: GDrive._createHeaders()
});
}
download(fileId, queryParams = {}, fileType="file") {
queryParams.alt = "media";
const parameters = _stringifyQueryParams(queryParams);
let url = `${GDrive._urlFiles}/${fileId}${parameters}`;
return FileSystem.downloadAsync(url, FileSystem.documentDirectory + 'temp.' + fileType, {
headers: {
"Authorization": `Bearer ${GDrive.accessToken}`
}
});
}
list(queryParams) {
return fetch(`${GDrive._urlFiles}${_stringifyQueryParams(queryParams)}`, {
headers: GDrive._createHeaders()
});
}
export(fileId, mimeType) {
return fetch(`${GDrive._urlFiles}/${fileId}/export?mimeType=${mimeType}`, {
headers: GDrive._createHeaders()
});
}
}