@pnp/spfx-property-controls
Version:
Reusable property pane controls for SharePoint Framework solutions
218 lines • 11.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OneDriveService = void 0;
const tslib_1 = require("tslib");
const sp_http_1 = require("@microsoft/sp-http");
const FileBrowserService_1 = require("./FileBrowserService");
const GeneralHelper_1 = require("../helpers/GeneralHelper");
class OneDriveService extends FileBrowserService_1.FileBrowserService {
constructor(context, itemsToDownloadCount) {
super(context, itemsToDownloadCount);
/**
* Gets files from OneDrive personal library
*/
this.getListItems = (libraryName, folderPath, acceptedFilesExtensions, nextPageQueryStringParams) => tslib_1.__awaiter(this, void 0, void 0, function* () {
let filesQueryResult = { items: [], nextHref: null };
try {
const oneDriveRootFolder = yield this.getOneDriveRootFolderFullUrl();
const encodedListUrl = encodeURIComponent(oneDriveRootFolder);
let queryStringParams = "";
folderPath = folderPath ? folderPath : this.oneDriveRootFolderRelativeUrl;
const encodedFolderPath = encodeURIComponent(folderPath);
if (nextPageQueryStringParams) {
// Remove start ? from the query params
if (nextPageQueryStringParams.charAt(0) === "?") {
nextPageQueryStringParams = nextPageQueryStringParams.substring(1);
}
queryStringParams = nextPageQueryStringParams;
}
else {
queryStringParams = `RootFolder=${encodedFolderPath}`;
}
const restApi = `${this.context.pageContext.web.absoluteUrl}/_api/SP.List.GetListDataAsStream?listFullUrl='${encodedListUrl}'&${queryStringParams}`;
filesQueryResult = yield this._getListDataAsStream(restApi, null, acceptedFilesExtensions);
}
catch (error) {
filesQueryResult.items = null;
console.error(error instanceof Error ? error.message : String(error));
}
return filesQueryResult;
});
this.getListItemsByListId = (listId, folderPath, acceptedFilesExtensions, nextPageQueryStringParams) => tslib_1.__awaiter(this, void 0, void 0, function* () {
let filesQueryResult = { items: [], nextHref: null };
try {
const oneDriveRootFolder = yield this.getOneDriveRootFolderFullUrl();
const encodedListUrl = encodeURIComponent(oneDriveRootFolder);
let queryStringParams = "";
folderPath = folderPath ? folderPath : this.oneDriveRootFolderRelativeUrl;
const encodedFolderPath = encodeURIComponent(folderPath);
if (nextPageQueryStringParams) {
// Remove start ? from the query params
if (nextPageQueryStringParams.charAt(0) === "?") {
nextPageQueryStringParams = nextPageQueryStringParams.substring(1);
}
queryStringParams = nextPageQueryStringParams;
}
else {
queryStringParams = `RootFolder=${encodedFolderPath}`;
}
const restApi = `${this.context.pageContext.web.absoluteUrl}/_api/SP.List.GetListDataAsStream?listFullUrl='${encodedListUrl}'&${queryStringParams}`;
filesQueryResult = yield this._getListDataAsStream(restApi, null, acceptedFilesExtensions);
}
catch (error) {
filesQueryResult.items = null;
console.error(error instanceof Error ? error.message : String(error));
}
return filesQueryResult;
});
/**
* Downloads document content from OneDrive location.
*/
this.downloadSPFileContent = (absoluteFileUrl, fileName) => tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
// replace url OneDrive site URL with current web url
const urlTokens = absoluteFileUrl.split("/_api/");
const fileUrl = `${this.context.pageContext.web.absoluteUrl}/_api/${urlTokens[1]}?`;
const fileInfoResult = yield this.context.spHttpClient.get(fileUrl, sp_http_1.SPHttpClient.configurations.v1);
const fileInfo = yield fileInfoResult.json();
const oneDrvieFileUrl = fileInfo["@content.downloadUrl"];
const fileDownloadResult = yield this.context.httpClient.get(oneDrvieFileUrl, sp_http_1.SPHttpClient.configurations.v1, {
headers: new Headers(),
method: 'GET',
mode: 'cors'
});
if (!fileDownloadResult || !fileDownloadResult.ok) {
throw new Error(`Something went wrong when downloading the file. Status='${fileDownloadResult.status}'`);
}
// Return file created from blob
const blob = yield fileDownloadResult.blob();
return GeneralHelper_1.GeneralHelper.getFileFromBlob(blob, fileName);
}
catch (err) {
console.error(`[OneDriveService.fetchFileContent] Err='${err instanceof Error ? err.message : String(err)}'`);
return null;
}
});
/**
* Gets users one drive personal documents library path
*/
this.getOneDriveRootFolderFullUrl = () => tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
// Return result if already obtained
if (this.oneDriveRootFolderAbsoluteUrl) {
return this.oneDriveRootFolderAbsoluteUrl;
}
const oneDriveUrl = yield this.getOneDrivePersonalUrl();
if (!oneDriveUrl) {
throw new Error(`Cannot obtain OneDrive personal URL.`);
}
const apiUrl = `${this.context.pageContext.web.absoluteUrl}/_api/SP.RemoteWeb()/Web/Lists?$filter=BaseTemplate eq 700 and BaseType eq 1&='${encodeURIComponent(oneDriveUrl)}'`;
const oneDriveFolderResult = yield this.context.spHttpClient.get(apiUrl, sp_http_1.SPHttpClient.configurations.v1, {
headers: {
"accept": "application/json;odata=nometadata",
"content-type": "application/json;odata=nometadata",
"odata-version": ""
}
});
if (!oneDriveFolderResult || !oneDriveFolderResult.ok) {
throw new Error(`Something went wrong when executing oneDriveRootFolder retrieve request. Status='${oneDriveFolderResult.status}'`);
}
const oneDriveLibsData = yield oneDriveFolderResult.json();
if (!oneDriveLibsData || !oneDriveLibsData.value || oneDriveLibsData.value.length === 0) {
throw new Error(`Cannot read one drive libs data.`);
}
const myDocumentsLibrary = oneDriveLibsData.value[0];
this.oneDrivePersonalLibraryTitle = myDocumentsLibrary.Title;
this.oneDrivePersonalLibraryId = myDocumentsLibrary.Id;
this.oneDriveRootFolderRelativeUrl = `${myDocumentsLibrary.ParentWebUrl}/${myDocumentsLibrary.Title}`;
this.oneDriveRootFolderAbsoluteUrl = `${this.oneDrivePersonalUrl}${myDocumentsLibrary.Title}`;
}
catch (error) {
console.error(`[FileBrowserService.getOneDrivePersonalUrl] Err='${error instanceof Error ? error.message : String(error)}'`);
this.oneDriveRootFolderAbsoluteUrl = null;
}
return this.oneDriveRootFolderAbsoluteUrl;
});
/**
* Gets OneDrive RootFolder server relative URL.
*/
this.getOneDriveRootFolderRelativeUrl = () => tslib_1.__awaiter(this, void 0, void 0, function* () {
if (!this.oneDriveRootFolderRelativeUrl) {
yield this.getOneDriveRootFolderFullUrl();
}
return this.oneDriveRootFolderRelativeUrl;
});
/**
* Gets OneDrive personal library Title
*/
this.getOneDrivePersonalLibraryTitle = () => tslib_1.__awaiter(this, void 0, void 0, function* () {
if (!this.oneDrivePersonalLibraryTitle) {
yield this.getOneDriveRootFolderFullUrl();
}
return this.oneDrivePersonalLibraryTitle;
});
/**
* Gets OneDrive personal library Id
* @returns OneDrive library Id
*/
this.getOneDrivePersonalLibraryId = () => tslib_1.__awaiter(this, void 0, void 0, function* () {
if (!this.oneDrivePersonalLibraryId) {
yield this.getOneDriveRootFolderFullUrl();
}
return this.oneDrivePersonalLibraryId;
});
/**
* Gets OneDrive library metadata
*/
this.getOneDriveMetadata = () => tslib_1.__awaiter(this, void 0, void 0, function* () {
if (!this.oneDriveRootFolderRelativeUrl) {
yield this.getOneDriveRootFolderFullUrl();
}
return {
folderPath: this.oneDriveRootFolderRelativeUrl,
libraryAbsolutePath: this.oneDriveRootFolderAbsoluteUrl,
libraryTitle: this.oneDrivePersonalLibraryTitle,
libraryId: this.oneDrivePersonalLibraryId
};
});
/**
* Gets personal site path.
*/
this.getOneDrivePersonalUrl = () => tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
// Return result if already obtained
if (this.oneDrivePersonalUrl) {
return this.oneDrivePersonalUrl;
}
const userProfileApi = `${this.context.pageContext.web.absoluteUrl}/_api/SP.UserProfiles.ProfileLoader.GetProfileLoader/GetUserProfile`;
const userProfileResult = yield this.context.spHttpClient.post(userProfileApi, sp_http_1.SPHttpClient.configurations.v1, {});
if (!userProfileResult || !userProfileResult.ok) {
throw new Error(`Something went wrong when executing user profile request. Status='${userProfileResult.status}'`);
}
const profileData = yield userProfileResult.json();
if (!profileData) {
throw new Error(`Cannot read user profile data.`);
}
this.oneDrivePersonalUrl = profileData.FollowPersonalSiteUrl;
}
catch (error) {
console.error(`[FileBrowserService.getOneDrivePersonalUrl] Err='${error instanceof Error ? error.message : String(error)}'`);
this.oneDrivePersonalUrl = null;
}
return this.oneDrivePersonalUrl;
});
/**
* Creates an absolute URL
*/
this.buildAbsoluteUrl = (relativeUrl) => {
return `https://${this.oneDrivePersonalUrl.split("//")[1].split("/")[0]}${relativeUrl}`;
};
this.oneDrivePersonalUrl = null;
this.oneDriveRootFolderRelativeUrl = null;
this.oneDriveRootFolderAbsoluteUrl = null;
this.oneDrivePersonalLibraryTitle = null;
this.oneDrivePersonalLibraryId = null;
}
}
exports.OneDriveService = OneDriveService;
//# sourceMappingURL=OneDriveService.js.map