-
Notifications
You must be signed in to change notification settings - Fork 24
/
fileinfo.go
66 lines (56 loc) · 1.78 KB
/
fileinfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package filesystem
import (
"fmt"
"path/filepath"
"strings"
)
// FileInfo holds fields used for internal tracking of files, events in notifications and so on.
type FileInfo struct {
directory string
name string
ext string
}
// NewFileInfo creates a new instance of FileInfo.
func NewFileInfo(directory, name, ext string) *FileInfo {
fileInfo := &FileInfo{directory: filepath.Clean(directory), name: name, ext: ext}
return fileInfo
}
// String returns a string representation of the FileInfo.
func (fileInfo *FileInfo) String() string {
return fmt.Sprintf("FileInfo<"+
"Name: %s "+
"| Ext: %s "+
"| Directory: %s"+
">",
fileInfo.name,
fileInfo.ext,
fileInfo.directory)
}
// ParseFilePath parses a filesystem path into a FileInfo.
func ParseFilePath(fpath string) *FileInfo {
path := filepath.Clean(fpath)
ext := filepath.Ext(path)
name := strings.TrimSuffix(filepath.Base(path), ext)
directory := filepath.Dir(path)
return NewFileInfo(directory, name, ext)
}
// GetDirectory returns the directory from FileInfo.
func (fileInfo *FileInfo) GetDirectory() string {
return fileInfo.directory
}
// GetFileName returns the file name from FileInfo.
func (fileInfo *FileInfo) GetFileName() string {
return fileInfo.name
}
// GetFileExt returns the file extension from FileInfo.
func (fileInfo *FileInfo) GetFileExt() string {
return fileInfo.ext
}
// GetFilePath returns the full file path from FileInfo, i.e., "directory/filename+extension".
func (fileInfo *FileInfo) GetFilePath() string {
return filepath.Join(fileInfo.directory, fileInfo.name+fileInfo.ext)
}
// GetFilePathWithoutExt returns the full file path from FileInfo, i.e., "directory/filename".
func (fileInfo *FileInfo) GetFilePathWithoutExt() string {
return filepath.Join(fileInfo.directory, fileInfo.name)
}