/
file.go
81 lines (66 loc) · 1.4 KB
/
file.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package input
import (
"io"
"os"
"path/filepath"
"strings"
)
// NewFileInput creates an instance of Input representing a file input.
func NewFileInput(name, stem string) Input {
var fi os.FileInfo
if len(name) > 0 {
fi, _ = os.Stat(name)
}
if len(stem) == 0 && fi != nil {
b := filepath.Base(fi.Name())
stem = strings.TrimSuffix(b, filepath.Ext(b))
}
return &file{fi, stem, createChecksums(name)}
}
type file struct {
fileInfo os.FileInfo
stem string
checksumms *Hash
}
// Name returns the files name without its path.
func (i *file) Name() string {
if i.fileInfo == nil {
return ""
}
return i.fileInfo.Name()
}
// Path returns the files path including the file name.
func (i *file) Path() string {
if i.fileInfo == nil {
return ""
}
p, _ := filepath.Abs(i.fileInfo.Name())
return p
}
// Path returns the inputs stem.
// By default, this is files name without the extension or whatever was
// set by the output option.
func (i *file) Stem() string {
return i.stem
}
func (i *file) Size() int64 {
if i.fileInfo == nil {
return -1
}
return i.fileInfo.Size()
}
// Checksums returns a set containing the files checksums calculated
// with several algorithms.
func (i *file) Checksums() *Hash {
return i.checksumms
}
func createChecksums(name string) *Hash {
cs := NewHash()
defer cs.Close()
f, _ := os.Open(name)
defer f.Close()
if f != nil {
io.Copy(cs, f)
}
return cs
}