UNPKG

@bytedance/mona-client-web

Version:

web for mona

167 lines 9.22 kB
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 }; } }; var getTokenHeaders = function () { return __awaiter(void 0, void 0, void 0, function () { var appId, token; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: appId = (_a = window.__MONA_LIGHT_APP_LIFE_CYCLE_LANUCH_QUERY) === null || _a === void 0 ? void 0 : _a.appId; if (!(window.__MONA_LIGHT_APP_GET_TOEKN && appId)) return [3 /*break*/, 2]; return [4 /*yield*/, window.__MONA_LIGHT_APP_GET_TOEKN()]; case 1: token = _b.sent(); return [2 /*return*/, { 'x-open-token': token, 'x-use-test': window.__MONA_LIGHT_USE_TEST, 'x-open-compass': window.__MONA_LIGHT_APP_GET_COMPASS_TOKEN ? window.__MONA_LIGHT_APP_GET_COMPASS_TOKEN() : '', }]; case 2: return [2 /*return*/, {}]; } }); }); }; var pipeResponse = function (response) { return __awaiter(void 0, void 0, void 0, function () { var respData, BizError, code; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!response.ok) { return [2 /*return*/, Promise.reject(new Error("".concat(response.status, " ").concat(response.statusText, " \n ").concat((_a = response.url) !== null && _a !== void 0 ? _a : '', " ")))]; } return [4 /*yield*/, response.json()]; case 1: respData = _b.sent(); BizError = respData === null || respData === void 0 ? void 0 : respData.BizError; code = (BizError || {}).code; if (!code) { return [2 /*return*/, respData]; } else { return [2 /*return*/, Promise.reject(new Error(BizError === null || BizError === void 0 ? void 0 : BizError.message))]; } return [2 /*return*/]; } }); }); }; export function getDownLoadFileUrl(fileKey) { return __awaiter(this, void 0, void 0, function () { var domain, url, appId, headers, data; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: domain = window.__MONA_LIGNT_APP_DOMAIN_NAME || 'lgw.jinritemai.com'; url = "https://".concat(domain, "/light/download/file/url"); appId = (_a = window.__MONA_LIGHT_APP_LIFE_CYCLE_LANUCH_QUERY) === null || _a === void 0 ? void 0 : _a.appId; if (!(appId && fileKey)) return [3 /*break*/, 3]; return [4 /*yield*/, getTokenHeaders()]; case 1: headers = _b.sent(); return [4 /*yield*/, fetch("".concat(url, "?appId=").concat(appId, "&fileKey=").concat(fileKey), { headers: headers, credentials: 'include', }).then(pipeResponse)]; case 2: data = _b.sent(); return [2 /*return*/, data === null || data === void 0 ? void 0 : data.url]; case 3: return [2 /*return*/]; } }); }); } export function uploadFileTemporary(file) { return __awaiter(this, void 0, void 0, function () { var crc32Res, _a, _b, domain, appId, filePath, index, ext, url, headers, data, crc32, buffer, crc32Res, headerAuthorization, headerHost, url_1, headers_1, uploadData, resultUrl; var _c, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: if (!window.LIGHT_UPLOAD) return [3 /*break*/, 2]; _b = (_a = require('./crc32.js')).crc32; return [4 /*yield*/, file.arrayBuffer()]; case 1: crc32Res = _b.apply(_a, [_e.sent()]); return [2 /*return*/, window.LIGHT_UPLOAD(file, crc32Res)]; case 2: domain = window.__MONA_LIGNT_APP_DOMAIN_NAME || 'lgw.jinritemai.com'; appId = (_c = window.__MONA_LIGHT_APP_LIFE_CYCLE_LANUCH_QUERY) === null || _c === void 0 ? void 0 : _c.appId; filePath = file.name; index = filePath.lastIndexOf('.'); ext = filePath.slice(index); if (!(window.__MONA_LIGHT_APP_GET_TOEKN && appId)) return [3 /*break*/, 8]; url = "https://".concat(domain, "/light/upload/file/url"); return [4 /*yield*/, getTokenHeaders()]; case 3: headers = _e.sent(); return [4 /*yield*/, fetch("".concat(url, "?appId=").concat(appId, "&extension=").concat(ext), { headers: headers, credentials: 'include', }).then(pipeResponse)]; case 4: data = _e.sent(); if (!data) return [3 /*break*/, 8]; crc32 = require('./crc32.js').crc32; return [4 /*yield*/, file.arrayBuffer()]; case 5: buffer = _e.sent(); crc32Res = crc32(buffer); console.log('crc32 :>> ', crc32Res); headerAuthorization = data.headerAuthorization, headerHost = data.headerHost, url_1 = data.url; headers_1 = { host: headerHost, Authorization: "".concat(headerAuthorization), 'Content-CRC32': crc32Res, // 'Content-Type': 'image/png', }; return [4 /*yield*/, fetch(url_1.replace('http://', 'https://'), { method: 'PUT', headers: headers_1, body: buffer, }).then(function (res) { return res.json(); })]; case 6: uploadData = _e.sent(); return [4 /*yield*/, getDownLoadFileUrl((_d = uploadData === null || uploadData === void 0 ? void 0 : uploadData.payload) === null || _d === void 0 ? void 0 : _d.key)]; case 7: resultUrl = _e.sent(); return [2 /*return*/, resultUrl]; case 8: return [2 /*return*/]; } }); }); } //# sourceMappingURL=upload.js.map