-
Notifications
You must be signed in to change notification settings - Fork 2
/
file.go
135 lines (109 loc) · 2.31 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package kvs
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"sync"
"github.com/mholt/archiver/v3"
)
var (
// DefaultTempDir creates temp dir.
DefaultTempDir = createTempDir()
// DefaultMaxSize for data size.
DefaultMaxSize int64 = 64 * 1024 * 1024
)
func createTempDir() string {
dir, _ := os.MkdirTemp("", "dewy-")
return dir
}
// File struct.
type File struct {
items map[string]*item //nolint
dir string
mutex sync.Mutex //nolint
MaxItems int
MaxSize int64
}
// GetDir returns dir.
func (f *File) GetDir() string {
return f.dir
}
// Default sets to struct.
func (f *File) Default() {
f.dir = DefaultTempDir
f.MaxSize = DefaultMaxSize
}
// Read data by key from file.
func (f *File) Read(key string) ([]byte, error) {
p := filepath.Join(f.dir, key)
if !IsFileExist(p) {
return nil, fmt.Errorf("File not found: %s", p)
}
content, err := os.ReadFile(p)
if err != nil {
return nil, err
}
return content, nil
}
// Write data to file.
func (f *File) Write(key string, data []byte) error {
dirstat, err := os.Stat(f.dir)
if err != nil {
return err
}
if !dirstat.Mode().IsDir() {
return errors.New("File.dir is not dir")
}
if dirstat.Size() > f.MaxSize {
return errors.New("Max size has been reached")
}
p := filepath.Join(f.dir, key)
file, err := os.OpenFile(p, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(data)
if err != nil {
return err
}
log.Printf("[INFO] Write file to %s", p)
return nil
}
// Delete data on file.
func (f *File) Delete(key string) error {
p := filepath.Join(f.dir, key)
if !IsFileExist(p) {
return fmt.Errorf("File not found: %s", p)
}
if err := os.Remove(p); err != nil {
return err
}
return nil
}
// List returns keys from file.
func (f *File) List() ([]string, error) {
files, err := os.ReadDir(f.dir)
if err != nil {
return nil, err
}
var list []string
for _, file := range files {
list = append(list, file.Name())
}
return list, nil
}
// ExtractArchive extracts by archive.
func ExtractArchive(src, dst string) error {
if !IsFileExist(src) {
return fmt.Errorf("File not found: %s", src)
}
return archiver.Unarchive(src, dst)
}
// IsFileExist checks file exists.
func IsFileExist(p string) bool {
_, err := os.Stat(p)
return !os.IsNotExist(err)
}