Documentation ¶
Overview ¶
Package Filenamer provides a set of methods for filename manipulation. It's a very simple API for adding custom prefixes and suffixes to your base filename such as timestamps, random strings etc. Very useful when working with file uploads and you need to genereate unique filenames for your uploads.
Basic usage example
// file name source := "filename.jpg" // create new filenamer instance fn := filenamer.New(source)
Example (Example1) ¶
This example shows basic usage when given file name is just cleaned up from characters that should not be used in general
source := "test FiLe.jpg" fn := New(source) fn.CleanIt() fmt.Println(fn.Get())
Output: test_file.jpg
Example (Example2) ¶
This example shows how to add prefix to a file name
source := "test FiLe.JPG" fn := New(source) fn.CleanIt() fn.WithCustomPrefix("00001") fmt.Println(fn.Get())
Output: 00001_test_file.jpg
Example (Example3) ¶
This example shows how to hash file name
source := "test FiLe.JPG" fn := New(source) fn.CleanIt() fn.WithCustomPrefix("00001") fn.HashIt() fmt.Println(fn.Get())
Output: b39483abd82c1fcc3f76616b324ea8d6.jpg
Example (Example4) ¶
This example shows how to hash file name and add suffix to it
source := "test FiLe.JPG" fn := New(source) fn.CleanIt() fn.WithCustomPrefix("00001") fn.HashIt() fn.WithCustomSuffix("00002") fmt.Println(fn.Get())
Output: b39483abd82c1fcc3f76616b324ea8d6_00002.jpg
Index ¶
- type Filenamer
- func (fn *Filenamer) CleanIt()
- func (fn *Filenamer) Get() string
- func (fn *Filenamer) HashIt()
- func (fn *Filenamer) SeparateWith(separator string)
- func (fn *Filenamer) WithCustomPrefix(prefix string)
- func (fn *Filenamer) WithCustomSuffix(suffix string)
- func (fn *Filenamer) WithExtension(extension string)
- func (fn *Filenamer) WithExtensionRemoved()
- func (fn *Filenamer) WithRandomPrefix(length int)
- func (fn *Filenamer) WithRandomSuffix(length int)
- func (fn *Filenamer) WithReplacement(current string, target string)
- func (fn *Filenamer) WithTimestamp()
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Filenamer ¶
type Filenamer struct {
// contains filtered or unexported fields
}
Filenamer stores input and output file name and it's current separator
func New ¶
New creates new Filenamer instance
Example ¶
This example shows how to create new instance of Filenamer
source := "test.jpg" New(source)
Output:
func (*Filenamer) CleanIt ¶
func (fn *Filenamer) CleanIt()
CleanIt cleans all undesired characters from filename
func (*Filenamer) SeparateWith ¶
SeparateWith sets active separator for all feature filename manipulations
func (*Filenamer) WithCustomPrefix ¶
WithCustomPrefix add custom prefix to filename
func (*Filenamer) WithCustomSuffix ¶
WithCustomSuffix adds custom suffix to filename
func (*Filenamer) WithExtension ¶
WithExtension adds new extension
func (*Filenamer) WithExtensionRemoved ¶
func (fn *Filenamer) WithExtensionRemoved()
WithExtensionRemoved removes extension from file name
func (*Filenamer) WithRandomPrefix ¶
WithRandomPrefix adds unique random string to filename
func (*Filenamer) WithRandomSuffix ¶
WithRandomSuffix adds unique random string to filename
func (*Filenamer) WithReplacement ¶
WithReplacement replaces part of a name with different string
func (*Filenamer) WithTimestamp ¶
func (fn *Filenamer) WithTimestamp()
WithTimestamp adds timestamp to filename