From 55e9aa70181052c915a0480e6e26293dd7d8feb8 Mon Sep 17 00:00:00 2001 From: Quentin Date: Fri, 20 Aug 2021 08:02:36 +0200 Subject: [PATCH] Read-only is working --- main.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 0ad5668..6bf7f7a 100644 --- a/main.go +++ b/main.go @@ -29,12 +29,20 @@ type garageCtx struct { StatCache map[string]*GarageStat } +func EnvOrDefault(key, def string) string { + if val, ok := os.LookupEnv(key); ok { + return val + } + return def +} + func main() { - pathPrefix := "/webdav" - UserBaseDN := "ou=users,dc=deuxfleurs,dc=fr" - UserNameAttr := "cn" - Endpoint := "garage.deuxfleurs.fr" - UseSSL := true + pathPrefix := EnvOrDefault("BAGAGE_WEBDAV_PREFIX", "/webdav") + LdapServer := EnvOrDefault("BAGAGE_LDAP_ENDPOINT", "127.0.0.1:1389") + UserBaseDN := EnvOrDefault("BAGAGE_LDAP_USER_BASE_DN", "ou=users,dc=deuxfleurs,dc=fr") + UserNameAttr := EnvOrDefault("BAGAGE_LDAP_USERNAME_ATTR", "cn") + Endpoint := EnvOrDefault("BAGAGE_S3_ENDPOINT", "garage.deuxfleurs.fr") + UseSSL := EnvOrDefault("BAGAGE_S3_SSL", "true") == "true" srv := &webdav.Handler{ Prefix: pathPrefix, @@ -54,7 +62,7 @@ func main() { return } - ldapSock, err := ldap.Dial("tcp", "127.0.0.1:1389") + ldapSock, err := ldap.Dial("tcp", LdapServer) if err != nil { log.Println(err) InternalError(w, r) @@ -177,6 +185,8 @@ func (s *GarageFS) Stat(ctx context.Context, name string) (os.FileInfo, error) { type GarageFile struct { ctx context.Context mc *minio.Client + obj *minio.Object + stat *GarageStat path string } @@ -185,15 +195,44 @@ func NewGarageFile(ctx context.Context, path string) (webdav.File, error) { gf.ctx = ctx gf.mc = ctx.Value(garageEntry).(garageCtx).MC gf.path = path + stat, err := NewGarageStat(ctx, path) + if err != nil { + return nil, err + } + gf.stat = stat return gf, nil } func (gf *GarageFile) Close() error { - return errors.New("not implemented Close") + if gf.obj == nil { + return nil + } + err := gf.obj.Close() + gf.obj = nil + return err +} + +func (gf *GarageFile) loadObject() error { + if gf.obj == nil { + log.Println("Called GetObject on", gf.path) + obj, err := gf.mc.GetObject(gf.ctx, gf.stat.bucket, gf.stat.obj.Key, minio.GetObjectOptions{}) + if err != nil { + return err + } + gf.obj = obj + } + return nil } func (gf *GarageFile) Read(p []byte) (n int, err error) { - return 0, errors.New("not implemented Read") + if gf.stat.Mode().IsDir() { + return 0, os.ErrInvalid + } + if err := gf.loadObject(); err != nil { + return 0, err + } + + return gf.obj.Read(p) } func (gf *GarageFile) Write(p []byte) (n int, err error) { @@ -201,7 +240,10 @@ func (gf *GarageFile) Write(p []byte) (n int, err error) { } func (gf *GarageFile) Seek(offset int64, whence int) (int64, error) { - return 0, errors.New("not implemented Seek") + if err := gf.loadObject(); err != nil { + return 0, err + } + return gf.obj.Seek(offset, whence) } /*