Skip to content

Commit

Permalink
refactor: cleanup storage interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Dec 27, 2023
1 parent 8813829 commit 4fe6958
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
21 changes: 6 additions & 15 deletions http/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,14 @@ func (s *httpServer) handleDownload(ctx *fiber.Ctx) error {
}

// open file for stats and eventually reading
file, err := s.storage.OpenFile(path, user)
file, stat, err := s.storage.OpenFile(path, user)
if err != nil {
return err
}

fileInfo, err := file.Stat()
if err != nil {
_ = file.Close()
return err
}

if fileInfo.IsDir() {
// we don't need the file anymore
_ = file.Close()

if stat.IsDir() {
// fix root archive name
name := fileInfo.Name()
name := stat.Name()
if name == "." {
name = "files"
}
Expand All @@ -133,13 +124,13 @@ func (s *httpServer) handleDownload(ctx *fiber.Ctx) error {

return compressFolderToArchive(s.storage, user, path, ctx)
} else {
ctx.Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", strconv.Quote(fileInfo.Name())))
ctx.Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", strconv.Quote(stat.Name())))

if fileInfo.Size() >= math.MaxInt {
if stat.Size() >= math.MaxInt {
// download file chunked
return ctx.SendStream(file)
} else {
return ctx.SendStream(file, int(fileInfo.Size()))
return ctx.SendStream(file, int(stat.Size()))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion http/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func compressFolderToArchive(storage fileshare.AuthenticatedStorageProvider, use
return err
}

file, err := storage.OpenFile(header.Name, user)
file, _, err := storage.OpenFile(header.Name, user)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ type PathACL struct {

type StorageProvider interface {
CreateFile(name string) (io.WriteCloser, error)
OpenFile(name string) (fs.File, error)
OpenFile(name string) (io.ReadCloser, fs.FileInfo, error)
ReadDir(name string) ([]fs.DirEntry, error)
}

type AuthenticatedStorageProvider interface {
CreateFile(name string, user *User) (io.WriteCloser, error)
OpenFile(name string, user *User) (fs.File, error)
OpenFile(name string, user *User) (io.ReadCloser, fs.FileInfo, error)
ReadDir(name string, user *User) ([]fs.DirEntry, error)
CanWrite(name string, user *User) bool
}
4 changes: 2 additions & 2 deletions storage/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ func (p *aclStorageProvider) CreateFile(name string, user *fileshare.User) (io.W
return p.underlying.CreateFile(name)
}

func (p *aclStorageProvider) OpenFile(name string, user *fileshare.User) (fs.File, error) {
func (p *aclStorageProvider) OpenFile(name string, user *fileshare.User) (io.ReadCloser, fs.FileInfo, error) {
if user.Admin {
return p.underlying.OpenFile(name)
}

read, _ := p.evalACL(name, user)
if !read {
return nil, fileshare.NewError("cannot read file", fileshare.ErrStorageReadForbidden, fmt.Errorf("user %s is not allowed to read from %s", user.Nickname, name))
return nil, nil, fileshare.NewError("cannot read file", fileshare.ErrStorageReadForbidden, fmt.Errorf("user %s is not allowed to read from %s", user.Nickname, name))
}

return p.underlying.OpenFile(name)
Expand Down
20 changes: 18 additions & 2 deletions storage/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,25 @@ func (p *localStorageProvider) CreateFile(name string) (io.WriteCloser, error) {
return os.Create(path)
}

func (p *localStorageProvider) OpenFile(name string) (fs.File, error) {
func (p *localStorageProvider) OpenFile(name string) (io.ReadCloser, fs.FileInfo, error) {
path := filepath.Join(p.base, filepath.Clean("/"+name))
return os.Open(path)

file, err := os.Open(path)
if err != nil {
return nil, nil, err
}

fileInfo, err := file.Stat()
if err != nil {
return nil, nil, err
}

if fileInfo.IsDir() {
_ = file.Close()
return nil, fileInfo, nil
} else {
return file, fileInfo, nil
}
}

func (p *localStorageProvider) ReadDir(name string) ([]fs.DirEntry, error) {
Expand Down

0 comments on commit 4fe6958

Please sign in to comment.