Hi everyone! This is my first post on Reddit, and I’d like to introduce myself: my name is Ruslan, I’m 18 years old, and I’m currently building my own cloud-based media engine and a Telegram bot for handling media files.
I’d like to dedicate this post to my open-source library for working with file metadata - files. I originally wrote it for my own personal needs, but during development, I realized that it’s a tool that others might find useful as well. Consequently, I decided to extract it into a separate, public repository.
>If the description below piques your interest and you’d like to check out the library, I’d be delighted to receive your feedback - or even just a star on GitHub! :)
The core purpose of the library is to handle file names, sizes, types, and MIME types. It is I/O-agnostic and does not serve as an extension of the operating system's capabilities; instead, it acts simply as a convenient and lightweight tool for managing file metadata.
The library is divided into three sub-packages (files, size, and mime), each of which encapsulates the logic for specific types of file data.
>Initially, everything was bundled into a single files package, but before releasing it to the public repository, I decided to clean up and refactor the project.
The library's primary value lies in its simplicity and speed. Internally, it implements fast validation and sanitization of file names (taking into account maximum name lengths, allowed characters, and reserved MS-DOS names). I deliberately opted against using regular expressions in favor of manual byte iteration, and I strove to minimize memory allocations as much as possible (for instance, by utilizing strings.Builder and strings.Cut).
Another equally important feature-one that I personally use very frequently-is a custom enum for file types. files.Type is a uint8 value that categorizes all the most common file types (such as audio, video, image, archive, and others). The type implements all the capabilities of the encoding package, so it will correctly parse your JSON—whether you specify the type using its numeric ID (since it's a uint8!) or its human-readable name.
Regarding the size package: it implements a Size type—based on float64 - that includes a custom JSON unmarshaler capable of interpreting both human-readable size representations ("10gb" or even "tb 100") and raw numeric values, which are interpreted as a byte count. Furthermore, the package provides a convenient constructor that allows you to specify a quantity and a unit, and - voilà - it yields the corresponding byte count stored within the internal Size type. Naturally, you can also convert a Size value back into a human-readable format using a built-in method.
s := New(1, Mb) // s = 1048576
log.Printf("size: %s", s) // output: "size: 1.00 MB"
I’ve tried to cover all the benefits and features of this project without overlooking anything important. I hope this text wasn't too dry; perhaps in the future, I'll get better at presenting my projects! ;)