Skip to content

.NET library that supports working with "data" URLs (RFC 2397)

License

Notifications You must be signed in to change notification settings

FolkerKinzel/DataUrls

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FolkerKinzel.DataUrls

NuGet GitHub

.NET library that supports working with "data" URLs (RFC 2397)

Project Reference

The library contains the static DataUrl class that supports the "data" URL scheme (RFC 2397), which allows to embed data in a URI. The DataUrl class allows

  • building "data" URL strings from files, byte arrays or text,
  • parsing "data" URL strings as DataUrlInfo structs in order to examine their content without having to allocate a lot of sub-strings, and to enable the comparison of "data" URLs for equality,
  • retrieving the embedded data from "data" URL strings,
  • retrieving an appropriate file type extension for the embedded data.

The library is designed to support performance and small heap allocation.

Version History

.

Example:

Creating and parsing a "data" URL:

using System.Diagnostics;
using FolkerKinzel.DataUrls;
using OneOf;

namespace Examples;

public static class DataUrlExample
{
    public static void Example()
    {
        string fotoFilePath = CreatePhotoFile();
        string dataUrl = DataUrl.FromFile(fotoFilePath);
        File.Delete(fotoFilePath);

        // Uncomment this to show the content of the
        // "data" URL in the Microsoft Edge browser.
        // (Make shure you have this browser installed.):
        // ShowPictureInMicrosoftEdge(dataUrl);

        Console.WriteLine(dataUrl);
        Console.WriteLine();

        // Parse the content of the "data" URL:
        _ = DataUrl.TryParse(dataUrl, out DataUrlInfo info);

        Console.WriteLine($"Contains Bytes: {info.ContainsEmbeddedBytes}");
        Console.WriteLine($"Contains Text:  {info.ContainsEmbeddedText}");
        Console.WriteLine($"MIME Type:      {info.MimeType}");
        Console.WriteLine($"File Type Ext.: {info.GetFileTypeExtension()}");
        Console.WriteLine($"Data Encoding:  {info.DataEncoding}");

        if (info.TryGetEmbeddedBytes(out byte[]? bytes))
        {
            Console.WriteLine($"Data Length:    {bytes.Length} Bytes");
        }
    }

    private static string CreatePhotoFile()
    {
        string url = "data:image/jpeg;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/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VeT72rT+dNxSMNq10iJOUKtyWi0JFFIH0+uvTs//Z";
        string path = "";

        if (DataUrl.TryGetEmbeddedData(url, out OneOf<string, byte[]> data, out string? fileTypeExtension)
            && data.Value is byte[] bytes)
        {
            path = Path.Combine(Directory.GetCurrentDirectory(), $"{Guid.NewGuid()}{fileTypeExtension}");
            File.WriteAllBytes(path, bytes);
        }
        
        return path;
    }

    private static void ShowPictureInMicrosoftEdge(string dataUrl)
    {
        var process = new Process();
        process.StartInfo.UseShellExecute = true;
        process.StartInfo.FileName = "msedge";
        process.StartInfo.Arguments = dataUrl;
        _ = process.Start();
    }
}

/*
Console Output:

data:image/jpeg;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAABLAAD/4QM1aHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjYtYzE0NSA3OS4xNjIzMTksIDIwMTgvMDIvMTUtMjA6Mjk6NDMgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLyIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bXA6Q3JlYXRvclRvb2w9IkFkb2JlIFBob3Rvc2hvcCBFbGVtZW50cyAxNy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xa2vdihLCn2uPtP9aai7n24e3mvcfMSy+3d0pZxbY/Iw5dLHi1us33G9uJrXGI0k6zuzNQeI15GlXNAKaz9dLuwVdzNRd2QqlmOgid0fM3vZLO/7ZmYkkmvryadYW2VIoyI1RNxQKooNbpKERAmNBMBbczuXO5jpZY/A5O4SWG4ltzKyndv2R0r8A0O/wDOoD1E8TvumTZrtkcvYMrHj7f9KLM8aAAM1I2KNVqD/MD8al63E1K2MEiRMzbGBppry4pJICxetaqT/XTEGZz7nx2QvOqJPBzWWC4j5ycTyCSVRqbbedCd56IPae++wuz9Qw3UcvkJjHgiUhRFKvcqKmKSdwKu0a/YK/FPnfSyddUcsBqY3Z2HesKToJr/AE7FX2O6li8dJG0kq26PLUEVeT72rT+dNxSMNq10iJOUKtyWi0JFFIH0+uvTs//Z

Contains Bytes: True
Contains Text:  False
MIME Type:      image/jpeg
File Type Ext.: .jpg
Data Encoding:  Base64
Data Length:    2472 Bytes
 */