#45498: create folder before upserting file

pull/45534/head
Artur Wierzbicki 3 years ago
parent dba4f69e2f
commit 29a5939148
  1. 48
      pkg/infra/filestorage/fs_integration_test.go
  2. 9
      pkg/infra/filestorage/wrapper.go

@ -348,6 +348,54 @@ func TestFsStorage(t *testing.T) {
createFolderCrudCases := func() []fsTestCase {
return []fsTestCase{
{
name: "recreating a folder after it was already created via upserting a file is a no-op",
steps: []interface{}{
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/aB/cD/eF/file.jpg",
Contents: &[]byte{},
},
},
queryListFolders{
input: queryListFoldersInput{
path: "/",
},
checks: [][]interface{}{
checks(fPath("/aB")),
checks(fPath("/aB/cD")),
checks(fPath("/aB/cD/eF")),
},
},
cmdCreateFolder{
path: "/ab/cd/ef",
},
queryListFolders{
input: queryListFoldersInput{
path: "/",
},
checks: [][]interface{}{
checks(fPath("/aB")),
checks(fPath("/aB/cD")),
checks(fPath("/aB/cD/eF")),
},
},
cmdCreateFolder{
path: "/ab/cd/ef/GH",
},
queryListFolders{
input: queryListFoldersInput{
path: "/",
},
checks: [][]interface{}{
checks(fPath("/aB")),
checks(fPath("/aB/cD")),
checks(fPath("/aB/cD/eF")),
checks(fPath("/aB/cD/eF/GH")),
},
},
},
},
{
name: "creating a folder with the same name or same name but different casing is a no-op",
steps: []interface{}{

@ -35,6 +35,9 @@ func getParentFolderPath(path string) string {
split := strings.Split(path, Delimiter)
splitWithoutLastPart := split[:len(split)-1]
if len(splitWithoutLastPart) == 1 && split[0] == "" {
return Delimiter
}
return strings.Join(splitWithoutLastPart, Delimiter)
}
@ -127,6 +130,12 @@ func (b wrapper) Upsert(ctx context.Context, file *UpsertFileCommand) error {
return nil
}
path := getParentFolderPath(file.Path)
b.log.Info("Creating folder before upserting file", "file", file.Path, "folder", path)
if err := b.CreateFolder(ctx, path); err != nil {
return err
}
if file.Contents != nil && file.MimeType == "" {
file.MimeType = detectContentType(file.Path, "")
}

Loading…
Cancel
Save