UNPKG

dynamicsmobile

Version:

Allows development of off-line mobile and web business apps over the Dynamics Mobile platform. More info on https://www.dynamicsmobile.com

310 lines 14.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BackendFileService = void 0; const tslib_1 = require("tslib"); const files_service_base_1 = require("../lib-core/files-service-base"); const injectable_1 = require("../ioc/injectable"); const application_context_service_1 = require("../lib-core/application-context-service"); const dms_api_wrapper_1 = require("../platform-web/dms-api-wrapper"); const path = tslib_1.__importStar(require("path")); const dms_platform_bridge_factory_1 = require("../platform/dms-platform-bridge-factory"); let BackendFileService = exports.BackendFileService = class BackendFileService extends files_service_base_1.FileService { constructor(dms) { super(dms); } getDirFiles(folder) { return tslib_1.__awaiter(this, void 0, void 0, function* () { let { actualFileName, actualFilePath } = this.validateFilePath(folder); const auth = dms_api_wrapper_1.DmsAPIWrapper.getAuthroizationToken(); const appArea = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getApparea", null); try { var response = yield dms_api_wrapper_1.DmsAPIWrapper.executeApiRequest(appArea, "/storage/file?root=" + encodeURIComponent(folder), "GET", auth); if (response && response.data) { if (typeof response.data === "string") response = JSON.parse(response.data); else response = response.data; return response; } else { this.processApiError('No response from server'); } } catch (err) { this.processApiError(err); } }); } fileWrite(fileName, fileContent, contentType) { return tslib_1.__awaiter(this, void 0, void 0, function* () { let { actualFileName, actualFilePath } = this.validateFilePath(fileName); if (!fileContent) throw new Error(`fileWrite requires not-empty fileContent!`); const ext = path.extname(actualFileName); let mimeType = contentType; if (!mimeType) { switch (ext) { case '.png': mimeType = 'image/png'; break; case '.jpg': case '.jpeg': mimeType = 'image/jpeg'; break; case '.bmp': mimeType = 'image/bmp'; break; case '.txt': case '.text': mimeType = 'image/text'; break; case '.xml': mimeType = 'application/xml'; break; case '.json': mimeType = 'application/json'; break; case '.csv': mimeType = 'text/csv'; break; case '.css': mimeType = 'text/css'; break; case '.html': mimeType = 'text/html'; break; case '.js': mimeType = 'text/javascript'; break; case '.rar': mimeType = 'application/vnd.rar'; break; case '.zip': mimeType = 'text/zip'; break; case '.woff': mimeType = 'font/woff'; break; case '.woff2': mimeType = 'font/woff2'; break; case '.otf': mimeType = 'font/otf'; break; case '.svg': mimeType = 'image/svg+xml'; break; case '.tiff': case '.tif': mimeType = 'image/tiff'; break; case '.pdf': mimeType = 'application/pdf'; break; case '.doc': mimeType = 'application/msword'; break; case '.docx': mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; break; case '.xls': mimeType = 'application/vnd.ms-excel'; break; case '.xlsx': mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; break; case '.ppt': mimeType = 'application/vnd.ms-powerpoint'; break; case '.pptx': mimeType = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; break; case '.avi': mimeType = 'video/x-msvideo'; break; case '.mpeg': mimeType = 'video/mpeg'; break; case '.ico': mimeType = 'image/ico'; break; case '.mp3': mimeType = 'audio/mpeg'; break; case '.wav': mimeType = 'audio/wav'; break; default: mimeType = 'application/octet-stream'; } } const appArea = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getApparea", null); const auth = dms_api_wrapper_1.DmsAPIWrapper.getAuthroizationToken(); try { var response = yield dms_api_wrapper_1.DmsAPIWrapper.executeApiRequest(appArea, "/storage/file/" + encodeURIComponent(actualFileName) + "?root=" + encodeURIComponent(actualFilePath), "POST", auth, mimeType); if (response && response.data) { if (typeof response.data === "string") response = JSON.parse(response.data); else response = response.data; if (!response.signedUrl) { throw new Error('Can not save file'); } yield this.dms.app.request.promise({ headers: { 'Content-Type': mimeType //appArea: appArea }, processData: false, method: "PUT", url: response.signedUrl, data: fileContent }); } else { throw new Error('Now response from server'); //this.processApiError('No response from server'); } } catch (err) { throw new Error(err); //this.processApiError(err); } }); } validateFilePath(fileName) { let actualFileName = null; let actualFilePath = null; if (fileName.indexOf('/') >= 0) { const valid = /^(cdn:\/\/)?([A-Za-z]:|[A-Za-z0-9_-\s]+(\.[A-Za-z0-9_-\s]+)*)((\/[A-Za-z0-9_.-\s]+)+)$/.test(fileName); if (!valid) throw new Error(`Invalid fileName format for FileService API '${fileName}'!`); actualFileName = path.basename(fileName); actualFilePath = path.dirname(fileName); } else { const valid = /^([_a-zA-Z0-9\.-/s]*)$/.test(fileName); if (!valid) throw new Error(`Invalid path format for fileWrite '${fileName}'!`); actualFileName = fileName; actualFilePath = '/'; } return { actualFileName, actualFilePath }; } fileRead(fileName, options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { let { actualFileName, actualFilePath } = this.validateFilePath(fileName); const appArea = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getApparea", null); const auth = dms_api_wrapper_1.DmsAPIWrapper.getAuthroizationToken(); try { var response = yield dms_api_wrapper_1.DmsAPIWrapper.executeApiRequest(appArea, "/storage/file/" + encodeURIComponent(actualFileName) + "?root=" + encodeURIComponent(actualFilePath), 'GET', auth); if (response && response.data) { if (typeof response.data === "string") response = JSON.parse(response.data); else response = response.data; if (!response.url) { throw new Error('Can not read file'); } const readResponse = yield this.dms.app.request.promise({ method: "GET", url: response.url, headers: { "Accept-Language": "*" }, //dataType: 'text' mimeType: options && options.mimeType ? options.mimeType : "text/plain;" }); return readResponse.data ? readResponse.data : readResponse; } else { throw new Error('No response from server'); } } catch (err) { throw new Error(err); } }); } fileDelete(fileName) { return tslib_1.__awaiter(this, void 0, void 0, function* () { let { actualFileName, actualFilePath } = this.validateFilePath(fileName); const auth = dms_api_wrapper_1.DmsAPIWrapper.getAuthroizationToken(); const appArea = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getApparea", null); try { var response = yield dms_api_wrapper_1.DmsAPIWrapper.executeApiRequest(appArea, "/storage/file/" + encodeURIComponent(actualFileName) + "?root=" + encodeURIComponent(actualFilePath), 'DELETE', auth); if (response && response.data) { if (typeof response.data === "string") response = JSON.parse(response.data); else response = response.data; return response; } else { this.processApiError('No response from server'); } } catch (err) { this.processApiError(err); } }); } fileExist(fileName) { return tslib_1.__awaiter(this, void 0, void 0, function* () { let { actualFileName, actualFilePath } = this.validateFilePath(fileName); const appArea = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getApparea", null); const auth = dms_api_wrapper_1.DmsAPIWrapper.getAuthroizationToken(); try { var response = yield dms_api_wrapper_1.DmsAPIWrapper.executeApiRequest(appArea, "/storage/file/" + encodeURIComponent(actualFileName) + "?root=" + encodeURIComponent(actualFilePath), 'GET', auth); if (response && response.data) { if (typeof response.data === "string") response = JSON.parse(response.data); else response = response.data; if (response.url) { return true; } else { return false; } } else { throw new Error('No response from server'); } } catch (err) { throw new Error(err); } }); } getFileNativeUri(fileName) { return tslib_1.__awaiter(this, void 0, void 0, function* () { let { actualFileName, actualFilePath } = this.validateFilePath(fileName); const appArea = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getApparea", null); const auth = dms_api_wrapper_1.DmsAPIWrapper.getAuthroizationToken(); try { var response = yield dms_api_wrapper_1.DmsAPIWrapper.executeApiRequest(appArea, "/storage/file/" + encodeURIComponent(actualFileName) + "?root=" + encodeURIComponent(actualFilePath), 'GET', auth); if (response && response.data) { if (typeof response.data === "string") response = JSON.parse(response.data); else response = response.data; if (response.url) { return response.url; } else { throw new Error('Can not get file native url: ' + response.message); } } else { throw new Error('No response from server'); } } catch (err) { throw new Error(err); } }); } }; exports.BackendFileService = BackendFileService = tslib_1.__decorate([ (0, injectable_1.Injectable)(), tslib_1.__metadata("design:paramtypes", [application_context_service_1.DmsApplicationService]) ], BackendFileService); //# sourceMappingURL=backend-files-service.js.map