lib: add File version for GGUF Magic number verification
This commit is contained in:
parent
f833c3a7ac
commit
f10d1ab022
|
|
@ -3,6 +3,7 @@ package com.arm.aichat.gguf
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import com.arm.aichat.internal.gguf.GgufMetadataReaderImpl
|
import com.arm.aichat.internal.gguf.GgufMetadataReaderImpl
|
||||||
|
import java.io.File
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
|
|
||||||
|
|
@ -14,8 +15,17 @@ interface GgufMetadataReader {
|
||||||
/**
|
/**
|
||||||
* Reads the magic number from the specified file path.
|
* Reads the magic number from the specified file path.
|
||||||
*
|
*
|
||||||
* @param context Context for obtaining ContentResolver
|
* @param file Java File to the GGUF file with absolute path
|
||||||
* @param uri Uri to the GGUF file provided by ContentProvider
|
* @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
|
* @return true if file is valid GGUF, otherwise false
|
||||||
* @throws InvalidFileFormatException if file format is invalid
|
* @throws InvalidFileFormatException if file format is invalid
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import android.net.Uri
|
||||||
import com.arm.aichat.gguf.GgufMetadata
|
import com.arm.aichat.gguf.GgufMetadata
|
||||||
import com.arm.aichat.gguf.GgufMetadataReader
|
import com.arm.aichat.gguf.GgufMetadataReader
|
||||||
import com.arm.aichat.gguf.InvalidFileFormatException
|
import com.arm.aichat.gguf.InvalidFileFormatException
|
||||||
|
import java.io.File
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
|
|
||||||
|
|
@ -67,6 +68,16 @@ internal class GgufMetadataReaderImpl(
|
||||||
is MetadataValue.ArrayVal -> elements.map { it.toPrimitive() }
|
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.
|
* Reads the magic number from the specified file path.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue