Create full folder hierarchy instead of just the top level folder

Signed-off-by: Sylvain Baubeau <sbaubeau@redhat.com>
This commit is contained in:
Sylvain Baubeau 2015-06-04 10:10:21 +02:00
parent 03188fe394
commit 1c01ca4b39

View file

@ -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". // PutContent stores the []byte content at a location designated by "path".
func (d *driver) PutContent(ctx context.Context, path string, contents []byte) error { func (d *driver) PutContent(ctx context.Context, path string, contents []byte) error {
if dir, err := d.createParentFolder(path); err != nil { if err := d.createParentFolders(path); err != nil {
return parseError(dir, err) return err
} }
err := d.Conn.ObjectPutBytes(d.Container, d.swiftPath(path), err := d.Conn.ObjectPutBytes(d.Container, d.swiftPath(path),
contents, d.getContentType()) contents, d.getContentType())
@ -241,8 +241,8 @@ func (d *driver) WriteStream(ctx context.Context, path string, offset int64, rea
if err != nil { if err != nil {
if swiftErr, ok := err.(*swift.Error); ok && swiftErr.StatusCode == 404 { if swiftErr, ok := err.(*swift.Error); ok && swiftErr.StatusCode == 404 {
// Create a object manifest // Create a object manifest
if dir, err := d.createParentFolder(path); err != nil { if err := d.createParentFolders(path); err != nil {
return bytesRead, parseError(dir, err) return bytesRead, err
} }
manifest, err := d.createManifest(path) manifest, err := d.createManifest(path)
if err != nil { if err != nil {
@ -455,22 +455,21 @@ func (d *driver) swiftPath(path string) string {
return strings.TrimLeft(strings.TrimRight(d.Prefix, "/")+path, "/") 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) dir := gopath.Dir(path)
if dir == "/" { for 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)),
_, _, err := d.Conn.Object(d.Container, d.swiftPath(dir)) false, "", directoryMimeType, nil)
if swiftErr, ok := err.(*swift.Error); ok && swiftErr.StatusCode == 404 { if err != nil {
_, err := d.Conn.ObjectPut(d.Container, d.swiftPath(dir), bytes.NewReader(make([]byte, 0)), return parseError(dir, err)
false, "", directoryMimeType, nil) }
if err != nil {
return dir, err
} }
dir = gopath.Dir(dir)
} }
return dir, nil return nil
} }
func (d *driver) getContentType() string { func (d *driver) getContentType() string {