-
Notifications
You must be signed in to change notification settings - Fork 19
/
file_manager.go
50 lines (41 loc) · 1.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
package attachment
import (
"path/filepath"
"strconv"
"time"
)
type FileManager interface {
Convert(string, string) error
SetFilename(string)
ToJson() map[string]interface{}
}
type FileBaseManager struct {
Dir *DirManager
Version string
Filename string
}
// Return FileManager for given base mime and version.
func NewFileManager(dm *DirManager, mime_base, version string) FileManager {
fbm := &FileBaseManager{Dir: dm, Version: version}
switch mime_base {
case "image":
return &FileImageManager{FileBaseManager: fbm}
default:
return &FileDefaultManager{FileBaseManager: fbm}
}
return nil
}
func (fbm *FileBaseManager) SetFilename(ext string) {
salt := strconv.FormatInt(seconds(), 36)
fbm.Filename = fbm.Version + "-" + salt + ext
}
func (fbm *FileBaseManager) Filepath() string {
return filepath.Join(fbm.Dir.Abs(), fbm.Filename)
}
func (fbm *FileBaseManager) Url() string {
return filepath.Join(fbm.Dir.Path, fbm.Filename)
}
func seconds() int64 {
t := time.Now()
return int64(t.Hour()*3600 + t.Minute()*60 + t.Second())
}