1 Star 0 Fork 1

Mesh-CPN / FRLite-Go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
local.go 1015 Bytes
一键复制 编辑 原始数据 按行查看 历史
Lakrimo 提交于 2022-11-15 13:13 . add config and fix dao
package storage
import (
"bufio"
"io"
"os"
"path/filepath"
)
type LocalStorage struct {
pathPrefix string
}
type LocalStorageOption func(*LocalStorage)
func WithLocalPathPrefix(prefix string) LocalStorageOption {
return func(storage *LocalStorage) {
storage.pathPrefix = prefix
}
}
func NewLocal(opts ...LocalStorageOption) Storage {
l := &LocalStorage{}
for _, o := range opts {
o(l)
}
return l
}
func (l *LocalStorage) Save(fileName string, data []byte) (filePath string, err error) {
f, err := os.OpenFile(filepath.Join(l.pathPrefix, fileName), os.O_CREATE|os.O_RDWR, os.ModePerm)
if err != nil {
return "", err
}
defer f.Close()
w := bufio.NewWriter(f)
w.Write(data)
w.Flush()
return fileName, nil
}
func (l *LocalStorage) Download(fileName string) ([]byte, error) {
f, err := os.OpenFile(filepath.Join(l.pathPrefix, fileName), os.O_CREATE|os.O_RDWR, os.ModePerm)
if err != nil {
return nil, err
}
defer f.Close()
return io.ReadAll(f)
}
func (l *LocalStorage) Close() {}
1
https://gitee.com/mesh_cpn/mesh.cpn.face_rkg.git
git@gitee.com:mesh_cpn/mesh.cpn.face_rkg.git
mesh_cpn
mesh.cpn.face_rkg
FRLite-Go
68828315a9ce

搜索帮助