-
Notifications
You must be signed in to change notification settings - Fork 10
/
local.go
96 lines (81 loc) · 1.8 KB
/
local.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
package store
import (
"bytes"
"errors"
"io"
"io/ioutil"
"mime/multipart"
"os"
"path"
)
//原始文件保存目录名
const sourceDirName = "src"
type localStore struct {
}
func (s localStore) md5ToPath(md5Code string) (path string) {
var buf bytes.Buffer
buf.WriteString(imagePath)
buf.WriteByte(os.PathSeparator)
for _, item := range md5Code {
buf.WriteRune(item)
buf.WriteByte(os.PathSeparator)
}
return buf.String()
}
func (s localStore) getSrcPath(md5 string) string {
var buf bytes.Buffer
buf.WriteString(s.md5ToPath(md5))
buf.WriteString(sourceDirName)
buf.WriteByte(os.PathSeparator)
return buf.String()
}
func (s localStore) write(f multipart.File, md5 string, name string) error {
//创建目录
srcPath := s.getSrcPath(md5)
err := os.MkdirAll(srcPath, os.ModePerm)
if err != nil {
return err
}
//创建文件
destFile, err := os.Create(srcPath + name)
if err != nil {
return err
}
defer destFile.Close()
//此处必须Seek回起点,否则copy不到东西
_, err = f.Seek(0, 0)
if err != nil {
return err
}
//文件落地
_, err = io.Copy(destFile, f)
return nil
}
func (s localStore) getDirFirstFile(dir string) (string, error) {
//获取目录信息
files, err := ioutil.ReadDir(dir)
if err != nil {
return "", err
}
//查找第一个文件返回路径
for _, file := range files {
return dir + file.Name(), nil
}
return "", errors.New("目录中没有文件")
}
func (s localStore) read(md5Code string, fileName *string) ([]byte, error) {
//获取文件路径
filePath := s.getSrcPath(md5Code)
if *fileName == "" {
var err error
filePath, err = s.getDirFirstFile(filePath)
if err != nil {
return nil, err
}
*fileName = path.Base(filePath)
} else {
filePath = filePath + *fileName
}
//读取文件
return ioutil.ReadFile(filePath)
}