From d2730014f963777a93446bc1f83e8e1a19893f36 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Sun, 25 Oct 2020 22:09:38 +0530 Subject: [PATCH] MM-29525: Strip path_prefix from ListDirectory (#15949) * MM-29525: Strip path_prefix from ListDirectory An AWS path prefix is meant to be an implementation detail which the calling application should not be aware of. Hence, we should strip the path prefix when returning objects in a directory because they anyways get applied while using the other APIs https://mattermost.atlassian.net/browse/MM-29525 * simplify Co-authored-by: Mattermod --- services/filesstore/s3store.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/services/filesstore/s3store.go b/services/filesstore/s3store.go index 0868da5859..f0348bda65 100644 --- a/services/filesstore/s3store.go +++ b/services/filesstore/s3store.go @@ -350,7 +350,13 @@ func (b *S3FileBackend) ListDirectory(path string) (*[]string, *model.AppError) if object.Err != nil { return nil, model.NewAppError("ListDirectory", "utils.file.list_directory.s3.app_error", nil, object.Err.Error(), http.StatusInternalServerError) } - paths = append(paths, strings.Trim(object.Key, "/")) + // We strip the path prefix that gets applied, + // so that it remains transparent to the application. + object.Key = strings.TrimPrefix(object.Key, b.pathPrefix) + trimmed := strings.Trim(object.Key, "/") + if trimmed != "" { + paths = append(paths, trimmed) + } } return &paths, nil