From 0372b9a1827ea265a62eb44e86dacc8ff8a8ed52 Mon Sep 17 00:00:00 2001 From: Andrew Beal Date: Sun, 28 Jun 2026 15:42:47 +0100 Subject: [PATCH] Address remaining Obsidian plugin scanner warnings --- AIClasses/BaseAIFileService.ts | 9 +++------ AIClasses/Gemini/GeminiFileService.ts | 6 +++--- Helpers/StringTools.ts | 12 ++++++++---- Services/ConversationFileSystemService.ts | 3 +-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/AIClasses/BaseAIFileService.ts b/AIClasses/BaseAIFileService.ts index 7b056fa..19858fc 100644 --- a/AIClasses/BaseAIFileService.ts +++ b/AIClasses/BaseAIFileService.ts @@ -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 | undefined { const header = Object.keys(headers).find(header => header.toLowerCase() === headerName.toLowerCase()); return header ? headers[header] : undefined; diff --git a/AIClasses/Gemini/GeminiFileService.ts b/AIClasses/Gemini/GeminiFileService.ts index c9425c5..f77a6bb 100644 --- a/AIClasses/Gemini/GeminiFileService.ts +++ b/AIClasses/Gemini/GeminiFileService.ts @@ -40,8 +40,8 @@ export class GeminiFileService extends BaseAIFileService { protected async uploadFileToAPI(data: string, mimeType: string, displayName?: string): Promise { 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 }); diff --git a/Helpers/StringTools.ts b/Helpers/StringTools.ts index 0014f9c..71373c0 100644 --- a/Helpers/StringTools.ts +++ b/Helpers/StringTools.ts @@ -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 { - 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(''); diff --git a/Services/ConversationFileSystemService.ts b/Services/ConversationFileSystemService.ts index f730479..49b8c2e 100644 --- a/Services/ConversationFileSystemService.ts +++ b/Services/ConversationFileSystemService.ts @@ -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);