-
Notifications
You must be signed in to change notification settings - Fork 17
/
resumableupload2_js.js
228 lines (221 loc) · 7.18 KB
/
resumableupload2_js.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* ResumableUploadToGoogleDrive for Javascript library
* GitHub https://github.com/tanaikech/ResumableUploadForGoogleDrive2_js<br>
* In this Class ResumableUploadToGoogleDrive2, the selected file is uploaded by splitting data on the disk. By this, the large file can be uploaded.<br>
*/
(function (r) {
let ResumableUploadToGoogleDrive2;
ResumableUploadToGoogleDrive2 = (function () {
function ResumableUploadToGoogleDrive2() {
this.obj = {};
this.chunkSize = 52428800;
this.endpoint =
"https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&supportsAllDrives=true";
}
/**
* Run resumable upload a file.
* @param {Object} resource the object for resumable uploading a file.
*/
ResumableUploadToGoogleDrive2.prototype.Do = async function (
resource,
callback
) {
callback({ status: "initialize" }, null);
try {
this.obj = await init.call(this, resource);
} catch (err) {
callback(null, err);
return;
}
try {
const head = await getLocation.call(this);
this.location = head.get("location");
callback({ status: "getLocation" }, null);
callback({ status: "start" }, null);
const fileSize = resource.file.size;
const len = Math.ceil(fileSize / this.chunkSize);
for (let i = 0; i < len; i++) {
let start = i * this.chunkSize;
let end =
fileSize < start + this.chunkSize
? fileSize
: start + this.chunkSize;
let data = resource.file.slice(start, end);
end -= 1;
callback(
{
status: "Uploading",
progressNumber: { current: i, end: len },
progressByte: {
current: start,
end: end,
total: fileSize,
},
},
null
);
try {
const res = await getFile.call(this, {
fileSize,
len,
start,
end,
data,
i,
});
if (
res.status == "Next" ||
(res.status == "Done" && i == len - 1)
) {
callback(res, null);
} else {
callback(null, "Internal error.");
return;
}
} catch (err) {
callback(null, err);
return;
}
}
} catch (err) {
callback(null, err);
return;
}
};
const init = function (resource) {
return new Promise((resolve, reject) => {
if (!("accessToken" in resource) || !("file" in resource)) {
reject({
Error:
"There are no required parameters. accessToken, fileName, fileSize, fileType and fileBuffer are required.",
});
return;
}
let object = {};
object.resource = resource;
if (
"chunkSize" in resource &&
resource.chunkSize >= 262144 &&
resource.chunkSize % 262144 == 0
) {
this.chunkSize = resource.chunkSize;
}
if ("fields" in resource && resource.fields != "") {
this.endpoint += "&fields=" + encodeURIComponent(resource.fields);
}
if ("convertToGoogleDocs" in resource && resource.convertToGoogleDocs) {
fetch(
"https://www.googleapis.com/drive/v3/about?fields=importFormats",
{
method: "GET",
headers: { Authorization: "Bearer " + resource.accessToken },
}
)
.then((res) => {
if (res.status != 200) {
res.json().then((e) => reject(e));
return;
}
res.json().then((res) => {
if (resource.file.type in res.importFormats) {
object.resource.fileType =
res.importFormats[resource.fileType][0];
}
resolve(object);
});
})
.catch((err) => {
reject(err);
});
} else {
resolve(object);
}
});
};
const getLocation = function () {
return new Promise((resolve, reject) => {
const resource = this.obj.resource;
const accessToken = resource.accessToken;
let metadata = {
mimeType: resource.file.type,
name: resource.file.name,
};
if ("folderId" in resource && resource.folderId != "") {
metadata.parents = [resource.folderId];
}
fetch(this.endpoint, {
method: "POST",
body: JSON.stringify(metadata),
headers: {
Authorization: "Bearer " + accessToken,
"Content-Type": "application/json",
},
})
.then((res) => {
if (res.status != 200) {
res.json().then((e) => reject(e));
return;
}
resolve(res.headers);
})
.catch((err) => {
reject(err);
});
});
};
const getFile = function ({ fileSize, len, start, end, data, i }) {
const location = this.location;
return new Promise(function (resolve, reject) {
const fr = new FileReader();
fr.onload = async function () {
const buf = fr.result;
const obj = {
data: new Uint8Array(buf),
length: end - start + 1,
range: "bytes " + start + "-" + end + "/" + fileSize,
startByte: start,
endByte: end,
total: fileSize,
cnt: i,
totalChunkNumber: len,
};
await doUpload(obj, location)
.then((res) => resolve(res))
.catch((err) => reject(err));
};
fr.readAsArrayBuffer(data);
});
};
const doUpload = function (e, url) {
return new Promise(function (resolve, reject) {
fetch(url, {
method: "PUT",
body: e.data,
headers: { "Content-Range": e.range },
})
.then((res) => {
const status = res.status;
if (status == 308) {
resolve({ status: "Next", result: r });
} else if (status == 200) {
res.json().then((r) => resolve({ status: "Done", result: r }));
} else {
res.json().then((err) => {
err.additionalInformation =
"When the file size is large, there is the case that the file cannot be converted to Google Docs. Please be careful this.";
reject(err);
return;
});
return;
}
})
.catch((err) => {
reject(err);
return;
});
});
};
return ResumableUploadToGoogleDrive2;
})();
return (r.ResumableUploadToGoogleDrive2 = ResumableUploadToGoogleDrive2);
})(this);