• Stars
    star
    9
  • Rank 1,878,895 (Top 39 %)
  • Language
    Crystal
  • License
    MIT License
  • Created over 4 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Crystal shard for filename sanitization

zaru_crystal

Build Status Docs GitHub release GitHub stars

Filename sanitization for Crystal. This is useful when you generate filenames for downloads from user input. Port of the Zaru gem for Ruby.

Installation

  1. Add the dependency to your shard.yml:
 dependencies:
   zaru_crystal:
     github: szTheory/zaru_crystal
  1. Run shards install

Usage

require "zaru_crystal"
Zaru.sanitize! "  whatver//wëird:user:înput:"
# => "whatēverwëirduserînput"

Zaru takes a given filename (a string) and normalizes, filters and truncates it.

It removes the bad stuff but leaves unicode characters in place, so users can use whatever alphabets they want to. Zaru also doesn't remove whitespace– instead, any sequence of whitespace that is 1 or more characters in length is collapsed to a single space. Filenames are truncated so that they are at maximum 255 characters long.

If extra breathing room is required (for example to add your own filename extension later), you can leave extra room with the :padding option:

Zaru.sanitize! "A"*400, padding: 100
# resulting filename is 145 characters long

If you need to customize the fallback filename you can add your own fallback with the :fallback option:

Zaru.sanitize! "<<<", fallback: "no_file"
# resulting filename is "no_file"

Bad things in filenames

Wikipedia has a good overview on filenames. Basically, on modern-ish operating systems, the following characters are considered no-no (Zaru filters these):

/ \ ? * : | " < >

Additionally the ASCII control characters (hexadecimal 00 to 1f) are filtered.

All Unicode whitespace at the beginning and end of the potential filename is removed, and any Unicode whitespace within the filename is collapse to a single space character.

Certain filenames are reserved in Windows and are filtered.

Wait, what, Zaru?

zaru_crystal is licensed under the terms of the MIT license.

More Repositories

1

exifcleaner

Cross-platform desktop GUI app to clean image metadata
TypeScript
1,325
star
2

real-world-phoenix

Real World Phoenix apps and their open source codebases for developers to learn from
138
star
3

upgrow

[mirror] A sustainable architecture for Ruby on Rails.
Ruby
132
star
4

real-world-elixir-apps

Real World Elixir apps and their open source codebases for developers to learn from
33
star
5

beamtoolbox

Curated BEAM language libraries by category
21
star
6

streakable

Track consecutive day streaks in your ActiveRecord models
Ruby
15
star
7

chip8go

Chip-8 emulator
Go
10
star
8

meta_presenter

Focused Rails presenter classes
Ruby
10
star
9

vsc-erlang-formatter

Erlang code formatter for Visual Studio Code
TypeScript
8
star
10

NDS-Cheat-Databases

[mirror] These cheat database sets are for Nintendo DS flashcarts, cheating devices, computer emulators, and homebrew applications for the Nintendo 2DS/3DS consoles. Each database contains Action Replay codes for your favorite NDS games.
7
star
11

startingperl.com

StartingPerl.com
6
star
12

real-world-nerves

Real World Nerves apps and their open source codebases for developers to learn from
6
star
13

real-world-erlang-apps

Real World Erlang apps and their open source codebases for developers to learn from
6
star
14

elixirtoolbox.dev

Curated list of Elixir/Erlang libraries
Elixir
4
star
15

turbolinks-scroll

Persist scroll position between Turbolinks AJAX calls
JavaScript
4
star
16

programmingnewsletters.com

Weekly programming newsletters without an email subscription
Perl
4
star
17

real-world-absinthe

Real World Absinthe apps and their open source codebases for developers to learn from
4
star
18

vscode-packer-powertools

VS Code extension for Packer with code formatting, inline error messages, syntax highlighting, code folding, and command palette integration
TypeScript
3
star
19

real-world-plug

Real World Plug apps and their open source codebases for developers to learn from
3
star
20

mixcloud-rss

[in production] Mixcloud RSS Reader written in Node JS
JavaScript
2
star
21

shellwords-crystal

This module manipulates strings according to the word parsing rules of the UNIX Bourne shell
Crystal
2
star
22

vsc-hex-lens

Hover info for Hex packages (Mix/Rebar3) for Visual Studio Code. Supports BEAM languages like Erlang, Elixir, Gleam, etc.
TypeScript
1
star