UNPKG

@platform/cell.client

Version:

A strongly typed HTTP client for operating with a CellOS service end-point.

164 lines (163 loc) 10.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var common_1 = require("../common"); function upload(args) { return tslib_1.__awaiter(this, void 0, void 0, function () { var http, urls, cellUri, input, sendChanges, changes, addChanges, cellUrls, url, uploadStartBody, res1, type, message, uploadStart, uploadUrls, fileUploadWait, res2, fileUploadSuccesses, fileUploadFails, fileUploadErrors, res3, fileCompleteFails, fileCompleteFailErrors, cellUploadCompleteBody, res4, cellUploadComplete, files, errors, status, responseBody; var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: http = args.http, urls = args.urls, cellUri = args.cellUri; input = Array.isArray(args.input) ? args.input : [args.input]; sendChanges = common_1.defaultValue(args.changes, false); addChanges = function (input) { if (sendChanges && input && input.length > 0) { changes = changes || []; changes = tslib_1.__spreadArrays(changes, input); } }; cellUrls = urls.cell(cellUri); url = { start: cellUrls.files.upload.query({ changes: sendChanges }).toString(), complete: cellUrls.files.uploaded.query({ changes: sendChanges }).toString(), }; uploadStartBody = { expires: undefined, files: input.map(function (_a) { var filename = _a.filename, data = _a.data, mimetype = _a.mimetype; var filehash = common_1.Schema.hash.sha256(data); return { filename: filename, filehash: filehash, mimetype: mimetype }; }), }; return [4, http.post(url.start, uploadStartBody)]; case 1: res1 = _a.sent(); if (!res1.ok) { type = common_1.ERROR.HTTP.SERVER; message = "Failed during initial file-upload step to '" + cellUri + "'."; return [2, common_1.util.toError(res1.status, type, message)]; } uploadStart = res1.json; addChanges(uploadStart.data.changes); uploadUrls = uploadStart.urls.uploads; fileUploadWait = uploadUrls .map(function (upload) { var file = input.find(function (item) { return item.filename === upload.filename; }); var data = file ? file.data : undefined; return { data: data, upload: upload }; }) .filter(function (_a) { var data = _a.data; return Boolean(data); }) .map(function (_a) { var upload = _a.upload, data = _a.data; return tslib_1.__awaiter(_this, void 0, void 0, function () { var url, uploadToS3, uploadToLocal, isLocal, res, ok, status, uri, filename, expiresAt; var _this = this; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: url = upload.url; uploadToS3 = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var props, contentType, form, headers; return tslib_1.__generator(this, function (_a) { props = upload.props; contentType = props['content-type']; form = new common_1.FormData(); Object.keys(props) .map(function (key) { return ({ key: key, value: props[key] }); }) .forEach(function (_a) { var key = _a.key, value = _a.value; return form.append(key, value); }); form.append('file', data, { contentType: contentType }); headers = form.getHeaders(); return [2, http.post(url, form, { headers: headers })]; }); }); }; uploadToLocal = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var path, headers; return tslib_1.__generator(this, function (_a) { path = upload.props.key; headers = { 'content-type': 'multipart/form-data', path: path }; return [2, http.post(url, data, { headers: headers })]; }); }); }; isLocal = url.startsWith('http://localhost'); return [4, (isLocal ? uploadToLocal() : uploadToS3())]; case 1: res = _b.sent(); ok = res.ok, status = res.status; uri = upload.uri, filename = upload.filename, expiresAt = upload.expiresAt; return [2, { ok: ok, status: status, uri: uri, filename: filename, expiresAt: expiresAt }]; } }); }); }); return [4, Promise.all(fileUploadWait)]; case 2: res2 = _a.sent(); fileUploadSuccesses = res2.filter(function (item) { return item.ok; }); fileUploadFails = res2.filter(function (item) { return !item.ok; }); fileUploadErrors = fileUploadFails.map(function (item) { var filename = item.filename; var message = "Failed while uploading file '" + filename + "'"; var error = { type: 'FILE/upload', filename: filename, message: message }; return error; }); return [4, Promise.all(fileUploadSuccesses.map(function (item) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var url, filename, body, res, status, ok, json, file; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: url = urls .file(item.uri) .uploaded.query({ changes: sendChanges }) .toString(); filename = item.filename; body = { filename: filename }; return [4, http.post(url, body)]; case 1: res = _a.sent(); status = res.status, ok = res.ok; json = res.json; file = json.data; return [2, { status: status, ok: ok, json: json, file: file, filename: filename }]; } }); }); }))]; case 3: res3 = _a.sent(); res3.forEach(function (res) { return addChanges(res.json.changes); }); fileCompleteFails = res3.filter(function (res) { return !res.ok; }); fileCompleteFailErrors = fileCompleteFails.map(function (res) { var filename = res.filename || 'UNKNOWN'; var message = "Failed while completing upload of file '" + filename + "'"; var error = { type: 'FILE/upload', filename: filename, message: message }; return error; }); cellUploadCompleteBody = {}; return [4, http.post(url.complete, cellUploadCompleteBody)]; case 4: res4 = _a.sent(); cellUploadComplete = res4.json; files = cellUploadComplete.data.files; addChanges(cellUploadComplete.data.changes); errors = tslib_1.__spreadArrays(uploadStart.data.errors, fileUploadErrors, fileCompleteFailErrors); status = errors.length === 0 ? 200 : 500; responseBody = { uri: cellUri, cell: cellUploadComplete.data.cell, files: files, errors: errors, changes: changes, }; return [2, common_1.util.toClientResponse(status, responseBody)]; } }); }); } exports.upload = upload;