filenamer

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2020 License: MIT Imports: 6 Imported by: 0

README

Package filenamer

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.

Installation

go get -u github.com/artdarek/filenamer

Examples

package main

import (
	"github.com/artdarek/filenamer"
	"fmt"
)

func main() {
	fn := filenamer.New("test file.jpg")
	fn.CleanIt()
	
	name := fn.Get()
	
	fmt.Println(name)
}
package main

import (
	"github.com/artdarek/filenamer"
	"fmt"
)

func main() {
	fn := filenamer.New("test fil e.jpg") // original filename
	
	fn.CleanIt() // replaces whitespaces with _
	fn.WithReplacement("test", "example") // will replace 'test' with 'example'

	fn.WithRandomPrefix(10) // add random charset as a prefix to your file
	fn.WithRandomSuffix(10) // add random charset as a suffix to your file	
	
	fn.SeparateWith("-") // set active separator for all feature modifications
	
	fn.WithTimestamp() // add timestamp prefix
	fn.WithCustomPrefix("image") // add prefix to your filename	
	fn.WithCustomSuffix("renamed") // add suffix to your filename
	fn.WithExtension("png") // change extension of your filename
	fn.WithExtensionRemoved() // removes extension from your filename

	fn.HashIt() // generates md5 hash from generated filename
	
	name := fn.Get() // Get generated filename

	fmt.Println(name)
}

See some more examples in cmd/examples/main.go. You can test them in action by running following command:

go run cmd/examples/main.go

Contributing

Pull requests, bug fixes and issue reports are welcome.

Before proposing a change, please discuss your change by raising an issue.

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

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

func New(fileName string) Filenamer

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) Get

func (fn *Filenamer) Get() string

Get returns output filename

func (*Filenamer) HashIt

func (fn *Filenamer) HashIt()

HashIt hash output filename

func (*Filenamer) SeparateWith

func (fn *Filenamer) SeparateWith(separator string)

SeparateWith sets active separator for all feature filename manipulations

func (*Filenamer) WithCustomPrefix

func (fn *Filenamer) WithCustomPrefix(prefix string)

WithCustomPrefix add custom prefix to filename

func (*Filenamer) WithCustomSuffix

func (fn *Filenamer) WithCustomSuffix(suffix string)

WithCustomSuffix adds custom suffix to filename

func (*Filenamer) WithExtension

func (fn *Filenamer) WithExtension(extension string)

WithExtension adds new extension

func (*Filenamer) WithExtensionRemoved

func (fn *Filenamer) WithExtensionRemoved()

WithExtensionRemoved removes extension from file name

func (*Filenamer) WithRandomPrefix

func (fn *Filenamer) WithRandomPrefix(length int)

WithRandomPrefix adds unique random string to filename

func (*Filenamer) WithRandomSuffix

func (fn *Filenamer) WithRandomSuffix(length int)

WithRandomSuffix adds unique random string to filename

func (*Filenamer) WithReplacement

func (fn *Filenamer) WithReplacement(current string, target string)

WithReplacement replaces part of a name with different string

func (*Filenamer) WithTimestamp

func (fn *Filenamer) WithTimestamp()

WithTimestamp adds timestamp to filename

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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