Tooark.Utils
4.11.0
dotnet add package Tooark.Utils --version 4.11.0
NuGet\Install-Package Tooark.Utils -Version 4.11.0
<PackageReference Include="Tooark.Utils" Version="4.11.0" />
<PackageVersion Include="Tooark.Utils" Version="4.11.0" />
<PackageReference Include="Tooark.Utils" />
paket add Tooark.Utils --version 4.11.0
#r "nuget: Tooark.Utils, 4.11.0"
#:package Tooark.Utils@4.11.0
#addin nuget:?package=Tooark.Utils&version=4.11.0
#tool nuget:?package=Tooark.Utils&version=4.11.0
Tooark.Utils
Library of general utility functions that help development, including methods for handling strings, files, languages and localized lists.
π Docs: Tooark.Utils on the site Β· All packages Β· API reference
π Languages: πΊπΈ English (this file) Β· π§π· PortuguΓͺs
π Contents
π§ Installation
dotnet add package Tooark.Utils
π§° Utilities
The available utilities are:
1. File Conversion and Extension Extraction
Purpose: File conversion and extension extraction.
Methods:
ToMemoryStream(string? stringFile, long maxBytes = 0): Converts a base64 string to aMemoryStream.CopyToMemoryStream(Stream? stream, long maxBytes = 0): Copies the content of aStreamto aMemoryStream.CopyToMemoryStreamAsync(Stream? stream, long maxBytes = 0, CancellationToken cancellationToken = default): Copies the content of aStreamto aMemoryStreamasynchronously.Extension(string? stringFile): Extracts the file extension from a base64 string.ExtensionFromFileName(string? fileName): Extracts the extension from the file name.
CopyToMemoryStream, CopyToMemoryStreamAsync and ExtensionFromFileName exist since v4.11.0.
Constant:
DefaultMaxBytes: Default maximum size of the converted content. 5242880 bytes (5MB).
The error message key for the limit is in UtilErrorMessages.
Behavior:
- Copying a
Streamstarts at the current position of the source and takes the binary content up to the end. The source is not closed: whoever opened the stream disposes it. The returnedMemoryStreamis already positioned at the start. - The conversion returns
nullwhen the stream is null, is empty or has already been read to the end, and when the string does not contain valid base64. - Size limit: above
maxBytesaPayloadTooLargeException(HTTP 413) is thrown, with the messageFile.SizeExceeded;{limit}. A non-positive value appliesDefaultMaxBytes; the absolute ceiling isint.MaxValue, which is what aMemoryStreamholds. For a string, the limit is checked from the length of the base64 part, before reserving memory. For aStreamthat reports its length, the excess is also refused before any allocation; for the others, such as a request body, the limit is checked block by block during the copy. - The string must carry the
;base64,marker, and the content is read right after it. A straybase64,elsewhere is not accepted. - The extension extracted from a base64 string comes from the declared type in the
data:type/extension;base64,contentformat, and the type may carry parameters (data:image/png;charset=utf-8;base64,...). - The extension extracted from a file name is what comes after the last dot. Whatever comes before a directory separator is discarded.
- The extension is validated: only ASCII letters and digits pass, with at most ten characters. Anything else results in
null. Since the extension comes from what the client declared β the file name or the data URL type β without that validation the return value could carry a directory separator, markup or text of arbitrary length into whoever consumes it. MIME types that have no extension form, such asimage/svg+xmlandapplication/vnd.ms-excel, result innull. - The extension is always returned in uppercase and without the leading dot. It reflects what was declared, not the actual file content.
Methods with IFormFile:
The three FileConvert methods that take an IFormFile are obsolete since v4.11.0 and removed in v5, together with the package dependency on Microsoft.AspNetCore.Http 2.x: IFormFile is an ASP.NET Core type, and the package only references ASP.NET Core because of them. They keep working on 4.x, with the CS0618 warning. The versions that take a string (ToMemoryStream(string?) and Extension(string?)) are not obsolete.
| Obsolete | With IFormFile (Tooark.AspNetCore.Files) |
Without IFormFile |
|---|---|---|
ToMemoryStream(IFormFile?, long) |
file.ToMemoryStream() |
CopyToMemoryStream(stream) |
ToMemoryStreamAsync(IFormFile?, long, CancellationToken) |
file.ToMemoryStreamAsync() |
CopyToMemoryStreamAsync(stream) |
Extension(IFormFile?) |
file.GetExtension() |
ExtensionFromFileName(name) |
In an ASP.NET Core application, use the IFormFile extensions of Tooark.AspNetCore, which apply the same rules. Outside one, use the versions that take a Stream and a file name.
2. File Validation
Purpose: File validity checks.
Methods:
IsImageFile(string? fileName, long length, long maxBytes = 0): Checks, by name and size, whether the file is a valid image.IsDocumentFile(string? fileName, long length, long maxBytes = 0): Checks, by name and size, whether the file is a valid document.IsVideoFile(string? fileName, long length, long maxBytes = 0): Checks, by name and size, whether the file is a valid video.IsCustomFile(string? fileName, long length, long maxBytes = 0, string[]? permittedExtensions = null): Checks, by name and size, whether the file is valid for custom extensions.
The four exist since v4.11.0. fileName is the name of the file, where the extension comes from, length is its size in bytes and maxBytes is the maximum size accepted.
Behavior:
- The check considers the size and the extension declared in the file name. It does not inspect the content, so it does not replace checking the actual file type when the uploaded content is untrusted.
- The image extensions are
.JPG,.JPEG,.PNG,.GIF,.BMP,.SVGand.WEBP; the document ones,.TXT,.CSV,.LOG,.PDF,.DOC,.DOCX,.XLS,.XLSX,.PPTand.PPTX; the video ones,.AVI,.MP4,.MPG,.MPEGand.WMV. - The default maximum size is 5242880 bytes (5MB), applied when
maxBytesis zero or negative. A file with a zero or negativelengthis invalid. - Custom extensions are compared case-insensitively and may be given with or without the leading dot:
"pdf"and".PDF"have the same effect. - If
permittedExtensionsis null, empty or contains only blank entries, every image, document and video extension is accepted.
Methods with IFormFile:
The four FileValid methods that take an IFormFile are obsolete since v4.11.0 and removed in v5, for the same reason as the FileConvert methods. They keep working on 4.x, with the CS0618 warning.
| Obsolete | With IFormFile (Tooark.AspNetCore.Files) |
Without IFormFile |
|---|---|---|
IsImage(IFormFile?, long) |
file.IsImage() |
IsImageFile(name, length) |
IsDocument(IFormFile?, long) |
file.IsDocument() |
IsDocumentFile(name, length) |
IsVideo(IFormFile?, long) |
file.IsVideo() |
IsVideoFile(name, length) |
IsCustom(IFormFile?, long, string[]?) |
file.IsCustom() |
IsCustomFile(name, length) |
The maximum size parameter, called fileSize in the obsolete methods, is called maxBytes in the replacements: whoever passes it by name (fileSize: ...) has to rename it when migrating.
3. String Generation
Purpose: Generation of strings by specific and random criteria.
Methods:
Sequential(int number): Converts an integer into its equivalent alphabetic representation.Password(int len = 12, bool upper = true, bool lower = true, bool number = true, bool special = true, bool similarity = false): Generates a string with specific criteria.Hexadecimal(int sizeToken = 128): Generates a random hexadecimal string.GuidCode(): Generates a Guid string without hyphens, with 32 characters.Token(int length = 256): Generates a token string. Obsolete since v4.8.0, removed in v5: it only generates the text, with no hash or verification. UseSecureToken.Create()fromTooark.Securities, which comes withHashand constant-timeVerify.
Behavior:
Sequentialreturns uppercase letters, and an empty string when the number is not positive.Passworduses a cryptographic generator and guarantees at least one character of each enabled type. Lengths below 8 are raised to 8, and if every type is disabled, all of them are re-enabled.Hexadecimalreturns exactly the requested size, including odd sizes. Values below 2 are raised to 2.
4. Information Lookup
Purpose:
Retrieval of localized information from a list of objects. The list type must have the LanguageCode property and the property being looked up.
Methods:
Name<T>(IList<T>? list, string? languageCode = null): Gets the localized name from a list of objects.Title<T>(IList<T>? list, string? languageCode = null): Gets the localized title from a list of objects.Description<T>(IList<T>? list, string? languageCode = null): Gets the localized description from a list of objects.Keywords<T>(IList<T>? list, string? languageCode = null): Gets the localized keywords from a list of objects.Custom<T>(IList<T>? list, string property, string? languageCode = null): Gets a localized value of a property from a list of objects.
Behavior:
- When
languageCodeis not provided, the current language is used. - The lookup tries the requested language, then the application's default language, and finally the first item of the list.
- A null or empty list, and a property with a null value, result in an empty string.
- Properties that are not
stringare converted withToString(). - If
LanguageCodeor the requested property does not exist on the type, aGetInfoExceptionis thrown.
5. Languages
Purpose: Language management.
Members:
Default: The default language code used by the application. Default "en-US".Current: The current language code of the execution environment.CurrentCulture: The current culture of the execution environment.SetCulture(string? culture): Sets the current culture using the culture name in thexx-XXformat.SetCulture(CultureInfo? culture): Sets the current culture using aCultureInfoobject.Instance: TheILanguageimplementation in use, replaceable in tests. Rejects null withArgumentNullException.
The Tooark.Utils.Interfaces.ILanguage interface exposes DefaultLanguage, CurrentLanguage, CurrentCultureInfo, SetCultureInfo(string?) and SetCultureInfo(CultureInfo?).
Behavior:
- The current language follows the culture of the execution flow (
CultureInfo.CurrentCulture), not a process-wide global state. In a web application, each request sees only its own culture. - The culture name is accepted case-insensitively:
"pt-br"is normalized to"pt-BR". - A null or empty name, one outside the
xx-XXformat, or one of a non-existent culture results in the application's default culture. SetCulturealso setsCultureInfo.CurrentUICulture.
6. Normalization
Purpose: String normalization. Removes spaces and punctuation, converts to uppercase and transliterates special characters.
Methods:
Value(string value): Normalizes a value by removing spaces, converting to uppercase and replacing special characters.
7. Error Messages
Purpose:
Translation keys of the error messages generated by the package itself, in Tooark.Utils.Messages.UtilErrorMessages.
Constants:
FileSizeExceeded:"File.SizeExceeded". Emitted byFileConvertwhen the content exceeds the limit, in theFile.SizeExceeded;{limit}format.
The translations ship with the Tooark.Extensions resources.
π Usage Examples
File Conversion and Extension Extraction
using Tooark.Utils;
using Tooark.Exceptions;
var base64String = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQ...";
// Default 5MB limit
MemoryStream? memoryStream = FileConvert.ToMemoryStream(base64String);
// From a Stream: the copy starts at the current position, and whoever opened the source disposes it
using var report = File.OpenRead("report.pdf");
MemoryStream? fromStream = FileConvert.CopyToMemoryStream(report);
// Custom limit, in bytes, in the asynchronous version
await using var video = File.OpenRead("video.mp4");
MemoryStream? large = await FileConvert.CopyToMemoryStreamAsync(video, 20 * 1024 * 1024, cancellationToken);
string? extension = FileConvert.Extension(base64String); // PNG
string? extensionFromName = FileConvert.ExtensionFromFileName("report.pdf"); // PDF
// Above the limit, the error is explicit and already carries the HTTP 413 status
try
{
using var spreadsheet = File.OpenRead("spreadsheet.xlsx");
FileConvert.CopyToMemoryStream(spreadsheet, 1024);
}
catch (PayloadTooLargeException ex)
{
// ex.Message -> "File.SizeExceeded;1024"
// ex.GetStatusCode() -> HttpStatusCode.RequestEntityTooLarge
}
File Validation
using Tooark.Utils;
// The name and the size come from wherever the file is: disk, storage, message
var photo = new FileInfo("photo.png");
bool isImage = FileValid.IsImageFile(photo.Name, photo.Length);
bool isDocument = FileValid.IsDocumentFile("contract.pdf", 2097152, 10485760); // 2MB, with a 10MB limit
bool isVideo = FileValid.IsVideoFile("lesson.mp4", 4194304);
// With or without the leading dot, the result is the same
bool isCustom = FileValid.IsCustomFile("data.csv", 1024, permittedExtensions: ["txt", ".csv"]);
String Generation
using Tooark.Utils;
string sequential = GenerateString.Sequential(27); // AA
string password = GenerateString.Password(); // aB3@dE1#fG2$
string onlyLetters = GenerateString.Password(16, number: false, special: false);
string hex = GenerateString.Hexadecimal(); // 1A2B3C4D5E6F...
string guid = GenerateString.GuidCode(); // 1A2B3C4D5E6F7A8B9C0D1E2F3A4B5C6D
Information Lookup
using Tooark.Utils;
// The type must have the LanguageCode property
var list = new List<MyObject> { /* ... */ };
string name = GetInfo.Name(list);
string title = GetInfo.Title(list);
string description = GetInfo.Description(list);
string keywords = GetInfo.Keywords(list);
// Explicit language and custom property
string customValue = GetInfo.Custom(list, "CustomProperty", "pt-BR");
Languages
using Tooark.Utils;
using System.Globalization;
Language.SetCulture("pt-BR");
string current = Language.Current; // pt-BR
string defaultLanguage = Language.Default; // en-US
CultureInfo currentCulture = Language.CurrentCulture; // pt-BR
// Accepts the name case-insensitively
Language.SetCulture("es-es"); // es-ES
// An invalid name falls back to the default culture
Language.SetCulture("pt"); // en-US
Normalization
using Tooark.Utils;
string normalizedValue = Normalize.Value("OlΓ‘ Mundo!"); // OLAMUNDO
string withSymbols = Normalize.Value("R&D 100$"); // RANDD100DOLLAR
π Dependencies
- Microsoft.AspNetCore.Http 2.x β brings
IFormFilefor the obsolete methods ofFileConvertandFileValidwithout requiring theMicrosoft.AspNetCore.Appshared framework, so the package also runs in console and worker applications. Inside an ASP.NET Core application the framework's own assembly wins andIFormFileis the same type. It leaves the package in v5, together with the methods that take anIFormFile, obsolete since v4.11.0. - Microsoft.AspNetCore.WebUtilities 2.3.14 β transitive dependency of the package above, pinned to the version that fixes CVE-2026-118750 so consumers never resolve the vulnerable 2.3.x. It leaves with it in v5.
Tooark.ExceptionsTooark.Validations
π€ Contributing
Contributions are welcome! Start with CONTRIBUTING.md β it covers the development workflow, the coding and commit conventions and the pull request checklist. Bugs and feature requests go through the issue templates of the Tooark repository.
By participating you agree to the Code of Conduct.
π Help & Security
- β Questions, bugs, feature ideas β see SUPPORT.md for the right channel
- π Security vulnerabilities β do not open a public issue; follow SECURITY.md
π Support
If Tooark helps your projects, consider supporting its development:
- π GitHub Sponsors
- β Ko-fi
Every contribution helps keep the project maintained and improving. Thank you! π
π License
This project is licensed under the BSD 3-Clause License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.AspNetCore.Http (>= 2.3.14)
- Microsoft.AspNetCore.WebUtilities (>= 2.3.14)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.15)
- Microsoft.Extensions.Options (>= 10.0.15)
- Tooark.Exceptions (>= 4.11.0)
- Tooark.Validations (>= 4.11.0)
-
net8.0
- Microsoft.AspNetCore.Http (>= 2.3.14)
- Microsoft.AspNetCore.WebUtilities (>= 2.3.14)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Options (>= 8.0.2)
- Tooark.Exceptions (>= 4.11.0)
- Tooark.Validations (>= 4.11.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Tooark.Utils:
| Package | Downloads |
|---|---|
|
Tooark.Extensions
Package for managing extensions and utilities for .NET applications. |
|
|
Tooark
Package with all Tooark resources for .NET applications. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.11.0 | 137 | 11/21/2026 |
| 4.10.0 | 242 | 11/18/2026 |
| 4.9.0 | 243 | 11/16/2026 |
| 4.8.0 | 238 | 11/15/2026 |
| 4.7.3 | 224 | 11/11/2026 |
| 4.7.2 | 204 | 11/3/2026 |
| 4.7.1 | 305 | 10/17/2026 |
| 4.7.0 | 558 | 9/30/2026 |
| 4.6.0 | 241 | 9/29/2026 |
| 4.5.0 | 197 | 9/29/2026 |
| 4.4.0 | 192 | 9/28/2026 |
| 4.3.0 | 200 | 9/26/2026 |
| 4.2.1 | 200 | 9/25/2026 |
| 4.2.0 | 226 | 9/20/2026 |
| 4.1.0 | 206 | 9/18/2026 |
| 4.0.1 | 284 | 8/25/2026 |
| 3.3.7 | 172 | 11/15/2026 |
| 3.3.6 | 185 | 9/18/2026 |
| 2.3.6 | 172 | 11/15/2026 |
| 2.3.5 | 173 | 9/18/2026 |