Add basic file stat & mimetype

This commit is contained in:
Dane Everitt 2019-04-07 13:47:14 -07:00
parent aef9521190
commit 91afa4d38e
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 50 additions and 0 deletions

1
go.mod
View File

@ -12,6 +12,7 @@ require (
github.com/docker/docker v0.0.0-20180422163414-57142e89befe
github.com/docker/go-connections v0.4.0
github.com/docker/go-units v0.3.3 // indirect
github.com/gabriel-vasile/mimetype v0.1.4 // indirect
github.com/gogo/protobuf v1.0.0 // indirect
github.com/google/go-cmp v0.2.0 // indirect
github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect

2
go.sum
View File

@ -18,6 +18,8 @@ github.com/docker/go-units v0.3.3 h1:Xk8S3Xj5sLGlG5g67hJmYMmUgXv5N4PhkjJHHqrwnTk
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gabriel-vasile/mimetype v0.1.4 h1:5mcsq3+DXypREUkW+1juhjeKmE/XnWgs+paHMJn7lf8=
github.com/gabriel-vasile/mimetype v0.1.4/go.mod h1:kMJbg3SlWZCsj4R73F1WDzbT9AyGCOVmUtIxxwO5pmI=
github.com/gogo/protobuf v1.0.0 h1:2jyBKDKU/8v3v2xVR2PtiWQviFUyiaGk2rpfyFT8rTM=
github.com/gogo/protobuf v1.0.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=

View File

@ -3,6 +3,7 @@ package server
import (
"bytes"
"errors"
"github.com/gabriel-vasile/mimetype"
"go.uber.org/zap"
"io"
"io/ioutil"
@ -186,4 +187,50 @@ func (fs *Filesystem) Readfile(p string) (io.Reader, error) {
}
return bytes.NewReader(b), nil
}
// Delete a file or folder from the system. If a folder location is passed in the
// folder and all of its contents are deleted.
func (fs *Filesystem) DeleteFile(p string) error {
cleaned, err := fs.SafePath(p)
if err != nil {
return err
}
return os.RemoveAll(cleaned)
}
// Defines the stat struct object.
type Stat struct {
Info *os.FileInfo
Mimetype string
}
// Stats a file or folder and returns the base stat object from go along with the
// MIME data that can be used for editing files.
func (fs *Filesystem) Stat(p string) (*Stat, error) {
cleaned, err := fs.SafePath(p)
if err != nil {
return nil, err
}
s, err := os.Stat(cleaned)
if err != nil {
return nil, err
}
var m = "inode/directory"
if !s.IsDir() {
m, _, err = mimetype.DetectFile(cleaned)
if err != nil {
return nil, err
}
}
st := &Stat{
Info: &s,
Mimetype: m,
}
return st, nil
}