Skip to content

Commit

Permalink
Merge pull request #1258 from PooLP/dev
Browse files Browse the repository at this point in the history
PR : correction for Bing search image api endpoint
  • Loading branch information
AJIXuMuK authored Jul 11, 2022
2 parents 2612e46 + 93546a7 commit 1cd6499
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/documentation/docs/controls/FilePicker.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ The FilePicker component can be configured with the following properties:
| context | BaseComponentContext | yes | Current context. |
| accepts | string[] | no | Array of strings containing allowed files extensions. E.g. [".gif", ".jpg", ".jpeg", ".bmp", ".dib", ".tif", ".tiff", ".ico", ".png", ".jxr", ".svg"] |
| required | boolean | no | Sets the label to inform that the value is required. |
| bingAPIKey | string | no | Used to execute WebSearch. If not provided SearchTab will not be available. |
| bingAPIKey | string | no | Used to execute WebSearch. If not provided SearchTab will not be available. The API key can be created on a Azure account ([Bing image search API](https://www.microsoft.com/en-us/bing/apis/bing-image-search-api)), a free version exist for 1000 query per month ([Pricing](https://www.microsoft.com/en-us/bing/apis/pricing)) |
| disabled | boolean | no | Specifies if the picker button is disabled |
| hidden | boolean | no | Specifies if the picker button is hidden (if hidden, panel visibility can still be controlled with isPanelOpen) |
| itemsCountQueryLimit | number | no | Number of items to obtain when executing REST queries. Default 100. |
Expand Down
4 changes: 2 additions & 2 deletions src/common/utilities/GeneralHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ export const toRelativeUrl = (absoluteUrl: string): string => {
if (!absoluteUrl) {
return '';
}

return absoluteUrl.replace(/^(?:\/\/|[^/]+)*\//, '/');
const relativeUrl: string = new URL(absoluteUrl).pathname;
return relativeUrl;
};

export function sortString(a: string, b: string, isDesc: boolean): number {
Expand Down
13 changes: 8 additions & 5 deletions src/services/FilesSearchService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseComponentContext } from '@microsoft/sp-component-base';
import { SPHttpClient } from "@microsoft/sp-http";
import { SPHttpClient, HttpClientResponse } from "@microsoft/sp-http";
import { ISearchResult, BingQuerySearchParams, IRecentFile } from "./FilesSearchService.types";
import { find } from "office-ui-fabric-react/lib/Utilities";
import { GeneralHelper } from "../common/utilities/GeneralHelper";
Expand Down Expand Up @@ -143,15 +143,18 @@ export class FilesSearchService {
}

// Submit the request
const apiUrl: string = `https://www.bingapis.com/api/v7/images/search?appid=${this.bingAPIKey}&traffictype=Internal_monitor&q=${encodeURIComponent(query)}&count=${maxResults}&aspect=${aspect}&maxFileSize=${maxFileSize}&size=${size}&mkt=en-US&license=${license}`;
const apiUrl: string = `https://api.bing.microsoft.com/v7.0/images/search?q=${encodeURIComponent(query)}&count=${maxResults}&aspect=${aspect}&maxFileSize=${maxFileSize}&size=${size}&mkt=en-US&license=${license}`;

const searchDataResponse: any = await this.context.httpClient.get(apiUrl, SPHttpClient.configurations.v1, {
const searchDataResponse: HttpClientResponse = await this.context.httpClient.get(apiUrl, SPHttpClient.configurations.v1, {
method: 'GET',
mode: 'cors'
mode: 'cors',
headers: {
'Ocp-Apim-Subscription-Key': this.bingAPIKey,
}
});

if (!searchDataResponse || !searchDataResponse.ok) {
throw new Error(`Something went wrong when executing search query. Status='${searchDataResponse.statusMessage}'`);
throw new Error(`Something went wrong when executing search query. Status='${searchDataResponse.statusText}'`);
}
const searchData = await searchDataResponse.json();
if (!searchData || !searchData.value) {
Expand Down
24 changes: 18 additions & 6 deletions src/services/OrgAssetsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,27 @@ export class OrgAssetsService extends FileBrowserService {
public getListItems = async (listUrl: string, folderPath: string, acceptedFilesExtensions?: string[], nextPageQueryStringParams?: string): Promise<FilesQueryResult> => {
let filesQueryResult: FilesQueryResult = { items: [], nextHref: null };
try {

// Retrieve Lib path from folder path
if (folderPath.charAt(0) !== "/") {
folderPath = `/${folderPath}`;
const isRootSite = this.context.pageContext.site.serverRelativeUrl === '/';

if (!isRootSite) {
if (folderPath.charAt(0) !== '/') {
folderPath = `/${folderPath}`;
}

} else {
if (folderPath.charAt(0) === '/') {
folderPath = folderPath.substring(1);
}
}

// Remove all the rest of the folder path
let libName = folderPath.replace(`${this._orgAssetsLibraryServerRelativeSiteUrl}/`, "");
libName = libName.split("/")[0];
// Buil absolute library URL
const libFullUrl = this.buildAbsoluteUrl(`${this._orgAssetsLibraryServerRelativeSiteUrl}/${libName}`);
let libName = folderPath.replace(`${this._orgAssetsLibraryServerRelativeSiteUrl}/`, '');
libName = libName.split('/')[0];

// Build absolute library URL
const libFullUrl = this.buildAbsoluteUrl(!isRootSite ? `${this._orgAssetsLibraryServerRelativeSiteUrl}/${libName}` : `${this._orgAssetsLibraryServerRelativeSiteUrl}${libName}`);

let queryStringParams: string = "";
// Do not pass FolderServerRelativeUrl as query parameter
Expand Down

0 comments on commit 1cd6499

Please sign in to comment.