svach

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2021 License: MIT Imports: 6 Imported by: 1

README

svach

Sanitize file name

Library

Documentation

CMD - Installation

go get github.com/nilsocket/svach/cmd/svach

CMD - Usage

svach            # print intended file name changes (Clean)
svach -r         # print intended file name changes recursively (Clean)

svach -c         # change intended file names (Clean)
svach -c -r      # change intended file names recursively (Clean)

svach -n         # print intended file name changes (Name)
svach -c -n -r   # change intended file names recursively (Name)

Difference between Name and Clean

Name

Creates a valid file name for all operating systems.

❯ svach -n 'Hello___World!!!!!/\\'
Hello___World!!!!!
Clean

Creates a valid file name and removes all control characters, repeated separators (_, -. +, \, !, ). Different kinds of space are replaced with normal space character.

❯ svach 'Hello___World!!!!!/\\'
Hello_World!

Documentation

Overview

Package svach file names for consistent naming across platforms. Incase of invalid fileName, a md5Sum of provided filename is returned

Options

`replaceStr`, replace invalid characters with given string, default "" (empty string)

`maxLen`, limit length of the output, default `240`

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrCntrl ,Control characters exist in `replaceStr`
	ErrCntrl = errors.New("Control characters exist in replaceStr")
	// ErrInval ,Invalid characters exist in `replaceStr`
	ErrInval = errors.New("Invalid characters like `., <, >, :, \", /, \\, |, ?, *` exist in replaceStr")

	// ErrLen ,`maxLen` greater than 255
	ErrLen = errors.New("maxLen can't be greater than 255")
)
View Source
var DefaultSvach = &Svach{"", iMaxLen}

DefaultSvach for Name and Clean

Functions

func Clean added in v0.0.2

func Clean(fileName string) string

Clean sanitizes `fileName` into more humane format.

Remove invisible and control characters, repeated separators. Replace different kinds of spaces with normal space.

Example
package main

import (
	"fmt"

	"github.com/nilsocket/svach"
)

func main() {
	res := svach.Clean(`.....Hello<>:/---\....W|orld?.!..`)
	fmt.Println(res)
}
Output:

.Hello-.World.!

func Name added in v0.0.2

func Name(fileName string) string

Name sanitizes `fileName`

Example
package main

import (
	"fmt"

	"github.com/nilsocket/svach"
)

func main() {
	// Incase of invalid filename, md5sum of filename is returned.
	res := svach.Name(`<>:"/\|?*`)
	fmt.Println(res)
}
Output:

3e4bde3cb1e4c9cfa2db74bbc536d5e2

Types

type Svach

type Svach struct {
	// contains filtered or unexported fields
}

Svach object

func WithOpts added in v0.0.2

func WithOpts(replaceStr string, maxLen int) (*Svach, error)

WithOpts returns Svach object, with opts set and returns error, if conditions aren't met.

Conditions:

- replaceStr can't contain Control or Invalid characters

- maxlen can't be greater than 255

Example
package main

import (
	"fmt"

	"github.com/nilsocket/svach"
)

func main() {
	s, _ := svach.WithOpts(" ", 6)
	res := s.Name(`.....H<>e:l.!..`)
	fmt.Println(res)
}
Output:

.H e l
Example (Error)
package main

import (
	"fmt"

	"github.com/nilsocket/svach"
)

func main() {
	_, err := svach.WithOpts("?", 6)
	fmt.Println(err)
}
Output:

Invalid characters like `., <, >, :, ", /, \, |, ?, *` exist in replaceStr

func (*Svach) Clean

func (s *Svach) Clean(fileName string) string

Clean sanitizes `fileName` into more humane format.

Remove invisible and control characters, repeated separators. Replace different kinds of spaces with normal space.

func (*Svach) Name

func (s *Svach) Name(fileName string) string

Name sanitizes `fileName`

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL