@microsoft/omnichannel-chat-sdk
Version:
Microsoft Omnichannel Chat SDK
395 lines • 20.4 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["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 (g && (g = 0, op[0] && (_ = 0)), _) 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 });
exports.AMSViewScanStatus = void 0;
var AMSFileScanner_1 = require("./AMSFileScanner");
var FilePermission;
(function (FilePermission) {
FilePermission[FilePermission["READ"] = 0] = "READ";
FilePermission[FilePermission["WRITE"] = 1] = "WRITE";
})(FilePermission || (FilePermission = {}));
var AMSFileManagerEvent;
(function (AMSFileManagerEvent) {
AMSFileManagerEvent["AMSUpload"] = "AMSUpload";
AMSFileManagerEvent["AMSDownload"] = "AMSDownload";
AMSFileManagerEvent["GetFileIds"] = "GetFileIds";
AMSFileManagerEvent["CreateFileIdProperty"] = "CreateFileIdProperty";
AMSFileManagerEvent["GetFileMetadata"] = "GetFileMetadata";
AMSFileManagerEvent["CreateFileMetadataProperty"] = "CreateFileMetadataProperty";
})(AMSFileManagerEvent || (AMSFileManagerEvent = {}));
var AMSViewScanStatus;
(function (AMSViewScanStatus) {
AMSViewScanStatus["PASSED"] = "passed";
AMSViewScanStatus["MALWARE"] = "malware";
AMSViewScanStatus["INPROGRESS"] = "in progress";
})(AMSViewScanStatus || (exports.AMSViewScanStatus = AMSViewScanStatus = {}));
var supportedImagesMimeTypes = ["image/jpeg", "image/png", "image/gif", "image/heic", "image/webp"];
var AMSFileManager = /** @class */ (function () {
function AMSFileManager(amsClient, logger, options) {
if (logger === void 0) { logger = null; }
if (options === void 0) { options = {}; }
var _a;
this.supportedImagesMimeTypes = supportedImagesMimeTypes;
this.logger = logger;
this.amsClient = amsClient;
this.options = options;
this.omnichannelChatToken = null;
if (((_a = this.options.fileScan) === null || _a === void 0 ? void 0 : _a.disabled) === false) {
var options_1 = __assign({}, this.options.fileScan);
this.fileScanner = new AMSFileScanner_1.default(this.amsClient, options_1);
}
}
AMSFileManager.prototype.uploadFiles = function (files) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, Promise.all(files.map(function (file) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/, this.uploadFileToAMS(file)];
}); }); }))];
});
});
};
AMSFileManager.prototype.downloadFiles = function (files) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, Promise.all(files.map(function (file) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/, this.downloadFileFromAMS(file)];
}); }); }))];
});
});
};
AMSFileManager.prototype.updatePermissions = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, undefined];
});
});
};
AMSFileManager.prototype.getFileIds = function (metadata) {
var _a, _b, _c;
if (!metadata) {
return;
}
if (!metadata.amsReferences && !metadata.amsreferences) {
return;
}
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.startScenario(AMSFileManagerEvent.GetFileIds);
try {
var result = undefined;
if (metadata === null || metadata === void 0 ? void 0 : metadata.amsReferences) {
result = JSON.parse(metadata === null || metadata === void 0 ? void 0 : metadata.amsReferences);
}
if (metadata === null || metadata === void 0 ? void 0 : metadata.amsreferences) {
result = JSON.parse(metadata === null || metadata === void 0 ? void 0 : metadata.amsreferences);
}
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.completeScenario(AMSFileManagerEvent.GetFileIds);
return result;
}
catch (error) {
var exceptionDetails = {
metadata: "".concat(metadata),
errorObject: "".concat(error)
};
(_c = this.logger) === null || _c === void 0 ? void 0 : _c.failScenario(AMSFileManagerEvent.GetFileIds, {
ExceptionDetails: JSON.stringify(exceptionDetails)
});
return undefined;
}
};
/**
* Creates property for the reference of the attachments to be sent to ACS as metadata after successful upload.
*
* @param fileIds List of fileIds
* @returns
*/
AMSFileManager.prototype.createFileIdProperty = function (fileIds) {
var _a, _b, _c;
if (!fileIds) {
return;
}
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.startScenario(AMSFileManagerEvent.CreateFileIdProperty);
try {
var result = {
amsReferences: JSON.stringify(fileIds),
amsreferences: JSON.stringify(fileIds)
};
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.completeScenario(AMSFileManagerEvent.CreateFileIdProperty);
return result;
}
catch (error) {
var exceptionDetails = {
fileIds: "".concat(fileIds),
errorObject: "".concat(error)
};
(_c = this.logger) === null || _c === void 0 ? void 0 : _c.failScenario(AMSFileManagerEvent.CreateFileIdProperty, {
ExceptionDetails: JSON.stringify(exceptionDetails)
});
return undefined;
}
};
AMSFileManager.prototype.getFileMetadata = function (metadata) {
var _a, _b, _c;
if (!metadata || !metadata.amsMetadata) {
return;
}
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.startScenario(AMSFileManagerEvent.GetFileMetadata);
try {
var result = JSON.parse(metadata === null || metadata === void 0 ? void 0 : metadata.amsMetadata);
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.completeScenario(AMSFileManagerEvent.GetFileMetadata);
return result;
}
catch (error) {
var exceptionDetails = {
metadata: "".concat(metadata),
errorObject: "".concat(error)
};
(_c = this.logger) === null || _c === void 0 ? void 0 : _c.failScenario(AMSFileManagerEvent.GetFileMetadata, {
ExceptionDetails: JSON.stringify(exceptionDetails)
});
return undefined;
}
};
/**
*
* Creates property for the metadata of the attachments to be sent to ACS as metadata after successful upload.
*
* @param metadata List of file metadata
* @returns
*/
AMSFileManager.prototype.createFileMetadataProperty = function (metadata) {
var _a, _b, _c;
if (!metadata) {
return;
}
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.startScenario(AMSFileManagerEvent.CreateFileMetadataProperty);
try {
var result = {
amsMetadata: JSON.stringify(metadata)
};
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.completeScenario(AMSFileManagerEvent.CreateFileMetadataProperty);
return result;
}
catch (error) {
var exceptionDetails = {
metadata: "".concat(metadata),
errorObject: "".concat(error)
};
(_c = this.logger) === null || _c === void 0 ? void 0 : _c.failScenario(AMSFileManagerEvent.CreateFileMetadataProperty, {
ExceptionDetails: JSON.stringify(exceptionDetails)
});
return undefined;
}
};
/**
* Creates content to be sent to ACS after successful upload.
*
* @param metadata List of file metadata
* @returns
*/
AMSFileManager.prototype.createBotAttachment = function (metadata) {
if (!metadata || Object.keys(metadata).length === 0) {
return null;
}
// Sending empty content
return null;
};
AMSFileManager.prototype.uploadFileToAMS = function (fileToUpload) {
return __awaiter(this, void 0, void 0, function () {
var blob, error_1, exceptionDetails, file, response, error_2, exceptionDetails, error_3, exceptionDetails;
var _a, _b, _c, _d, _e;
return __generator(this, function (_f) {
switch (_f.label) {
case 0:
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.startScenario(AMSFileManagerEvent.AMSUpload);
if (!(fileToUpload.contentUrl && fileToUpload.name)) return [3 /*break*/, 12];
blob = void 0;
_f.label = 1;
case 1:
_f.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.amsClient.fetchBlob(fileToUpload.contentUrl)];
case 2:
blob = _f.sent();
return [3 /*break*/, 4];
case 3:
error_1 = _f.sent();
exceptionDetails = {
response: 'AMSFetchBlobFailure',
errorObject: "".concat(error_1)
};
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.failScenario(AMSFileManagerEvent.AMSUpload, {
ExceptionDetails: JSON.stringify(exceptionDetails)
});
throw error_1;
case 4:
file = new File([blob], fileToUpload.name, { type: fileToUpload.contentType });
response = void 0;
_f.label = 5;
case 5:
_f.trys.push([5, 7, , 8]);
return [4 /*yield*/, this.amsClient.createObject(this.amsClient.chatToken.chatId, file, this.omnichannelChatToken, this.supportedImagesMimeTypes)];
case 6:
response = _f.sent(); // eslint-disable-line @typescript-eslint/no-explicit-any
return [3 /*break*/, 8];
case 7:
error_2 = _f.sent();
exceptionDetails = {
response: 'AMSCreateObjectFailure',
errorObject: "".concat(error_2)
};
(_c = this.logger) === null || _c === void 0 ? void 0 : _c.failScenario(AMSFileManagerEvent.AMSUpload, {
ExceptionDetails: JSON.stringify(exceptionDetails)
});
throw error_2;
case 8:
_f.trys.push([8, 10, , 11]);
return [4 /*yield*/, this.amsClient.uploadDocument(response.id, file, this.omnichannelChatToken, this.supportedImagesMimeTypes)];
case 9:
_f.sent();
return [3 /*break*/, 11];
case 10:
error_3 = _f.sent();
exceptionDetails = {
response: 'AMSUploadDocumentFailure',
errorObject: "".concat(error_3)
};
(_d = this.logger) === null || _d === void 0 ? void 0 : _d.failScenario(AMSFileManagerEvent.AMSUpload, {
ExceptionDetails: JSON.stringify(exceptionDetails)
});
throw error_3;
case 11:
(_e = this.logger) === null || _e === void 0 ? void 0 : _e.completeScenario(AMSFileManagerEvent.AMSUpload);
return [2 /*return*/, {
fileId: response.id,
metadata: {
contentType: fileToUpload.contentType,
fileName: fileToUpload.name
}
}];
case 12: return [2 /*return*/];
}
});
});
};
AMSFileManager.prototype.downloadFileFromAMS = function (uploadedFile) {
return __awaiter(this, void 0, void 0, function () {
var fileMetadata, response, error_4, exceptionDetails, blob, view_location, scan, file_1, exceptionDetails, error_5, exceptionDetails, file;
var _a, _b, _c, _d, _e, _f;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.startScenario(AMSFileManagerEvent.AMSDownload);
if (!(uploadedFile.fileId && uploadedFile.metadata && uploadedFile.metadata.fileName)) return [3 /*break*/, 9];
fileMetadata = {
id: uploadedFile.fileId,
type: uploadedFile.metadata.contentType,
name: uploadedFile.metadata.fileName
};
response = void 0;
_g.label = 1;
case 1:
_g.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.amsClient.getViewStatus(fileMetadata, this.omnichannelChatToken, this.supportedImagesMimeTypes)];
case 2:
response = _g.sent(); // eslint-disable-line @typescript-eslint/no-explicit-any
return [3 /*break*/, 4];
case 3:
error_4 = _g.sent();
exceptionDetails = {
response: 'AMSGetViewStatusFailure',
errorObject: "".concat(error_4)
};
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.failScenario(AMSFileManagerEvent.AMSDownload, {
ExceptionDetails: JSON.stringify(exceptionDetails)
});
return [2 /*return*/, undefined];
case 4:
blob = void 0;
view_location = response.view_location, scan = response.scan;
if (((_c = this.options.fileScan) === null || _c === void 0 ? void 0 : _c.disabled) === false && scan && (scan === null || scan === void 0 ? void 0 : scan.status) !== AMSViewScanStatus.PASSED) {
file_1 = new File([blob], uploadedFile.metadata.fileName, { type: uploadedFile.metadata.contentType });
exceptionDetails = {
response: "InvalidFileScanResult"
};
this.fileScanner.addOrUpdateFile(fileMetadata.id, fileMetadata, scan);
(_d = this.logger) === null || _d === void 0 ? void 0 : _d.failScenario(AMSFileManagerEvent.AMSDownload, {
ExceptionDetails: JSON.stringify(exceptionDetails)
});
return [2 /*return*/, file_1];
}
_g.label = 5;
case 5:
_g.trys.push([5, 7, , 8]);
return [4 /*yield*/, this.amsClient.getView(fileMetadata, view_location, this.omnichannelChatToken, this.supportedImagesMimeTypes)];
case 6:
blob = _g.sent(); // eslint-disable-line @typescript-eslint/no-explicit-any
return [3 /*break*/, 8];
case 7:
error_5 = _g.sent();
exceptionDetails = {
response: 'AMSGetViewFailure',
errorObject: "".concat(error_5)
};
(_e = this.logger) === null || _e === void 0 ? void 0 : _e.failScenario(AMSFileManagerEvent.AMSDownload, {
ExceptionDetails: JSON.stringify(exceptionDetails)
});
return [2 /*return*/, undefined];
case 8:
file = new File([blob], uploadedFile.metadata.fileName, { type: uploadedFile.metadata.contentType });
(_f = this.logger) === null || _f === void 0 ? void 0 : _f.completeScenario(AMSFileManagerEvent.AMSDownload);
return [2 /*return*/, file];
case 9: return [2 /*return*/];
}
});
});
};
return AMSFileManager;
}());
exports.default = AMSFileManager;
//# sourceMappingURL=AMSFileManager.js.map