Skip to content

Commit

Permalink
Transfer file package from assets (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
unanoc authored Feb 9, 2022
1 parent 1b26142 commit af3a665
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 0 deletions.
29 changes: 29 additions & 0 deletions file/asset_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package file

import (
"github.com/trustwallet/go-primitives/coin"
)

type AssetFile struct {
path *Path
}

func NewAssetFile(path string) *AssetFile {
return &AssetFile{path: NewPath(path)}
}

func (i *AssetFile) Path() string {
return i.path.String()
}

func (i *AssetFile) Type() string {
return i.path.fileType
}

func (i *AssetFile) Chain() coin.Coin {
return i.path.chain
}

func (i *AssetFile) Asset() string {
return i.path.asset
}
167 changes: 167 additions & 0 deletions file/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package file

import (
"os"
"path/filepath"
"regexp"

"github.com/trustwallet/assets-go-libs/strings"
"github.com/trustwallet/go-primitives/coin"
)

var (
regexAssetInfoFile = regexp.MustCompile(`blockchains/(\w+[\-]\w+|\w+)/assets/(\w+[\-]\w+|\w+)/info.json$`)
regexAssetLogoFile = regexp.MustCompile(`blockchains/(\w+[\-]\w+|\w+)/assets/(\w+[\-]\w+|\w+)/logo.png$`)

regexChainInfoFile = regexp.MustCompile(`blockchains/(\w+[\-]\w+|\w+)/info/info.json$`)
regexChainLogoFile = regexp.MustCompile(`blockchains/(\w+[\-]\w+|\w+)/info/logo.png$`)

regexTokenListFile = regexp.MustCompile(`blockchains/(\w+[\-]\w+|\w+)/tokenlist.json$`)
regexTokenListExtendedFile = regexp.MustCompile(`blockchains/(\w+[\-]\w+|\w+)/tokenlist-extended.json$`)

regexValidatorsAssetLogo = regexp.MustCompile(
`blockchains/(\w+[\-]\w+|\w+)/validators/assets/(\w+[\-]\w+|\w+)/logo.png$`)
regexValidatorsList = regexp.MustCompile(`blockchains/(\w+[\-]\w+|\w+)/validators/list.json$`)

regexDappsLogo = regexp.MustCompile(`dapps/[a-zA-Z-.]+\.png$`)
)

var (
regexAssetFolder = regexp.MustCompile(`blockchains/(\w+[\-]\w+|\w+)/assets/(\w+[\-]\w+|\w+)$`)
regexAssetsFolder = regexp.MustCompile(`blockchains/(\w+[\-]\w+|\w+)/assets$`)

regexValidatorsFolder = regexp.MustCompile(`blockchains/(\w+[\-]\w+|\w+)/validators$`)
regexValidatorsAssetFolder = regexp.MustCompile(
`blockchains/(\w+[\-]\w+|\w+)/validators/assets/(\w+[\-]\w+|\w+)$`)
regexValidatorsAssetsFolder = regexp.MustCompile(`blockchains/(\w+[\-]\w+|\w+)/validators/assets$`)

regexChainFolder = regexp.MustCompile(`blockchains/(\w+[^/])$`)
regexChainInfoFolder = regexp.MustCompile(`blockchains/(\w+[\-]\w+|\w+)/info$`)
regexChainsFolder = regexp.MustCompile(`blockchains$`)

regexDappsFolder = regexp.MustCompile(`dapps$`)
regexRoot = regexp.MustCompile(`./$`)
)

func GetRegexMap() map[string]*regexp.Regexp {
return map[string]*regexp.Regexp{
TypeAssetInfoFile: regexAssetInfoFile,
TypeAssetLogoFile: regexAssetLogoFile,

TypeChainInfoFile: regexChainInfoFile,
TypeChainLogoFile: regexChainLogoFile,

TypeTokenListFile: regexTokenListFile,
TypeTokenListExtendedFile: regexTokenListExtendedFile,

TypeValidatorsListFile: regexValidatorsList,
TypeValidatorsLogoFile: regexValidatorsAssetLogo,

TypeDappsLogoFile: regexDappsLogo,

TypeAssetFolder: regexAssetFolder,
TypeAssetsFolder: regexAssetsFolder,

TypeChainFolder: regexChainFolder,
TypeChainsFolder: regexChainsFolder,
TypeChainInfoFolder: regexChainInfoFolder,

TypeDappsFolder: regexDappsFolder,
TypeRootFolder: regexRoot,

TypeValidatorsFolder: regexValidatorsFolder,
TypeValidatorsAssetsFolder: regexValidatorsAssetsFolder,
TypeValidatorsAssetFolder: regexValidatorsAssetFolder,
}
}

type Path struct {
path string
chain coin.Coin
asset string
fileType string
regexpMap map[string]*regexp.Regexp
}

func NewPath(path string) *Path {
p := Path{
path: path,
regexpMap: GetRegexMap(),
}

fileType, reg := p.defineFileType(path)
if reg == nil {
p.fileType = TypeUnknown

return &p
}

match := reg.FindStringSubmatch(path)
if fileType != TypeUnknown {
p.fileType = fileType
}

if len(match) >= 2 {
chain, err := coin.GetCoinForId(match[1])
if err != nil {
p.chain = coin.Coin{Handle: match[1]}
} else {
p.chain = chain
}
}

if len(match) == 3 {
p.asset = match[2]
}

return &p
}

func (p Path) Type() string {
return p.fileType
}

func (p Path) String() string {
return p.path
}

func (p Path) Chain() coin.Coin {
return p.chain
}

func (p Path) Asset() string {
return p.asset
}

func (p Path) defineFileType(path string) (string, *regexp.Regexp) {
for t, r := range p.regexpMap {
if r.MatchString(path) {
return t, r
}
}

return TypeUnknown, nil
}

func ReadLocalFileStructure(root string, filesToSkip []string) ([]string, error) {
paths := []string{"./"}

err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if strings.Contains(path, filesToSkip) {
return nil
}

paths = append(paths, path)

return nil
})
if err != nil {
return nil, err
}

