🏷️ Created File domain
This commit is contained in:
parent
ea040738b4
commit
44e00a35cb
|
@ -0,0 +1,102 @@
|
|||
package domain
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
type File struct {
|
||||
language language.Tag
|
||||
baseFileName string
|
||||
contentBaseName string
|
||||
dir string
|
||||
ext string
|
||||
filename string
|
||||
logicalName string
|
||||
path string
|
||||
translationBaseName string
|
||||
uniqueId string
|
||||
}
|
||||
|
||||
func NewFile(path string) File {
|
||||
out := File{
|
||||
language: language.Tag{},
|
||||
baseFileName: "",
|
||||
contentBaseName: "",
|
||||
dir: filepath.Dir(path) + "/",
|
||||
ext: strings.TrimPrefix(filepath.Ext(path), "."),
|
||||
filename: path,
|
||||
logicalName: filepath.Base(path),
|
||||
path: path,
|
||||
translationBaseName: "",
|
||||
uniqueId: "",
|
||||
}
|
||||
out.path, _ = filepath.Abs(path)
|
||||
out.baseFileName = strings.TrimSuffix(out.logicalName, filepath.Ext(out.logicalName))
|
||||
|
||||
parts := strings.Split(out.baseFileName, ".")
|
||||
out.language = language.Make(parts[len(parts)-1])
|
||||
out.translationBaseName = strings.Join(parts[:len(parts)-1], ".")
|
||||
out.contentBaseName = out.translationBaseName
|
||||
|
||||
switch out.translationBaseName {
|
||||
default:
|
||||
out.contentBaseName = out.translationBaseName
|
||||
case "_index", "index":
|
||||
out.contentBaseName = filepath.Base(out.dir)
|
||||
}
|
||||
|
||||
hash := md5.New()
|
||||
_, _ = hash.Write([]byte(out.path))
|
||||
out.uniqueId = string(hash.Sum(nil))
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
// BaseFileName returns file name without extention.
|
||||
func (f File) BaseFileName() string {
|
||||
return f.baseFileName
|
||||
}
|
||||
|
||||
func (f File) ContentBaseName() string {
|
||||
return f.contentBaseName
|
||||
}
|
||||
|
||||
// Dir returns directory path.
|
||||
func (f File) Dir() string {
|
||||
return f.dir
|
||||
}
|
||||
|
||||
// Ext returns file extention.
|
||||
func (f File) Ext() string {
|
||||
return f.ext
|
||||
}
|
||||
|
||||
func (f File) Filename() string {
|
||||
return f.filename
|
||||
}
|
||||
|
||||
// Language returns language.Tag of current file based on his suffix before
|
||||
// extention.
|
||||
func (f File) Language() language.Tag {
|
||||
return f.language
|
||||
}
|
||||
|
||||
func (f File) LogicalName() string {
|
||||
return f.logicalName
|
||||
}
|
||||
|
||||
func (f File) Path() string {
|
||||
return f.path
|
||||
}
|
||||
|
||||
func (f File) TranslationBaseName() string {
|
||||
return f.translationBaseName
|
||||
}
|
||||
|
||||
func (f File) UniqueID() string {
|
||||
return f.uniqueId
|
||||
}
|
Loading…
Reference in New Issue