Skip to content

Commit

Permalink
优化逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Dec 10, 2024
1 parent 8d13ab5 commit 6b439eb
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 208 deletions.
4 changes: 2 additions & 2 deletions filesystem/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (this *Adapter) Has(string) bool {
}

// 上传
func (this *Adapter) Write(path string, contents string, conf interfaces.Config) (map[string]any, error) {
func (this *Adapter) Write(path string, contents []byte, conf interfaces.Config) (map[string]any, error) {
panic("go-filesystem: Write does not implement")
}

Expand All @@ -32,7 +32,7 @@ func (this *Adapter) WriteStream(path string, stream io.Reader, conf interfaces.
}

// 更新
func (this *Adapter) Update(path string, contents string, conf interfaces.Config) (map[string]any, error) {
func (this *Adapter) Update(path string, contents []byte, conf interfaces.Config) (map[string]any, error) {
panic("go-filesystem: Update does not implement")
}

Expand Down
45 changes: 22 additions & 23 deletions filesystem/adapter/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"net/http"
"path/filepath"

"github.com/deatil/go-filesystem/filesystem/interfaces"
"github.com/deatil/go-filesystem/filesystem/adapter"
"github.com/deatil/go-filesystem/filesystem/interfaces"
)

// 权限列表
Expand Down Expand Up @@ -72,7 +72,7 @@ func (this *Local) Has(path string) bool {
}

// 上传
func (this *Local) Write(path string, contents string, conf interfaces.Config) (map[string]any, error) {
func (this *Local) Write(path string, contents []byte, conf interfaces.Config) (map[string]any, error) {
location := this.ApplyPathPrefix(path)
this.EnsureDirectory(filepath.Dir(location))

Expand All @@ -83,9 +83,9 @@ func (this *Local) Write(path string, contents string, conf interfaces.Config) (

defer out.Close()

_, writeErr := out.WriteString(contents)
_, writeErr := out.Write(contents)
if writeErr != nil {
return nil, errors.New("go-filesystem: exec os.WriteString() fail, error: " + writeErr.Error())
return nil, errors.New("go-filesystem: exec os.Write() fail, error: " + writeErr.Error())
}

size, sizeErr := this.FileSize(location)
Expand Down Expand Up @@ -139,7 +139,7 @@ func (this *Local) WriteStream(path string, stream io.Reader, conf interfaces.Co
}

// 更新
func (this *Local) Update(path string, contents string, conf interfaces.Config) (map[string]any, error) {
func (this *Local) Update(path string, contents []byte, conf interfaces.Config) (map[string]any, error) {
location := this.ApplyPathPrefix(path)

out, createErr := os.Create(location)
Expand All @@ -149,9 +149,9 @@ func (this *Local) Update(path string, contents string, conf interfaces.Config)

defer out.Close()

_, writeErr := out.WriteString(contents)
_, writeErr := out.Write(contents)
if writeErr != nil {
return nil, errors.New("go-filesystem: exec os.WriteString() fail, error: " + writeErr.Error())
return nil, errors.New("go-filesystem: exec os.Write() fail, error: " + writeErr.Error())
}

size, sizeErr := this.FileSize(location)
Expand Down Expand Up @@ -183,19 +183,17 @@ func (this *Local) UpdateStream(path string, stream io.Reader, config interfaces
func (this *Local) Read(path string) (map[string]any, error) {
location := this.ApplyPathPrefix(path)

file, openErr := os.Open(location)
if openErr != nil {
return nil, errors.New("go-filesystem: exec os.Open() fail, error: " + openErr.Error())
file, err := os.Open(location)
if err != nil {
return nil, errors.New("go-filesystem: exec os.Open() fail, error: " + err.Error())
}
defer file.Close()

data, readAllErr := io.ReadAll(file)
if readAllErr != nil {
return nil, errors.New("go-filesystem: exec io.ReadAll() fail, error: " + readAllErr.Error())
contents, err := io.ReadAll(file)
if err != nil {
return nil, errors.New("go-filesystem: exec io.ReadAll() fail, error: " + err.Error())
}

contents := fmt.Sprintf("%s", data)

return map[string]any{
"type": "file",
"path": path,
Expand Down Expand Up @@ -227,6 +225,7 @@ func (this *Local) Rename(path string, newpath string) error {
location := this.ApplyPathPrefix(path)
destination := this.ApplyPathPrefix(newpath)
parentDirectory := this.ApplyPathPrefix(filepath.Dir(newpath))

this.EnsureDirectory(parentDirectory)

err := os.Rename(location, destination)
Expand Down Expand Up @@ -624,14 +623,6 @@ func (this *Local) FileMode(fp string) (uint32, error) {
return uint32(perm), nil
}

// 权限格式化
// Format Perm
func (this *Local) FormatPerm(i uint32) os.FileMode {
// 八进制转成十进制
// p, _ := strconv.ParseInt(strconv.Itoa(i), 8, 0)
return os.FileMode(i)
}

// 软链接
// Symlink
func (this *Local) Symlink(target, link string) error {
Expand All @@ -649,3 +640,11 @@ func (this *Local) Readlink(link string) (string, error) {
func (this *Local) IsSymlink(m os.FileMode) bool {
return m&os.ModeSymlink != 0
}

// 权限格式化
// Format Perm
func (this *Local) FormatPerm(i uint32) os.FileMode {
// 八进制转成十进制
// p, _ := strconv.ParseInt(strconv.Itoa(i), 8, 0)
return os.FileMode(i)
}
21 changes: 8 additions & 13 deletions filesystem/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import(
"github.com/deatil/go-filesystem/filesystem/interfaces"
)

type (
// 配置 map
DataMap = map[string]any
)

/**
* 配置
*
Expand All @@ -17,23 +12,23 @@ type (
*/
type Config struct {
// 数据
Data DataMap
data map[string]any
}

/**
* 构造函数
*/
func New(data DataMap) Config {
func New(data map[string]any) Config {
return Config{
Data: data,
data: data,
}
}

/**
* 覆盖旧数据
*/
func (this Config) With(data DataMap) interfaces.Config {
this.Data = data
func (this Config) With(data map[string]any) interfaces.Config {
this.data = data

return this
}
Expand All @@ -42,7 +37,7 @@ func (this Config) With(data DataMap) interfaces.Config {
* 设置单个新数据
*/
func (this Config) Set(key string, value any) interfaces.Config {
this.Data[key] = value
this.data[key] = value

return this
}
Expand All @@ -51,7 +46,7 @@ func (this Config) Set(key string, value any) interfaces.Config {
* 是否存在
*/
func (this Config) Has(key string) bool {
if _, ok := this.Data[key]; ok {
if _, ok := this.data[key]; ok {
return true
}

Expand All @@ -62,7 +57,7 @@ func (this Config) Has(key string) bool {
* 获取一个带默认的值
*/
func (this Config) Get(key string, defaults ...any) any {
if data, ok := this.Data[key]; ok {
if data, ok := this.data[key]; ok {
return data
}

Expand Down
4 changes: 2 additions & 2 deletions filesystem/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func NewDirectory(filesystem *Filesystem, path ...string) *Directory {
}

// 设置管理器
func (this *Directory) SetFilesystem(filesystem *Filesystem) *Directory {
func (this *Directory) WithFilesystem(filesystem *Filesystem) *Directory {
this.filesystem = filesystem

return this
}

// 设置目录
func (this *Directory) SetPath(path string) *Directory {
func (this *Directory) WithPath(path string) *Directory {
this.path = path

return this
Expand Down
28 changes: 14 additions & 14 deletions filesystem/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ func NewFile(filesystem *Filesystem, path ...string) *File {
}

// 设置管理器
func (this *File) SetFilesystem(filesystem *Filesystem) *File {
func (this *File) WithFilesystem(filesystem *Filesystem) *File {
this.filesystem = filesystem

return this
}

// 设置目录
func (this *File) SetPath(path string) *File {
func (this *File) WithPath(path string) *File {
this.path = path

return this
Expand All @@ -47,7 +47,7 @@ func (this *File) Exists() bool {
}

// 读取
func (this *File) Read() (string, error) {
func (this *File) Read() ([]byte, error) {
return this.filesystem.Read(this.path)
}

Expand All @@ -56,8 +56,8 @@ func (this *File) ReadStream() (*os.File, error) {
return this.filesystem.ReadStream(this.path)
}

// 写入字符
func (this *File) Write(content string) (bool, error) {
// 写入字节
func (this *File) Write(content []byte) (bool, error) {
return this.filesystem.Write(this.path, content)
}

Expand All @@ -66,8 +66,8 @@ func (this *File) WriteStream(resource io.Reader) (bool, error) {
return this.filesystem.WriteStream(this.path, resource)
}

// 更新字符
func (this *File) Update(content string) (bool, error) {
// 更新字节
func (this *File) Update(content []byte) (bool, error) {
return this.filesystem.Update(this.path, content)
}

Expand All @@ -76,8 +76,8 @@ func (this *File) UpdateStream(resource io.Reader) (bool, error) {
return this.filesystem.UpdateStream(this.path, resource)
}

// 导入字符
func (this *File) Put(content string) (bool, error) {
// 导入字节
func (this *File) Put(content []byte) (bool, error) {
return this.filesystem.Update(this.path, content)
}

Expand Down Expand Up @@ -110,6 +110,11 @@ func (this *File) Copy(newpath string) (*File, error) {
return nil, err
}

// 删除
func (this *File) Delete() (bool, error) {
return this.filesystem.Delete(this.path)
}

// 时间戳
func (this *File) GetTimestamp() (int64, error) {
return this.filesystem.GetTimestamp(this.path)
Expand All @@ -134,8 +139,3 @@ func (this *File) GetMetadata() (map[string]any, error) {
func (this *File) GetSize() (int64, error) {
return this.filesystem.GetSize(this.path)
}

// 删除
func (this *File) Delete() (bool, error) {
return this.filesystem.Delete(this.path)
}
Loading

0 comments on commit 6b439eb

Please sign in to comment.