From 515f2f6caf270b3d030c8976a8f04d7110962b8c Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Fri, 10 Dec 2021 20:44:03 +0000 Subject: [PATCH] fix reading --- file/handler/files.go | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/file/handler/files.go b/file/handler/files.go index d6114de..36f344d 100644 --- a/file/handler/files.go +++ b/file/handler/files.go @@ -83,30 +83,29 @@ func (e *File) Read(ctx context.Context, req *file.ReadRequest, rsp *file.ReadRe path := filepath.Join("file", tenantId, req.Project, req.Path) - var opts []store.ReadOption - - if strings.HasSuffix(req.Path, "/") { - opts = append(opts, store.ReadPrefix()) - } - - records, err := store.Read(path, opts...) + records, err := store.Read(path) if err != nil { return err } - // filter the file - for _, rec := range records { - file := new(file.Record) - - if err := rec.Decode(file); err != nil { - continue - } - - // strip the tenant id - file.Project = strings.TrimPrefix(file.Project, tenantId+"/") - file.Path = strings.TrimPrefix(file.Path, filepath.Join(tenantId, req.Project)) + if len(records) == 0 { + return errors.NotFound("file.read", "file not found") } + // filter the file + rec := records[0] + file := new(file.Record) + + if err := rec.Decode(file); err != nil { + return err + } + + // strip the tenant id + file.Project = strings.TrimPrefix(file.Project, tenantId+"/") + file.Path = strings.TrimPrefix(file.Path, filepath.Join(tenantId, req.Project)) + + rsp.File = file + return nil }