Tooark.Utils
4.4.0
See the version list below for details.
dotnet add package Tooark.Utils --version 4.4.0
NuGet\Install-Package Tooark.Utils -Version 4.4.0
<PackageReference Include="Tooark.Utils" Version="4.4.0" />
<PackageVersion Include="Tooark.Utils" Version="4.4.0" />
<PackageReference Include="Tooark.Utils" />
paket add Tooark.Utils --version 4.4.0
#r "nuget: Tooark.Utils, 4.4.0"
#:package Tooark.Utils@4.4.0
#addin nuget:?package=Tooark.Utils&version=4.4.0
#tool nuget:?package=Tooark.Utils&version=4.4.0
Tooark.Utils
Library of general utility functions that help development, including methods for handling strings, files, languages and localized lists.
🌍 Languages: 🇺🇸 English (this file) · 🇧🇷 Português
Installation
dotnet add package Tooark.Utils
Contents
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.ToMemoryStream(IFormFile? fromFile, long maxBytes = 0): Converts anIFormFileto aMemoryStream.ToMemoryStreamAsync(IFormFile? fromFile, long maxBytes = 0, CancellationToken cancellationToken = default): Converts anIFormFileto aMemoryStreamasynchronously.Extension(string? stringFile): Extracts the file extension from a base64 string.Extension(IFormFile? fromFile): Extracts the file extension from anIFormFile.
Constant:
DefaultMaxBytes: Default maximum size of the converted content. 5242880 bytes (5MB).
The error message key for the limit is in UtilErrorMessages.
Behavior:
- Converting an
IFormFilecopies the file's binary content. The returnedMemoryStreamis already positioned at the start. - The conversion returns
nullwhen the file is null or empty, 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. The limit is checked before reserving memory, and also block by block while reading the upload, so it holds even when the declared size does not match the actual content. - 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 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.
2. File Validation
Purpose: File validity checks.
Methods:
IsImage(IFormFile? file, long fileSize = 0): Checks whether the file is a valid image.IsDocument(IFormFile? file, long fileSize = 0): Checks whether the file is a valid document.IsVideo(IFormFile? file, long fileSize = 0): Checks whether the file is a valid video.IsCustom(IFormFile? file, long fileSize = 0, string[]? permittedExtensions = null): Checks whether the file is valid for custom extensions.
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 default size is 5242880 bytes (5MB), applied when
fileSizeis zero or negative. - 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.
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.
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 Example
File Conversion and Extension Extraction
using Tooark.Utils;
using Microsoft.AspNetCore.Http;
using Tooark.Exceptions;
var base64String = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQ...";
// Default 5MB limit
MemoryStream? memoryStream = FileConvert.ToMemoryStream(base64String);
MemoryStream? fromUpload = FileConvert.ToMemoryStream(formFile);
// Custom limit, in bytes
MemoryStream? large = FileConvert.ToMemoryStream(formFile, 20 * 1024 * 1024);
MemoryStream? asynchronous = await FileConvert.ToMemoryStreamAsync(formFile, cancellationToken: cancellationToken);
string? extension = FileConvert.Extension(base64String); // PNG
string? extensionFromFile = FileConvert.Extension(formFile); // TXT
// Above the limit, the error is explicit and already carries the HTTP 413 status
try
{
FileConvert.ToMemoryStream(formFile, 1024);
}
catch (PayloadTooLargeException ex)
{
// ex.Message -> "File.SizeExceeded;1024"
// ex.GetStatusCode() -> HttpStatusCode.RequestEntityTooLarge
}
File Validation
using Tooark.Utils;
using Microsoft.AspNetCore.Http;
bool isImage = FileValid.IsImage(formFile);
bool isDocument = FileValid.IsDocument(formFile, 10485760); // 10MB
bool isVideo = FileValid.IsVideo(formFile);
// With or without the leading dot, the result is the same
bool isCustom = FileValid.IsCustom(formFile, 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
string token = GenerateString.Token(); // 1A2B3C4D5E6F7A8B9C0D1E2F3A4B5C6DADE123E21...
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
IFormFileforFileConvertandFileValidwithout 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. Tooark.ExceptionsTooark.Validations
Contributing
Contributions are welcome! Feel free to open issues and pull requests in the Tooark.Utils repository.
License
This project is licensed under the BSD 3-Clause License. See the LICENSE file for details.
| 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.13)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Options (>= 10.0.12)
- Tooark.Exceptions (>= 4.4.0)
- Tooark.Validations (>= 4.4.0)
-
net8.0
- Microsoft.AspNetCore.Http (>= 2.3.13)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Options (>= 8.0.2)
- Tooark.Exceptions (>= 4.4.0)
- Tooark.Validations (>= 4.4.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.7.1 | 38 | 10/17/2026 |
| 4.7.0 | 348 | 9/30/2026 |
| 4.6.0 | 220 | 9/29/2026 |
| 4.5.0 | 180 | 9/29/2026 |
| 4.4.0 | 176 | 9/28/2026 |
| 4.3.0 | 184 | 9/26/2026 |
| 4.2.1 | 181 | 9/25/2026 |
| 4.2.0 | 204 | 9/20/2026 |
| 4.1.0 | 189 | 9/18/2026 |
| 4.0.1 | 272 | 8/25/2026 |
| 4.0.0 | 222 | 8/25/2026 |
| 3.3.6 | 168 | 9/18/2026 |
| 3.3.5 | 191 | 8/25/2026 |
| 3.3.4 | 369 | 8/12/2026 |
| 3.3.3 | 449 | 7/27/2026 |
| 3.3.2 | 381 | 6/11/2026 |
| 3.3.1 | 235 | 6/6/2026 |
| 2.3.5 | 158 | 9/18/2026 |
| 2.3.4 | 192 | 8/25/2026 |
| 2.3.3 | 207 | 7/27/2026 |