Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for 2825 #2826

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,23 @@ export function hOP<T extends string>(o: any, p: T): boolean {
return Object.hasOwnProperty.call(o, p);
}


/**
* @returns validates and returns a valid atob conversion
*/
export function parseToAtob(str: string): string {
const base64Regex = /^[A-Za-z0-9+/]+={0,2}$/;
try {
// test if str has been JSON.stringified
const parsed = JSON.parse(str);
if(base64Regex.test(parsed)){
return atob(parsed);
}
return null;
} catch (err) {
// Not a valid JSON string, check if it's a standalone Base64 string
return base64Regex.test(str) ? atob(str) : null;
}
}

/**
* Generates a ~unique hash code
Expand Down
18 changes: 16 additions & 2 deletions packages/queryable/behaviors/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Queryable } from "../queryable.js";
import { hOP, TimelinePipe } from "@pnp/core";
import { hOP, TimelinePipe, parseToAtob } from "@pnp/core";
import { isFunc } from "@pnp/core";

export function DefaultParse(): TimelinePipe {
Expand All @@ -25,7 +25,21 @@ export function TextParse(): TimelinePipe {

export function BlobParse(): TimelinePipe {

return parseBinderWithErrorCheck(r => r.blob());
return parseBinderWithErrorCheck( async (response) => {
const binaryResponseBody = parseToAtob(await response.clone().text());
// handle batch responses for things that are base64, like photos https://github.com/pnp/pnpjs/issues/2825
if(binaryResponseBody){
// Create an array buffer from the binary string
const arrayBuffer = new ArrayBuffer(binaryResponseBody.length);
const uint8Array = new Uint8Array(arrayBuffer);
for (let i = 0; i < binaryResponseBody.length; i++) {
uint8Array[i] = binaryResponseBody.charCodeAt(i);
}
// Create a Blob from the array buffer
return new Blob([arrayBuffer], {type:response.headers.get("Content-Type")});
}
return response.blob();
});
}

export function JSONParse(): TimelinePipe {
Expand Down
Loading