assetmanager

package module
v0.0.0-...-4656ec9 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2022 License: MIT Imports: 5 Imported by: 11

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AssetManager

type AssetManager struct {
	Files         map[string][]byte
	AllowedExt    map[string]bool
	DisallowedExt map[string]bool
	Replacers     map[string]ReplaceFunc
}

AssetManager asset manager

func New

func New() *AssetManager

New asset manager

func (*AssetManager) AddAllowedExt

func (manager *AssetManager) AddAllowedExt(ext ...string)

AddAllowedExt add allowed extension

func (*AssetManager) AddDir

func (manager *AssetManager) AddDir(dirs ...string)

AddDir add directories

func (*AssetManager) AddDisallowExt

func (manager *AssetManager) AddDisallowExt(ext ...string)

AddDisallowExt add disallow extension

func (*AssetManager) AddFile

func (manager *AssetManager) AddFile(files ...string)

AddFile add file

func (*AssetManager) AddFileString

func (manager *AssetManager) AddFileString(name, content string)

AddFileString add file

func (*AssetManager) AddReplacer

func (manager *AssetManager) AddReplacer(name string, fn ReplaceFunc)

AddReplacer add replacer function

Example
package main

import (
	"fmt"
	"strings"

	"github.com/s4l1h/assetmanager"
)

func main() {
	asset := assetmanager.New()
	// Add Replacer
	asset.AddReplacer("testReplacer", func(name string) string {
		return fmt.Sprintf("main:%s", name)
	})
	// Add Another Replacer.
	asset.AddReplacer("convergohtml", func(name string) string {
		return strings.Replace(name, ".html", ".gohtml", -1)
	})
	// add file from string
	asset.AddFileString("index.html", "test content")
	// get file content
	c, _ := asset.GetString("main:index.gohtml")
	fmt.Println(c)
}
Output:

test content

func (*AssetManager) CheckAddFile

func (manager *AssetManager) CheckAddFile(file string) bool

CheckAddFile check file.

func (*AssetManager) CheckAllowed

func (manager *AssetManager) CheckAllowed(ext string) bool

CheckAllowed check file allowed and trusted file.

func (*AssetManager) CheckDisallowed

func (manager *AssetManager) CheckDisallowed(ext string) bool

CheckDisallowed check file disallowed and trusted file.

func (*AssetManager) Copy

func (manager *AssetManager) Copy(from *AssetManager)

Copy assetmanager

func (*AssetManager) Delete

func (manager *AssetManager) Delete(name string)

Delete file from manager

func (*AssetManager) Exists

func (manager *AssetManager) Exists(file string) bool

Exists check file exists

func (*AssetManager) ExistsReplacer

func (manager *AssetManager) ExistsReplacer(name string) bool

ExistsReplacer check replacer function exists

func (*AssetManager) File

func (manager *AssetManager) File(name string) (*bytes.Buffer, error)

File io.Reader implement

Example
package main

import (
	"fmt"
	"io/ioutil"

	"github.com/s4l1h/assetmanager"
)

func main() {

	asset := assetmanager.New()
	asset.AddDir("./test")
	buffer, err := asset.File("test/index.html")
	if err != nil {
		fmt.Println(err)
	}
	r, _ := ioutil.ReadAll(buffer)
	fmt.Println(string(r))
}
Output:

index.html

func (*AssetManager) Get

func (manager *AssetManager) Get(file string) ([]byte, error)

Get get file

func (*AssetManager) GetAll

func (manager *AssetManager) GetAll() map[string][]byte

GetAll files

func (*AssetManager) GetExt

func (manager *AssetManager) GetExt(file string) string

GetExt return file extension

func (*AssetManager) GetString

func (manager *AssetManager) GetString(file string) (string, error)

GetString get file string

func (*AssetManager) Merge

func (manager *AssetManager) Merge(asset *AssetManager) *AssetManager

Merge another assetmanager

Example
package main

import (
	"fmt"

	"github.com/s4l1h/assetmanager"
)

func main() {
	asset := assetmanager.New()
	asset.AddFileString("index.html", "index.html content")
	asset.AddFileString("asset1.html", "asset1.html content")
	asset2 := assetmanager.New()
	asset2.AddFileString("asset2.html", "asset2.html content")
	asset.Merge(asset2)
	c, _ := asset.GetString("asset2.html")
	fmt.Println(c)
}
Output:

asset2.html content

func (*AssetManager) MergeAndRunReplacer

func (manager *AssetManager) MergeAndRunReplacer(asset *AssetManager) *AssetManager

MergeAndRunReplacer merge another assetmanager and run replacer

Example
package main

import (
	"fmt"
	"strings"

	"github.com/s4l1h/assetmanager"
)

func main() {
	asset := assetmanager.New()
	asset.AddReplacer("ToUpper", func(name string) string {
		return strings.ToUpper(name)
	})
	asset.AddFileString("index.html", "index.html content")
	asset.AddFileString("asset1.html", "asset1.html content")

	asset2 := assetmanager.New()
	asset2.AddFileString("asset2.html", "asset2.html content")
	asset.MergeAndRunReplacer(asset2)
	c, _ := asset.GetString("ASSET2.HTML")
	fmt.Println(c)
}
Output:

asset2.html content

func (*AssetManager) RemoveAllowedExt

func (manager *AssetManager) RemoveAllowedExt(ext string)

RemoveAllowedExt remove allowed extension

func (*AssetManager) RemoveDisallowExt

func (manager *AssetManager) RemoveDisallowExt(ext string)

RemoveDisallowExt remove disallow extension

func (*AssetManager) RemoveReplacer

func (manager *AssetManager) RemoveReplacer(name string)

RemoveReplacer remove replacer function

type ReplaceFunc

type ReplaceFunc func(name string) string

ReplaceFunc file name replacer function

Jump to

Keyboard shortcuts

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