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

[Document Intelligence] Corrupted images #31908

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 4 additions & 3 deletions sdk/core/core-rest-pipeline/src/nodeHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ class NodeHttpClient implements HttpClient {
) {
response.readableStreamBody = responseStream;
} else {
response.bodyAsText = await streamToText(responseStream);
const encoding = response.headers.get("content-type") === "image/png" ? "binary" : "utf8";
response.bodyAsText = await streamToText(responseStream, encoding);
}

return response;
Expand Down Expand Up @@ -337,7 +338,7 @@ function getDecodedResponseStream(
return stream;
}

function streamToText(stream: NodeJS.ReadableStream): Promise<string> {
function streamToText(stream: NodeJS.ReadableStream, encoding: "utf8" | "binary"): Promise<string> {
return new Promise<string>((resolve, reject) => {
const buffer: Buffer[] = [];

Expand All @@ -349,7 +350,7 @@ function streamToText(stream: NodeJS.ReadableStream): Promise<string> {
}
});
stream.on("end", () => {
resolve(Buffer.concat(buffer).toString("utf8"));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utf8 encoding corrupts the image data

resolve(Buffer.concat(buffer).toString(encoding));
});
stream.on("error", (e) => {
if (e && e?.name === "AbortError") {
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"build:samples": "dev-tool samples publish --force",
"build:test": "npm run clean && dev-tool run build-package && dev-tool run build-test",
"check-format": "dev-tool run vendored prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.{ts,cts,mts}\" \"test/**/*.{ts,cts,mts}\" \"*.{js,cjs,mjs,json}\"",
"clean": "dev-tool run vendored rimraf --glob dist dist-browser dist-esm test-dist temp types *.tgz *.log",
"clean": "dev-tool run vendored rimraf --glob dist dist-browser dist-esm test-dist temp types *.tgz",
"execute:samples": "dev-tool samples run samples-dev",
"extract-api": "dev-tool run vendored rimraf review && dev-tool run vendored mkdirp ./review && dev-tool run extract-api",
"format": "dev-tool run vendored prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.{ts,cts,mts}\" \"test/**/*.{ts,cts,mts}\" \"*.{js,cjs,mjs,json}\"",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { env } from "@azure-tools/test-recorder";
import DocumentIntelligence from "../../src/documentIntelligence.js";
import { describe, it } from "vitest";
import fs from "fs";
import path from "path";
import { getLongRunningPoller, isUnexpected } from "../../src/index.js";
import {
ASSET_PATH,
} from "./utils/utils.js";

describe("DocumentIntelligenceClient", () => {
describe("get AnalyzeResult methods", function () {
it("getAnalyzeResult figures", async function () {
await analyze();
});
});
});


const MODEL_ID = 'prebuilt-layout';

const FILE_PATH = path.join(ASSET_PATH, "test.pdf");

const API_ENDPOINT = env.DOCUMENT_INTELLIGENCE_ENDPOINT || "";
const API_KEY = env.DOCUMENT_INTELLIGENCE_API_KEY || "";

const client = DocumentIntelligence(API_ENDPOINT, {
key: API_KEY,
}, {
additionalPolicies: [{
policy: {
name: 'documentIntelligencePolicy',
async sendRequest(request, next) {
console.log(`Request: ${request.url}`);
return next(request);
}
},
position: "perCall"
}]
}
);


interface AnalyzeResponse {
analyzeResult?: {
figures?: Figure[];
};
}

interface Figure {
id?: string;
}

async function fetchFigure(responseId: string, figureId: string): Promise<void | null> {
console.log(`modelId: ${MODEL_ID}, responseId: ${responseId}, figureId: ${figureId}`);
const response = await client
.path(`/documentModels/{modelId}/analyzeResults/{resultId}/figures/{figureId}`, MODEL_ID, responseId, figureId)
.get();

console.log(response.headers["content-type"]);
console.log(response);
const { body } = response;

await fs.promises.writeFile(`./figures/${figureId}.png`, Buffer.from(body, "binary"));
}

async function analyze(): Promise<void> {
const base64Source = fs.readFileSync(FILE_PATH, { encoding: 'base64' });
await fs.promises.rm(`./figures`, { recursive: true }).catch(() => { });
await fs.promises.mkdir(`./figures`);

const initialResponse = await client.path(`/documentModels/{modelId}:analyze`, MODEL_ID).post({
contentType: 'application/json',
body: { base64Source },
queryParameters: {
output: ['figures'],
},
});

const poller = await getLongRunningPoller(client, initialResponse);

const response = await poller.pollUntilDone();

if (isUnexpected(response)) {
console.log(response);
throw new Error("The response was unexpected.");
}

const figures = (response.body as AnalyzeResponse).analyzeResult?.figures || [];

for (const figure of figures) {
if (figure.id) {

await fetchFigure(poller.getOperationId(), figure.id);
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import { relativeRecordingsPath } from "@azure-tools/test-recorder";

export default defineConfig({
test: {
reporters: ["verbose", "basic"],
reporters: ["verbose"],
outputFile: {
junit: "test-results.browser.xml",
},
fakeTimers: {
toFake: ["setTimeout", "Date"],
},
watch: false,
include: ["test/**/*.spec.ts"],
include: ["test/**/figures.spec.ts"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we only want to keep it at one test file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, apologies about this. I was testing my changes in core- with this example.
I'll revert this back once the changes in core are finalized :)

exclude: ["test/**/browser/*.spec.ts"],
coverage: {
include: ["src/**/*.ts"],
Expand Down
Loading