namespace Khaemhat.Core.Files; /// A file saved in the store. /// Where it is in the company folder (yyyy/guid); keep it to open or delete the file. /// The size in bytes. /// The SHA-256 of the content, lowercase hexadecimal. public sealed record StoredFile(string Key, long Size, string Sha256); /// The space a company uses in the store. /// How many files. /// Their total size. /// The quota of the company; 0 means no quota. public sealed record FileUsage(long Files, long Bytes, long QuotaBytes) { /// The bytes still available; without quota. public long RemainingBytes => QuotaBytes <= 0 ? long.MaxValue : Math.Max(0, QuotaBytes - Bytes); /// Whether the company uses more than its quota. public bool IsOverQuota => QuotaBytes > 0 && Bytes > QuotaBytes; } /// The limits applied while a file is written. /// The largest file accepted; 0 means no limit. /// The space a company may use in all; 0 means no quota. public sealed record FileLimits(long MaxBytes, long QuotaBytes) { /// The default size limit of an uploaded file (SPEC §9.4): 25 MB. public const long DefaultMaxBytes = 25L * 1024 * 1024; /// No limit at all (generated files are bounded by their generator). public static FileLimits None { get; } = new(0, 0); } /// Why the store refused a file. is one of the constants of this class. public sealed class FileStoreException : Exception { /// The file is larger than . public const string TooLarge = "file_too_large"; /// The file would take the company over its quota. public const string QuotaExceeded = "file_quota_exceeded"; /// The scanner () rejected the file. public const string Rejected = "file_rejected"; /// The file does not exist in the store. public const string NotFound = "file_not_found"; /// Creates the exception. public FileStoreException() : this(NotFound, "File store error.") { } /// Creates the exception. /// The message. public FileStoreException(string message) : this(NotFound, message) { } /// Creates the exception. /// The message. /// The cause. public FileStoreException(string message, Exception innerException) : base(message, innerException) => Code = NotFound; /// Creates the exception. /// The error code. /// The message. public FileStoreException(string code, string message) : base(message) => Code = code; /// The error code. public string Code { get; } } /// /// A file being written to the store: write the content to , then call /// . Disposing without committing discards what was written. /// public interface IFileStoreWriter : IAsyncDisposable { /// The write-only, non-seekable stream that receives the content (it counts bytes and computes SHA-256). /// Writing goes past (code ). Stream Content { get; } /// Completes the file: checks quota and scanner, then makes it visible under its key. /// Cancellation. /// The stored file. /// The file is refused. Task CommitAsync(CancellationToken cancellationToken = default); } /// /// Where attachment bytes live (SPEC §9.4): one folder per company, one subfolder per year. Writes are streamed and /// atomic (a partial file is never visible under its key); every key belongs to one company and cannot reach outside /// its folder. /// public interface IFileStore { /// Saves a file read from a stream. /// The company. /// The content, read to the end. /// The limits; for the store defaults. /// Cancellation. /// The stored file. /// The file is refused (size, quota, scanner). Task SaveAsync(string companyCode, Stream content, FileLimits? limits = null, CancellationToken cancellationToken = default); /// Starts a file that the caller writes (generators, ZIP packages). /// The company. /// The limits; for the store defaults. /// Cancellation. /// The writer; dispose it in every case. Task CreateAsync(string companyCode, FileLimits? limits = null, CancellationToken cancellationToken = default); /// Opens a file for reading. /// The company. /// The key returned when the file was saved. /// A read-only seekable stream; dispose it. /// The file does not exist (code ). Stream OpenRead(string companyCode, string key); /// Whether a file exists. /// The company. /// The key. /// if it exists. bool Exists(string companyCode, string key); /// Deletes a file; nothing happens when it does not exist. /// The company. /// The key. /// if a file was deleted. bool Delete(string companyCode, string key); /// The space the company uses, with its quota. /// The company. /// The quota to report; for the store default. /// Cancellation. /// The usage. Task GetUsageAsync(string companyCode, long? quotaBytes = null, CancellationToken cancellationToken = default); /// Removes the partial files (.part) left by writes interrupted more than ago. /// The company. /// How old a partial file must be. /// How many were removed. int DeleteAbandonedParts(string companyCode, TimeSpan olderThan); } /// The verdict of a scanner. /// Whether the file may be stored. /// Why it was refused (for example the name of the threat). public sealed record FileScanResult(bool Clean, string? Reason = null) { /// The file may be stored. public static FileScanResult Ok { get; } = new(true); } /// /// The hook for an antivirus (SPEC §9.4): Khaemhat includes none, the default implementation accepts every file. It /// runs on the complete file before it becomes visible. /// public interface IFileScanner { /// Scans a file. /// The company. /// The content, readable from the start. /// Cancellation. /// The verdict. ValueTask ScanAsync(string companyCode, Stream content, CancellationToken cancellationToken = default); }