Address remaining Obsidian plugin scanner warnings

This commit is contained in:
Andrew Beal 2026-06-28 15:42:47 +01:00
parent 2131ad0edd
commit 0372b9a182
4 changed files with 15 additions and 15 deletions

View file

@ -146,20 +146,17 @@ export abstract class BaseAIFileService implements IAIFileService {
// Calculate total length and concatenate
const totalLength = parts.reduce((sum, part) => sum + part.byteLength, 0);
const result = new Uint8Array(totalLength);
const buffer = new ArrayBuffer(totalLength);
const result = new Uint8Array(buffer);
let offset = 0;
for (const part of parts) {
result.set(part, offset);
offset += part.byteLength;
}
return this.bytesToBuffer(result);
return buffer;
}
protected bytesToBuffer(bytes: Uint8Array): ArrayBuffer {
return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) as ArrayBuffer;
}
protected getHeader(headerName: string, headers: Record<string, string>): string | undefined {
const header = Object.keys(headers).find(header => header.toLowerCase() === headerName.toLowerCase());
return header ? headers[header] : undefined;

View file

@ -40,8 +40,8 @@ export class GeminiFileService extends BaseAIFileService {
protected async uploadFileToAPI(data: string, mimeType: string, displayName?: string): Promise<string> {
return this.withRetry("Upload file", async () => {
const bytes = StringTools.toBytes(data);
const numBytes = bytes.byteLength;
const buffer = StringTools.toBuffer(data);
const numBytes = buffer.byteLength;
const metadata = displayName ? { file: { displayName } } : {};
@ -78,7 +78,7 @@ export class GeminiFileService extends BaseAIFileService {
"X-Goog-Upload-Offset": "0",
"X-Goog-Upload-Command": "upload, finalize"
},
body: this.bytesToBuffer(bytes),
body: buffer,
throw: false
});

View file

@ -78,17 +78,21 @@ export abstract class StringTools {
}
public static toBytes(input: string): Uint8Array {
return new Uint8Array(this.toBuffer(input));
}
public static toBuffer(input: string): ArrayBuffer {
const binaryString = atob(input);
const bytes = new Uint8Array(new ArrayBuffer(binaryString.length));
const buffer = new ArrayBuffer(binaryString.length);
const bytes = new Uint8Array(buffer);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
return buffer;
}
public static async computeSHA256Hash(base64: string): Promise<string> {
const bytes = this.toBytes(base64);
const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) as ArrayBuffer;
const buffer = this.toBuffer(base64);
const hashBuffer = await crypto.subtle.digest('SHA-256', buffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');

View file

@ -229,8 +229,7 @@ export class ConversationFileSystemService {
return filePath;
}
const bytes = StringTools.toBytes(attachment.base64);
const arrayBuffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) as ArrayBuffer;
const arrayBuffer = StringTools.toBuffer(attachment.base64);
const result = await this.fileSystemService.writeBinaryFile(filePath, arrayBuffer, true);