From 6c7dbcaf4ed4783db1a0cda78e909534f31bfbfb Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 7 Jun 2015 02:51:31 -0700 Subject: [PATCH] More prefix url cleanup --- cp-url.go | 18 +++++++----------- pkg/client/s3/s3.go | 18 ++++++++++-------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/cp-url.go b/cp-url.go index f1034337..1c9e43f7 100644 --- a/cp-url.go +++ b/cp-url.go @@ -213,13 +213,6 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *cpURLs { // add `/` after trimming off `...` to emulate directories sourceURL = stripRecursiveURL(sourceURL) - // is set when ... includes a directory, i.e. /path/to/dir... instead of path/to/dir/... - recursivePrefix := "" - if !strings.HasSuffix(sourceURL, string(filepath.Separator)) { - recursivePrefix = filepath.Base(sourceURL) - } - sourceURL = strings.TrimSuffix(sourceURL, string(filepath.Separator)) + string(filepath.Separator) - sourceClient, err := source2Client(sourceURL) if err != nil { cpURLsCh <- &cpURLs{Error: iodine.New(err, nil)} @@ -285,16 +278,19 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *cpURLs { continue } - sourceContentParse, err := client.Parse(sourceURLParse.String() + sourceContent.Content.Name) + sourceURLDelimited := sourceURLParse.String()[:strings.LastIndex(sourceURLParse.String(), + string(sourceURLParse.Separator))+1] + sourceContentName := sourceContent.Content.Name + sourceContentURL := sourceURLDelimited + sourceContentName + sourceContentParse, err := client.Parse(sourceContentURL) if err != nil { - cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidSource{URL: sourceContent.Content.Name}, nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidSource{URL: sourceContentName}, nil)} continue } // Construct target path from recursive path of source without its prefix dir. newTargetURLParse := *targetURLParse - newTargetURLParse.Path = filepath.Join(newTargetURLParse.Path, recursivePrefix, - strings.TrimPrefix(sourceContentParse.Path, filepath.Dir(sourceURLParse.Path))) + newTargetURLParse.Path = filepath.Join(newTargetURLParse.Path, sourceContentName) cpURLsCh <- prepareCopyURLsTypeA(sourceContentParse.String(), newTargetURLParse.String()) } }(sourceURL, targetURL, cpURLsCh) diff --git a/pkg/client/s3/s3.go b/pkg/client/s3/s3.go index 34158782..45169b61 100644 --- a/pkg/client/s3/s3.go +++ b/pkg/client/s3/s3.go @@ -162,17 +162,16 @@ func (c *s3Client) Stat() (*client.Content, error) { if object != "" { metadata, err := c.api.StatObject(bucket, object) if err != nil { - if err.Error() == "404 Not Found" { + errResponse := s3.ToErrorResponse(err) + if errResponse.Code == "NoSuchKey" { for content := range c.List(false) { if content.Err != nil { return nil, iodine.New(err, nil) } - if !strings.HasPrefix(content.Content.Name, object) { - content.Content.Type = os.ModeDir - content.Content.Name = object - content.Content.Size = 0 - return content.Content, nil - } + content.Content.Type = os.ModeDir + content.Content.Name = object + content.Content.Size = 0 + return content.Content, nil } } return nil, iodine.New(err, nil) @@ -336,7 +335,10 @@ func (c *s3Client) listRecursiveInRoutine(contentCh chan client.ContentOnChannel return } content := new(client.Content) - normalizedKey := strings.TrimPrefix(object.Stat.Key, strings.TrimSuffix(o, delimiter)+delimiter) + normalizedKey := object.Stat.Key + if strings.HasSuffix(o, delimiter) { + normalizedKey = strings.TrimPrefix(object.Stat.Key, o) + } content.Name = normalizedKey content.Size = object.Stat.Size content.Time = object.Stat.LastModified