-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_manager.go
168 lines (135 loc) · 3.04 KB
/
file_manager.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package blimpy
import (
"crypto/sha512"
"database/sql"
"encoding/hex"
"errors"
"github.com/coopernurse/gorp"
_ "github.com/mattn/go-sqlite3"
"io"
"os"
"path/filepath"
"syscall"
)
const (
dirNames = "abcdefghijklmnopqrstuvwxyz0123456789"
fileDatabaseName = "blimpy_files.sqlite3"
)
func makeFilePath(root, id string) string {
return filepath.Join(root, id[0:1], id[1:2], id)
}
type FileManager interface {
GetRoot() string
SetRoot(root string) error
GetFile(id string) (*File, error)
DeleteFile(id string) error
InsertFile(file *File) error
UpdateFile(file *File) error
}
type FSFileManager struct {
root string
dbHandle *sql.DB
dbMap *gorp.DbMap
}
func NewFSFileManager(root string) (*FSFileManager, error) {
self := FSFileManager{}
err := self.SetRoot(root)
if err != nil {
return nil, err
}
return &self, nil
}
func (self *FSFileManager) GetRoot() string {
return self.root
}
func (self *FSFileManager) SetRoot(root string) error {
if self.dbHandle != nil {
if err := self.dbHandle.Close(); err != nil {
return err
}
}
db, err := sql.Open("sqlite3", filepath.Join(root, fileDatabaseName))
if err != nil {
return err
}
self.root = root
self.dbHandle = db
self.dbMap = &gorp.DbMap{
Db: self.dbHandle,
Dialect: gorp.SqliteDialect{},
}
self.dbMap.AddTableWithName(File{}, "files").SetKeys(false, "Id")
err = self.dbMap.CreateTablesIfNotExists()
if err != nil {
return err
}
err = self.ensureStorageDirectories()
if err != nil {
return err
}
return nil
}
func (self *FSFileManager) ensureStorageDirectories() error {
for _, s := range dirNames {
err := os.Mkdir(filepath.Join(self.root, string(s)), os.FileMode(0755))
if err != nil && !os.IsExist(err) {
return err
}
for _, s2 := range dirNames {
err := os.Mkdir(filepath.Join(self.root, string(s), string(s2)), os.FileMode(0755))
if err != nil && !os.IsExist(err) {
return err
}
}
}
return nil
}
func (self *FSFileManager) InsertFile(file *File, fd *os.File) error {
hash := sha512.New()
_, err := io.Copy(hash, fd)
if err != nil {
return err
}
_, err = fd.Seek(0, 0)
if err != nil {
return err
}
file.Id = hex.EncodeToString(hash.Sum(nil))
file.path = makeFilePath(self.root, file.Id)
err = file.Open()
if err != nil {
return err
}
io.Copy(file, fd)
_, err = file.Seek(0, 0)
if err != nil {
return err
}
return self.dbMap.Insert(file)
}
func (self *FSFileManager) GetFile(id string) (*File, error) {
var file File
err := self.dbMap.SelectOne(&file, "select * from files where id = ?", id)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
} else {
return nil, err
}
}
file.path = makeFilePath(self.root, id)
return &file, nil
}
func (self *FSFileManager) UpdateFile(file *File) error {
return errors.New("not implemented")
}
func (self *FSFileManager) DeleteFile(id string) error {
_, err := self.dbMap.Exec("delete from files where id = ?", id)
if err != nil {
return err
}
err = syscall.Unlink(makeFilePath(self.root, id))
if err != nil {
return err
}
}