Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ozone-api-upload

Package Overview
Dependencies
Maintainers
6
Versions
145
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ozone-api-upload - npm Package Compare versions

Comparing version 5.0.4 to 5.1.0

545

dist/ozone-api-upload.js

@@ -10,6 +10,33 @@ "use strict";

};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
const ozone_config_1 = require("ozone-config");
const ozone_api_request_1 = require("ozone-api-request");
const uuid_1 = require("uuid");
var ozone_config_1 = require("ozone-config");
var ozone_api_request_1 = require("ozone-api-request");
var uuid_1 = require("uuid");
/**

@@ -36,8 +63,8 @@ * UploadFileRequest is a JavaScrip class that can be use as an

*/
class UploadFileRequest {
constructor() {
var UploadFileRequest = /** @class */ (function () {
function UploadFileRequest() {
/**
* XMLHttpRequest.onreadystatechange event handler
*/
this.onreadystatechange = () => { };
this.onreadystatechange = function () { };
/**

@@ -58,4 +85,4 @@ * set the interval to verify ozone has finish the element processing

this.upload = {
onprogress: () => { },
onloadstart: () => { },
onprogress: function () { },
onloadstart: function () { },
};

@@ -68,28 +95,40 @@ try {

}
callOneadystatechange() {
UploadFileRequest.prototype.callOneadystatechange = function () {
if (typeof (this.onreadystatechange) == 'function') {
this.onreadystatechange();
}
}
};
Object.defineProperty(UploadFileRequest.prototype, "readyState", {
/**
* XMLHttpRequest.readyState
* @type {number}
*/
get: function () {
return this._internalReadyState;
},
enumerable: true,
configurable: true
});
Object.defineProperty(UploadFileRequest.prototype, "_readyState", {
set: function (readyState) {
this._internalReadyState = readyState;
this.callOneadystatechange();
},
enumerable: true,
configurable: true
});
Object.defineProperty(UploadFileRequest.prototype, "mediaId", {
/**
* Accessor to uploaded media id.
* default value is null
*
* @return {string | null}
*/
get: function () {
return this._mediaId;
},
enumerable: true,
configurable: true
});
/**
* XMLHttpRequest.readyState
* @type {number}
*/
get readyState() {
return this._internalReadyState;
}
set _readyState(readyState) {
this._internalReadyState = readyState;
this.callOneadystatechange();
}
/**
* Accessor to uploaded media id.
* default value is null
*
* @return {string | null}
*/
get mediaId() {
return this._mediaId;
}
/**
* like an XMLHttpRequest.open()

@@ -104,5 +143,6 @@ * Parameters passed are not used.

*/
open(method, url, async = true) {
UploadFileRequest.prototype.open = function (method, url, async) {
if (async === void 0) { async = true; }
this._readyState = 1;
}
};
/**

@@ -114,7 +154,14 @@ * like an XMLHttpRequest.send()

*/
send(formData) {
return __awaiter(this, void 0, void 0, function* () {
yield (this.uploadFile(formData));
UploadFileRequest.prototype.send = function (formData) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (this.uploadFile(formData))];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
};
/**

@@ -124,3 +171,3 @@ * like XMLHttpRequest.abort()

*/
abort() {
UploadFileRequest.prototype.abort = function () {
this.isAbort = true;

@@ -130,3 +177,3 @@ if (this.currentRequest) {

}
}
};
/**

@@ -137,5 +184,5 @@ * unused.

*/
setRequestHeader(key, value) {
}
_createRequest() {
UploadFileRequest.prototype.setRequestHeader = function (key, value) {
};
UploadFileRequest.prototype._createRequest = function () {
if (this.isAbort) {

@@ -146,17 +193,27 @@ throw new Error('request abort');

return this.currentRequest;
}
_buildUrl(service, ...param) {
return __awaiter(this, void 0, void 0, function* () {
const otherUrlParam = param || [];
const config = yield ozone_config_1.OzoneConfig.get();
return [
config.host.replace(/\/$/, ''),
config.endPoints[service]
.replace(/\/$/, '')
.replace(/^\//, ''),
...otherUrlParam
]
.join('/');
};
UploadFileRequest.prototype._buildUrl = function (service) {
var param = [];
for (var _i = 1; _i < arguments.length; _i++) {
param[_i - 1] = arguments[_i];
}
return __awaiter(this, void 0, void 0, function () {
var otherUrlParam, config;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
otherUrlParam = param || [];
return [4 /*yield*/, ozone_config_1.OzoneConfig.get()];
case 1:
config = _a.sent();
return [2 /*return*/, [
config.host.replace(/\/$/, ''),
config.endPoints[service]
.replace(/\/$/, '')
.replace(/^\//, '')
].concat(otherUrlParam).join('/')];
}
});
});
}
};
/**

@@ -168,22 +225,24 @@ * alias to send method.

*/
uploadFile(file, folderId = '0') {
UploadFileRequest.prototype.uploadFile = function (file, folderId) {
var _this = this;
if (folderId === void 0) { folderId = '0'; }
return this._startUploadSession(file, folderId)
.then((result) => this._getUploadId(result))
.then((result) => this._performUpload(result))
.then((result) => this._endUploadSession(result))
.then((result) => this._waitForTask(result))
.then((mediaId) => {
this.status = 200;
this._readyState = 4;
this._mediaId = mediaId;
if (this.eventTarget)
this.eventTarget.dispatchEvent(new CustomEvent('ozone-upload-completed', { bubbles: true, detail: { mediaId } }));
.then(function (result) { return _this._getUploadId(result); })
.then(function (result) { return _this._performUpload(result); })
.then(function (result) { return _this._endUploadSession(result); })
.then(function (result) { return _this._waitForTask(result); })
.then(function (mediaId) {
_this.status = 200;
_this._readyState = 4;
_this._mediaId = mediaId;
if (_this.eventTarget)
_this.eventTarget.dispatchEvent(new CustomEvent('ozone-upload-completed', { bubbles: true, detail: { mediaId: mediaId } }));
return mediaId;
}).catch((error) => {
this.status = 555;
this._readyState = 4;
}).catch(function (error) {
_this.status = 555;
_this._readyState = 4;
console.error(error.message);
return null;
});
}
};
/**

@@ -195,21 +254,24 @@ * alias to send method.

*/
importOrganisation(file, parentTenantId) {
UploadFileRequest.prototype.importOrganisation = function (file, parentTenantId) {
var _this = this;
return this._uploadOrganisation(parentTenantId, file)
.then((result) => {
.then(function (result) {
return result.response;
})
.then((uploadFileId) => this._waitForTask({ uploadFileId }))
.then(() => {
this.status = 200;
this._readyState = 4;
.then(function (uploadFileId) {
return _this._waitForTask({ uploadFileId: uploadFileId });
})
.then(function () {
_this.status = 200;
_this._readyState = 4;
return null;
}).catch((error) => {
this.status = 555;
this._readyState = 4;
}).catch(function (error) {
_this.status = 555;
_this._readyState = 4;
console.error(error.message);
throw error;
});
}
notifyOnError() {
const self = this;
};
UploadFileRequest.prototype.notifyOnError = function () {
var self = this;
return function (ev) {

@@ -223,92 +285,130 @@ if (this.status === 0

};
}
_startUploadSession(file, folderId) {
return __awaiter(this, void 0, void 0, function* () {
const request = this._createRequest();
request.url = yield this._buildUrl('uploadStart');
request.method = 'POST';
request.onreadystatechange = this.notifyOnError();
//TODO understand need of folderId??
const numeric_id = parseInt('0x' + folderId.split('-')[4]);
const config = yield ozone_config_1.OzoneConfig.get();
const body = {
mediaUploadChannelIdentifier: config.uploadChannel,
autoCommit: false,
mediaMetadatas: [{
"type": {
"type": "PROPERTY",
"identifier": "org.taktik.metadata.folderId"
},
"valueObject": numeric_id.toString()
}]
};
request.body = JSON.stringify(body);
return request.sendRequest()
.then((xhr) => {
const response = xhr.response;
return {
file: file,
sessionId: response.result
};
};
UploadFileRequest.prototype._startUploadSession = function (file, folderId) {
return __awaiter(this, void 0, void 0, function () {
var request, _a, numeric_id, config, body;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
request = this._createRequest();
_a = request;
return [4 /*yield*/, this._buildUrl('uploadStart')];
case 1:
_a.url = _b.sent();
request.method = 'POST';
request.onreadystatechange = this.notifyOnError();
numeric_id = parseInt('0x' + folderId.split('-')[4]);
return [4 /*yield*/, ozone_config_1.OzoneConfig.get()];
case 2:
config = _b.sent();
body = {
mediaUploadChannelIdentifier: config.uploadChannel,
autoCommit: false,
mediaMetadatas: [{
"type": {
"type": "PROPERTY",
"identifier": "org.taktik.metadata.folderId"
},
"valueObject": numeric_id.toString()
}]
};
request.body = JSON.stringify(body);
return [2 /*return*/, request.sendRequest()
.then(function (xhr) {
var response = xhr.response;
return {
file: file,
sessionId: response.result
};
})];
}
});
});
}
_getUploadId(data) {
return __awaiter(this, void 0, void 0, function* () {
const request = this._createRequest();
request.url = yield this._buildUrl('uploadId', data.sessionId);
request.method = 'GET';
request.onreadystatechange = this.notifyOnError();
return request.sendRequest()
.then((xhr) => {
const response = xhr.response;
const resultInfo = data;
resultInfo.uploadId = response.result;
resultInfo.folderId = response.folderId;
return resultInfo;
};
UploadFileRequest.prototype._getUploadId = function (data) {
return __awaiter(this, void 0, void 0, function () {
var request, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
request = this._createRequest();
_a = request;
return [4 /*yield*/, this._buildUrl('uploadId', data.sessionId)];
case 1:
_a.url = _b.sent();
request.method = 'GET';
request.onreadystatechange = this.notifyOnError();
return [2 /*return*/, request.sendRequest()
.then(function (xhr) {
var response = xhr.response;
var resultInfo = data;
resultInfo.uploadId = response.result;
resultInfo.folderId = response.folderId;
return resultInfo;
})];
}
});
});
}
_performUpload(data) {
return __awaiter(this, void 0, void 0, function* () {
const request = this._createRequest();
request.url = yield this._buildUrl('upload', data.uploadId);
request.method = 'POST';
request.onreadystatechange = this.notifyOnError();
const xhr = request.createXMLHttpRequest(false);
xhr.upload.onprogress = this.upload.onprogress;
request.body = data.file;
return request.sendRequest(xhr)
.then(() => {
return data;
};
UploadFileRequest.prototype._performUpload = function (data) {
return __awaiter(this, void 0, void 0, function () {
var request, _a, xhr;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
request = this._createRequest();
_a = request;
return [4 /*yield*/, this._buildUrl('upload', data.uploadId)];
case 1:
_a.url = _b.sent();
request.method = 'POST';
request.onreadystatechange = this.notifyOnError();
xhr = request.createXMLHttpRequest(false);
xhr.upload.onprogress = this.upload.onprogress;
request.body = data.file;
return [2 /*return*/, request.sendRequest(xhr)
.then(function () {
return data;
})];
}
});
});
}
_endUploadSession(data) {
return __awaiter(this, void 0, void 0, function* () {
const request = this._createRequest();
request.url = yield this._buildUrl('uploadComplete', data.sessionId);
request.method = 'POST';
request.onreadystatechange = this.notifyOnError();
const info = {
'selectedFileFieldNames': [['files']],
mediaMetadatas: [
{ type: { type: 'PROPERTY', identifier: 'org.taktik.metadata.folderId' }, valueObject: data.folderId }
]
};
request.body = JSON.stringify(info);
return request.sendRequest()
.then((xhr) => {
const response = data;
response.uploadFileId = xhr.response.file;
return response;
};
UploadFileRequest.prototype._endUploadSession = function (data) {
return __awaiter(this, void 0, void 0, function () {
var request, _a, info;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
request = this._createRequest();
_a = request;
return [4 /*yield*/, this._buildUrl('uploadComplete', data.sessionId)];
case 1:
_a.url = _b.sent();
request.method = 'POST';
request.onreadystatechange = this.notifyOnError();
info = {
'selectedFileFieldNames': [['files']],
mediaMetadatas: [
{ type: { type: 'PROPERTY', identifier: 'org.taktik.metadata.folderId' }, valueObject: data.folderId }
]
};
request.body = JSON.stringify(info);
return [2 /*return*/, request.sendRequest()
.then(function (xhr) {
var response = data;
response.uploadFileId = xhr.response.file;
return response;
})];
}
});
});
}
_waitForSubTasks(asyncTasksGroupId) {
let mediaId;
return (new Promise((resolve, reject) => {
let interval = setInterval(() => {
this._awaitTask(asyncTasksGroupId)
.then((data) => {
};
UploadFileRequest.prototype._waitForSubTasks = function (asyncTasksGroupId) {
var _this = this;
var mediaId;
return (new Promise(function (resolve, reject) {
var interval = setInterval(function () {
_this._awaitTask(asyncTasksGroupId)
.then(function (data) {
if (data.stepsCount === data.stepsDone) {

@@ -321,16 +421,17 @@ clearInterval(interval);

})
.catch((error) => {
.catch(function (error) {
clearInterval(interval);
reject(error);
});
}, this.pollInterval);
}, _this.pollInterval);
}));
}
_waitForTask(uploadEndResult) {
let mediaId;
return (new Promise((resolve, reject) => {
let interval = setInterval(() => {
this._awaitTask(uploadEndResult.uploadFileId)
.then((data) => {
const taskExecution = data.taskExecutions[uploadEndResult.uploadFileId];
};
UploadFileRequest.prototype._waitForTask = function (uploadEndResult) {
var _this = this;
var mediaId;
return (new Promise(function (resolve, reject) {
var interval = setInterval(function () {
_this._awaitTask(uploadEndResult.uploadFileId)
.then(function (data) {
var taskExecution = data.taskExecutions[uploadEndResult.uploadFileId];
if (taskExecution.isComplete ||

@@ -344,48 +445,65 @@ taskExecution.completed) {

})
.catch((error) => {
.catch(function (error) {
clearInterval(interval);
reject(error);
});
}, this.pollInterval);
}, _this.pollInterval);
}))
.then((data) => {
const taskResult = data || {};
.then(function (data) {
var taskResult = data || {};
if (taskResult.asyncTasksGroupId) {
return this._waitForSubTasks(taskResult.asyncTasksGroupId);
return _this._waitForSubTasks(taskResult.asyncTasksGroupId);
}
})
.then(() => mediaId);
}
_awaitTask(uploadFileId) {
return __awaiter(this, void 0, void 0, function* () {
const request = this._createRequest();
request.url = yield this._buildUrl('wait', uploadFileId, '120');
request.method = 'GET';
request.onreadystatechange = this.notifyOnError();
return request.sendRequest()
.then((xhr) => {
return xhr.response;
.then(function () { return mediaId; });
};
UploadFileRequest.prototype._awaitTask = function (uploadFileId) {
return __awaiter(this, void 0, void 0, function () {
var request, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
request = this._createRequest();
_a = request;
return [4 /*yield*/, this._buildUrl('wait', uploadFileId, '120')];
case 1:
_a.url = _b.sent();
request.method = 'GET';
request.onreadystatechange = this.notifyOnError();
return [2 /*return*/, request.sendRequest()
.then(function (xhr) {
return xhr.response;
})];
}
});
});
}
_uploadOrganisation(parentTenantId, file) {
return __awaiter(this, void 0, void 0, function* () {
const ozoneRequest = new ozone_api_request_1.OzoneAPIRequest();
const queryParams = {
tenantsPrefix: uuid_1.v4(),
rootTenantId: parentTenantId
};
const query = this.buildSearchParam(queryParams);
const url = yield this._buildUrl('import', `upload?${query}`);
ozoneRequest.method = 'POST';
ozoneRequest.url = url;
ozoneRequest.onreadystatechange = this.notifyOnError();
const xmlhttp = ozoneRequest.createXMLHttpRequest(false);
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.setRequestHeader('Accept', 'application/json');
xmlhttp.upload.onprogress = this.upload.onprogress;
ozoneRequest.body = file;
return ozoneRequest.sendRequest(xmlhttp);
};
UploadFileRequest.prototype._uploadOrganisation = function (parentTenantId, file) {
return __awaiter(this, void 0, void 0, function () {
var ozoneRequest, queryParams, query, url, xmlhttp;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
ozoneRequest = new ozone_api_request_1.OzoneAPIRequest();
queryParams = {
tenantsPrefix: uuid_1.v4(),
rootTenantId: parentTenantId
};
query = this.buildSearchParam(queryParams);
return [4 /*yield*/, this._buildUrl('import', "upload?" + query)];
case 1:
url = _a.sent();
ozoneRequest.method = 'POST';
ozoneRequest.url = url;
ozoneRequest.onreadystatechange = this.notifyOnError();
xmlhttp = ozoneRequest.createXMLHttpRequest(false);
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.setRequestHeader('Accept', 'application/json');
xmlhttp.upload.onprogress = this.upload.onprogress;
ozoneRequest.body = file;
return [2 /*return*/, ozoneRequest.sendRequest(xmlhttp)];
}
});
});
}
};
/**

@@ -404,6 +522,7 @@ * This function build url query parameters

*/
buildSearchParam(queryParams) {
UploadFileRequest.prototype.buildSearchParam = function (queryParams) {
return new URLSearchParams(queryParams).toString();
}
}
};
return UploadFileRequest;
}());
exports.UploadFileRequest = UploadFileRequest;
{
"name": "ozone-api-upload",
"version": "5.0.4",
"version": "5.1.0",
"publishConfig": {

@@ -27,8 +27,8 @@ "access": "public"

"dependencies": {
"ozone-api-request": "^5.0.0",
"ozone-config": "^5.0.4",
"ozone-api-request": "^5.1.0",
"ozone-config": "^5.1.0",
"uuid": "^3.3.2"
},
"repository": "https://github.com/taktik/ozone-components/packages/ozone-api/ozone-api-upload",
"gitHead": "57f782bb30accea48224b9e11d83a9ea29fb8d0a"
"gitHead": "d11ca259166381d398811f4a84a7d859b0b19202"
}
{
"compilerOptions": {
"target": "es6",
"target": "es5",
"module": "commonjs", // Use esnext to enable lazy loading

@@ -28,2 +28,2 @@ "moduleResolution": "node",

]
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc