UNPKG

@kwiz/common

Version:

KWIZ common utilities and helpers for M365 platform

27 lines 1.19 kB
import { blobToBase64 } from "../helpers/base64"; import { isNullOrEmptyString } from "../helpers/typecheckers"; import { GetJson } from "./rest"; import { GetFile } from "./sharepoint.rest/file.folder"; /** if the file is in SharePoint, provide a siteRelativeUrl so that we can get the file value via REST api to avoid CORS error when accessing a file on the site from within the app web */ export async function imageToBase64(imageSrc, siteRelativeUrl) { try { let fileBlob = null; if (!isNullOrEmptyString(siteRelativeUrl) && imageSrc.toLowerCase().indexOf(siteRelativeUrl.toLowerCase()) >= 0) { let spFile = await GetFile(siteRelativeUrl, imageSrc, true, "blob"); if (spFile && spFile.Exists) fileBlob = spFile.Content; } //try simple rest if the first option failed if (fileBlob === null) { fileBlob = await GetJson(imageSrc, null, { responseType: "blob", allowCache: true }); } if (fileBlob) { let base64 = await blobToBase64(fileBlob); return base64; } } catch (e) { } return null; } //# sourceMappingURL=base64.js.map