From f10d1ab0225ad39dc1968e653f545aa25aac129e Mon Sep 17 00:00:00 2001 From: Han Yin Date: Tue, 23 Sep 2025 20:17:32 -0700 Subject: [PATCH] lib: add File version for GGUF Magic number verification --- .../java/com/arm/aichat/gguf/GgufMetadataReader.kt | 14 ++++++++++++-- .../aichat/internal/gguf/GgufMetadataReaderImpl.kt | 11 +++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/examples/llama.android/lib/src/main/java/com/arm/aichat/gguf/GgufMetadataReader.kt b/examples/llama.android/lib/src/main/java/com/arm/aichat/gguf/GgufMetadataReader.kt index 4d5ff25783..264a6c0bda 100644 --- a/examples/llama.android/lib/src/main/java/com/arm/aichat/gguf/GgufMetadataReader.kt +++ b/examples/llama.android/lib/src/main/java/com/arm/aichat/gguf/GgufMetadataReader.kt @@ -3,6 +3,7 @@ package com.arm.aichat.gguf import android.content.Context import android.net.Uri import com.arm.aichat.internal.gguf.GgufMetadataReaderImpl +import java.io.File import java.io.IOException import java.io.InputStream @@ -14,8 +15,17 @@ interface GgufMetadataReader { /** * Reads the magic number from the specified file path. * - * @param context Context for obtaining ContentResolver - * @param uri Uri to the GGUF file provided by ContentProvider + * @param file Java File to the GGUF file with absolute path + * @return true if file is valid GGUF, otherwise false + * @throws InvalidFileFormatException if file format is invalid + */ + suspend fun ensureSourceFileFormat(file: File): Boolean + + /** + * Reads the magic number from the specified file path. + * + * @param context Context for obtaining [android.content.ContentProvider] + * @param uri Uri to the GGUF file provided by [android.content.ContentProvider] * @return true if file is valid GGUF, otherwise false * @throws InvalidFileFormatException if file format is invalid */ diff --git a/examples/llama.android/lib/src/main/java/com/arm/aichat/internal/gguf/GgufMetadataReaderImpl.kt b/examples/llama.android/lib/src/main/java/com/arm/aichat/internal/gguf/GgufMetadataReaderImpl.kt index d8b95aff37..bf250ac13c 100644 --- a/examples/llama.android/lib/src/main/java/com/arm/aichat/internal/gguf/GgufMetadataReaderImpl.kt +++ b/examples/llama.android/lib/src/main/java/com/arm/aichat/internal/gguf/GgufMetadataReaderImpl.kt @@ -5,6 +5,7 @@ import android.net.Uri import com.arm.aichat.gguf.GgufMetadata import com.arm.aichat.gguf.GgufMetadataReader import com.arm.aichat.gguf.InvalidFileFormatException +import java.io.File import java.io.IOException import java.io.InputStream @@ -67,6 +68,16 @@ internal class GgufMetadataReaderImpl( is MetadataValue.ArrayVal -> elements.map { it.toPrimitive() } } + /** + * Reads the magic number from the specified file path. + * + * @param context Context for obtaining ContentResolver + * @param uri Uri to the GGUF file provided by ContentProvider + * @return true if file is valid GGUF, otherwise false + */ + override suspend fun ensureSourceFileFormat(file: File): Boolean = + file.inputStream().buffered().use { ensureMagic(it) } + /** * Reads the magic number from the specified file path. *