| 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 |
1×
1×
1×
1×
1×
1×
1×
| 'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (_ref) {
var client = _ref.client,
filterQuery = _ref.filterQuery,
mustContain = _ref.mustContain,
busy = _ref.busy,
encodeQueryAsString = _ref.encodeQueryAsString,
progress = _ref.progress;
function uploadChunk(uploadId, offset, chunk) {
return new Promise(function (resolve, reject) {
var data = new FormData();
data.append('uploadId', uploadId);
data.append('offset', offset);
data.append('chunk', chunk);
var xhr = new XMLHttpRequest();
function extractResponse(ctx) {
return {
ctx: ctx,
data: JSON.parse(xhr.responseText),
status: xhr.status,
statusText: xhr.statusText,
headers: {},
config: {}
};
}
xhr.addEventListener('progress', function (event) {
if (event.lengthComputable) {
var complete = event.loaded / event.total;
console.log('chunk progress', complete);
}
});
xhr.addEventListener('load', function (event) {
resolve(extractResponse('load'));
});
xhr.addEventListener('error', function (event) {
console.log('Transfer as failed', event);
reject(extractResponse('error'));
});
xhr.addEventListener('abort', function (event) {
console.log('Transfer as been canceled', event);
reject(extractResponse('abort'));
});
xhr.open('POST', client.baseURL + '/file/chunk', true);
xhr.responseType = 'text';
xhr.setRequestHeader('Accept', 'application/json, text/plain, */*');
xhr.setRequestHeader('Girder-Token', client.token);
xhr.send(data);
});
}
function uploadFileToItem(params, file) {
var fileId = file.name + '_' + file.lastModified;
// upload file to item
return new Promise(function (resolve, reject) {
busy(client._.post('/file' + encodeQueryAsString(params))).then(function (upload) {
var chunkSize = 10 * 1024 * 1024;
// i = 0,
// chunks = Math.floor(file.size / chunkSize);
var uploadNextChunk = function uploadNextChunk(offset) {
var blob = void 0;
progress(fileId, offset, file.size);
if (offset + chunkSize >= file.size) {
blob = file.slice(offset);
uploadChunk(upload.data._id, offset, blob).then(function (uploadResp) {
progress(fileId, file.size, file.size);
resolve(uploadResp);
}).catch(function (error) {
console.warn('could not upload final chunk');
console.warn(error);
reject(error);
});
} else {
blob = file.slice(offset, offset + chunkSize);
uploadChunk(upload.data._id, offset, blob).then(function (uploadResp) {
// var msg = '';
// i += 1;
// msg += `chunk ${i} of ${chunks} uploaded`;
uploadNextChunk(offset + chunkSize);
}).catch(function (error) {
console.warn('could not upload chunk');
console.warn(error);
reject(error);
});
}
};
uploadNextChunk(0);
}).catch(function (error) {
console.warn('Could not upload file');
console.warn(error);
reject(error);
});
});
}
return {
uploadChunk: uploadChunk,
uploadFileToItem: uploadFileToItem,
getFile: function getFile(id) {
return busy(client._.get('/file/' + id));
},
getUploadOffset: function getUploadOffset(id) {
return busy(client._.get('/file/offset', { params: { uploadId: id } }));
},
downloadFile: function downloadFile(id, offset, endByte, contentDisposition) {
var params = { offset: offset, endByte: endByte, contentDisposition: contentDisposition };
Object.keys(params).forEach(function (key) {
if (params[key] === null || typeof params[key] === 'undefined') {
delete params[key];
}
});
return busy(client._.get('/file/' + id + '/download' + encodeQueryAsString(params)));
},
updateFileContent: function updateFileContent(id, size) {
return busy(client._.put('/file/' + id + '/contents?size=' + size));
},
deleteFile: function deleteFile(id) {
return busy(client._.delete('/file/' + id));
},
editFile: function editFile(file) {
var expected = ['name', 'mimeType'];
var params = filterQuery.apply(undefined, [file].concat(expected));
var _mustContain = mustContain(file, '_id'),
missingKeys = _mustContain.missingKeys,
promise = _mustContain.promise;
return missingKeys ? promise : busy(client._.put('/file/' + file._id + encodeQueryAsString(params)));
},
copyFile: function copyFile(fileId, itemId) {
return busy(client._.post('/file/' + fileId + '/copy?itemId=' + itemId));
},
moveFilesOffline: function moveFilesOffline(files) {
return busy(client._.put('/file/move', files));
},
newFile: function newFile(file) {
var expected = ['parentType', 'parentId', 'name', 'size', 'mimeType', 'linkUrl'];
var params = filterQuery.apply(undefined, [file].concat(expected));
var _mustContain2 = mustContain(file, 'parentType', 'parentId', 'name'),
missingKeys = _mustContain2.missingKeys,
promise = _mustContain2.promise;
return missingKeys ? promise : busy(client._.post('/file' + encodeQueryAsString(params)));
}
};
};
|