From 1d46bb2bccf69ebf81585d821467fdded6fd36fb Mon Sep 17 00:00:00 2001 From: Sylvain Baubeau Date: Thu, 4 Jun 2015 10:10:21 +0200 Subject: [PATCH] Create full folder hierarchy instead of just the top level folder Signed-off-by: Sylvain Baubeau --- docs/storage/driver/swift/swift.go | 31 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/docs/storage/driver/swift/swift.go b/docs/storage/driver/swift/swift.go index 213dfc29..38e87239 100644 --- a/docs/storage/driver/swift/swift.go +++ b/docs/storage/driver/swift/swift.go @@ -178,8 +178,8 @@ func (d *driver) GetContent(ctx context.Context, path string) ([]byte, error) { // PutContent stores the []byte content at a location designated by "path". func (d *driver) PutContent(ctx context.Context, path string, contents []byte) error { - if dir, err := d.createParentFolder(path); err != nil { - return parseError(dir, err) + if err := d.createParentFolders(path); err != nil { + return err } err := d.Conn.ObjectPutBytes(d.Container, d.swiftPath(path), contents, d.getContentType()) @@ -241,8 +241,8 @@ func (d *driver) WriteStream(ctx context.Context, path string, offset int64, rea if err != nil { if swiftErr, ok := err.(*swift.Error); ok && swiftErr.StatusCode == 404 { // Create a object manifest - if dir, err := d.createParentFolder(path); err != nil { - return bytesRead, parseError(dir, err) + if err := d.createParentFolders(path); err != nil { + return bytesRead, err } manifest, err := d.createManifest(path) if err != nil { @@ -455,22 +455,21 @@ func (d *driver) swiftPath(path string) string { return strings.TrimLeft(strings.TrimRight(d.Prefix, "/")+path, "/") } -func (d *driver) createParentFolder(path string) (string, error) { +func (d *driver) createParentFolders(path string) error { dir := gopath.Dir(path) - if dir == "/" { - return dir, nil - } - - _, _, err := d.Conn.Object(d.Container, d.swiftPath(dir)) - if swiftErr, ok := err.(*swift.Error); ok && swiftErr.StatusCode == 404 { - _, err := d.Conn.ObjectPut(d.Container, d.swiftPath(dir), bytes.NewReader(make([]byte, 0)), - false, "", directoryMimeType, nil) - if err != nil { - return dir, err + for dir != "/" { + _, _, err := d.Conn.Object(d.Container, d.swiftPath(dir)) + if swiftErr, ok := err.(*swift.Error); ok && swiftErr.StatusCode == 404 { + _, err := d.Conn.ObjectPut(d.Container, d.swiftPath(dir), bytes.NewReader(make([]byte, 0)), + false, "", directoryMimeType, nil) + if err != nil { + return parseError(dir, err) + } } + dir = gopath.Dir(dir) } - return dir, nil + return nil } func (d *driver) getContentType() string {