Handle missing trailing slashes + Add Dockerfile

This commit is contained in:
Quentin 2021-08-20 17:28:10 +02:00
parent 36cef67f6b
commit 1cc0de75a6
2 changed files with 29 additions and 5 deletions

12
Dockerfile Normal file
View file

@ -0,0 +1,12 @@
FROM golang:1.17.0-alpine3.14 as builder
ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64
RUN apk update && apk add --no-cache ca-certificates && update-ca-certificates
WORKDIR /opt
COPY main.go go.mod go.sum /opt/
RUN go build .
FROM scratch
COPY --from=builder /opt/bagage /
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
USER 1000:1000
ENTRYPOINT ["/bagage"]

22
main.go
View file

@ -39,6 +39,8 @@ func EnvOrDefault(key, def string) string {
}
func main() {
log.Println("=== Starting Bagage ===")
HttpListen := EnvOrDefault("BAGAGE_HTTP_LISTEN", ":8080")
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")
@ -132,7 +134,7 @@ func main() {
return
})
if err := http.ListenAndServe(":8080", nil); err != nil {
if err := http.ListenAndServe(HttpListen, nil); err != nil {
log.Fatalf("Error with WebDAV server: %v", err)
}
}
@ -171,6 +173,7 @@ func (s *GarageFS) Mkdir(ctx context.Context, name string, perm os.FileMode) err
}
func (s *GarageFS) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) {
log.Println("Stat from GarageFS.OpenFile()", name)
NewGarageStatFromFile(ctx, name)
return NewGarageFile(ctx, name)
}
@ -184,7 +187,7 @@ func (s *GarageFS) Rename(ctx context.Context, oldName, newName string) error {
}
func (s *GarageFS) Stat(ctx context.Context, name string) (os.FileInfo, error) {
log.Println("Stat from GarageFS")
log.Println("Stat from GarageFS.Stat()", name)
return NewGarageStat(ctx, name)
}
@ -319,6 +322,7 @@ func (gf *GarageFile) readDirRoot(count int) ([]fs.FileInfo, error) {
entries := make([]fs.FileInfo, 0, len(buckets))
for _, bucket := range buckets {
log.Println("Stat from GarageFile.readDirRoot()", "/"+bucket.Name)
ngf, err := NewGarageStat(gf.ctx, "/"+bucket.Name)
if err != nil {
return nil, err
@ -330,8 +334,13 @@ func (gf *GarageFile) readDirRoot(count int) ([]fs.FileInfo, error) {
}
func (gf *GarageFile) readDirChild(count int) ([]fs.FileInfo, error) {
prefix := gf.path.key
if prefix[len(prefix)-1:] != "/" {
prefix = prefix + "/"
}
objs_info := gf.mc.ListObjects(gf.ctx, gf.path.bucket, minio.ListObjectsOptions{
Prefix: gf.path.key,
Prefix: prefix,
Recursive: false,
})
@ -340,6 +349,7 @@ func (gf *GarageFile) readDirChild(count int) ([]fs.FileInfo, error) {
if object.Err != nil {
return nil, object.Err
}
log.Println("Stat from GarageFile.readDirChild()", path.Join("/", gf.path.bucket, object.Key))
ngf, err := NewGarageStatFromObjectInfo(gf.ctx, gf.path.bucket, object)
if err != nil {
return nil, err
@ -351,6 +361,7 @@ func (gf *GarageFile) readDirChild(count int) ([]fs.FileInfo, error) {
}
func (gf *GarageFile) Stat() (fs.FileInfo, error) {
log.Println("Stat from GarageFile.Stat()", gf.path.path)
return NewGarageStatFromFile(gf.ctx, gf.path.path)
}
@ -377,7 +388,9 @@ func NewGarageStatFromFile(ctx context.Context, path string) (*GarageStat, error
gs := new(GarageStat)
gs.ctx = ctx
gs.path = NewS3Path(path)
gs.path.class = OBJECT // known because called from GarageFile
if gs.path.class == OPAQUE_KEY {
gs.path.class = OBJECT // known because called from GarageFile
}
gs.obj.Key = gs.path.key
gs.obj.LastModified = time.Now()
@ -409,7 +422,6 @@ func NewGarageStatFromObjectInfo(ctx context.Context, bucket string, obj minio.O
* Stat a path without additional information
*/
func NewGarageStat(ctx context.Context, path string) (*GarageStat, error) {
log.Println("Probe file", path)
cache := ctx.Value(garageEntry).(garageCtx).StatCache
if entry, ok := cache[path]; ok {
return entry, nil