using AIStudio.Tools.Rust;
namespace AIStudio.Chat;
///
/// Represents an immutable file attachment with details about its type, name, path, and size.
///
/// The type of the file attachment.
/// The name of the file, including extension.
/// The full path to the file, including the filename and extension.
/// The size of the file in bytes.
public readonly record struct FileAttachment(FileAttachmentType Type, string FileName, string FilePath, long FileSizeBytes)
{
///
/// Gets a value indicating whether the file still exists on the file system.
///
public bool Exists => File.Exists(this.FilePath);
///
/// Gets a value indicating whether the file type is forbidden and should not be attached.
///
public bool IsForbidden => this.Type == FileAttachmentType.FORBIDDEN;
///
/// Gets a value indicating whether the file type is valid and allowed to be attached.
///
public bool IsValid => this.Type != FileAttachmentType.FORBIDDEN;
///
/// Creates a FileAttachment from a file path by automatically determining the type,
/// extracting the filename, and reading the file size.
///
/// The full path to the file.
/// A FileAttachment instance with populated properties.
public static FileAttachment FromPath(string filePath)
{
var fileName = Path.GetFileName(filePath);
var fileSize = File.Exists(filePath) ? new FileInfo(filePath).Length : 0;
var type = DetermineFileType(filePath);
return new FileAttachment(type, fileName, filePath, fileSize);
}
///
/// Determines the file attachment type based on the file extension.
/// Uses centrally defined file type filters from .
///
/// The file path to analyze.
/// The corresponding FileAttachmentType.
private static FileAttachmentType DetermineFileType(string filePath)
{
var extension = Path.GetExtension(filePath).TrimStart('.').ToLowerInvariant();
// Check if it's an image file:
if (FileTypeFilter.AllImages.FilterExtensions.Contains(extension))
return FileAttachmentType.IMAGE;
// Check if it's an audio file:
if (FileTypeFilter.AllAudio.FilterExtensions.Contains(extension))
return FileAttachmentType.AUDIO;
// Check if it's an allowed document file (PDF, Text, or Office):
if (FileTypeFilter.PDF.FilterExtensions.Contains(extension) ||
FileTypeFilter.Text.FilterExtensions.Contains(extension) ||
FileTypeFilter.AllOffice.FilterExtensions.Contains(extension))
return FileAttachmentType.DOCUMENT;
// All other file types are forbidden:
return FileAttachmentType.FORBIDDEN;
}
}