return paths, nil
}
58 changes: 58 additions & 0 deletions file/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package file

import (
"path/filepath"
"strings"
"sync"
)

type Service struct {
mu *sync.RWMutex
cache map[string]*AssetFile
}

func NewService(filePaths ...string) *Service {
filesMap := make(map[string]*AssetFile)

for _, path := range filePaths {
assetFile := NewAssetFile(path)
filesMap[path] = assetFile
}

return &Service{
mu: &sync.RWMutex{},
cache: filesMap,
}
}

func (f *Service) GetAssetFile(path string) *AssetFile {
f.mu.RLock()
defer f.mu.RUnlock()

return f.getFile(path)
}

func (f *Service) UpdateFile(file *AssetFile, newFileBaseName string) {
f.mu.RLock()
defer f.mu.RUnlock()

oldFileBaseName := filepath.Base(file.Path())

for path := range f.cache {
if strings.Contains(path, oldFileBaseName) {
newPath := strings.ReplaceAll(path, oldFileBaseName, newFileBaseName)
f.cache[path] = NewAssetFile(newPath)
}
}
}

func (f *Service) getFile(path string) *AssetFile {
if file, exists := f.cache[path]; exists {
return file
}

assetF := NewAssetFile(path)
f.cache[path] = assetF

return assetF
}
26 changes: 26 additions & 0 deletions file/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package file

const (
TypeAssetFolder = "asset_folder"
TypeAssetsFolder = "assets_folder"
TypeChainFolder = "chain_folder"
TypeChainInfoFolder = "chain_info_folder"
TypeChainsFolder = "chains_folder"
TypeDappsFolder = "dapps_folder"
TypeRootFolder = "root_folder"
TypeValidatorsFolder = "validators_folder"
TypeValidatorsAssetFolder = "validators_asset_folder"
TypeValidatorsAssetsFolder = "validators_assets_folder"

TypeAssetInfoFile = "asset_info_file"
TypeAssetLogoFile = "asset_logo_file"
TypeChainInfoFile = "chain_info_file"
TypeChainLogoFile = "chain_logo_file"
TypeTokenListFile = "chain_tokenlist_file"
TypeTokenListExtendedFile = "chain_tokenlist_extended_file"
TypeDappsLogoFile = "dapps_logo_file"
TypeValidatorsListFile = "validators_list_file"
TypeValidatorsLogoFile = "validators_logo_file"

TypeUnknown = "unknown_file"
)

0 comments on commit af3a665

Please sign in to comment.