Skip to content

Commit 30b0961

Browse files
authored
Merge pull request #5 from hirose31/handle-curious-path
Handle odd object key
2 parents 1d8b0ff + bbda3e5 commit 30b0961

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

pkg/c/controller.go

+27-10
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,34 @@ func (c Controller) moveDown(prefix string) {
199199
func (c Controller) Download(key string) {
200200
c.Debugf("bucket=%s prefix=%s key=%s\n", c.m.Bucket(), c.m.Prefix(), key)
201201

202+
cwd, err := os.Getwd()
203+
if err != nil {
204+
panic(err)
205+
}
206+
cwd = cwd + "/"
207+
202208
totalSize := int64(0)
203209
existFilePath := []string{}
204210
objects := c.m.ListObjects(key)
211+
destPathMap := map[string]string{}
205212
for _, object := range objects {
206-
filePath := aws.ToString(object.Key)
207-
c.Debugf("- %s\n", filePath)
208-
if _, err := os.Stat(filePath); err == nil {
209-
existFilePath = append(existFilePath, filePath)
213+
key := aws.ToString(object.Key)
214+
// download into under current directory
215+
destPath, err := filepath.Abs("./" + key)
216+
if err != nil {
217+
panic(err)
218+
}
219+
220+
// just to be safe
221+
if !strings.HasPrefix(destPath, cwd) {
222+
panic(fmt.Sprintf("destPath is not under current directory: destPath=%s cwd=%s", destPath, cwd))
223+
}
224+
225+
destPathMap[key] = destPath
226+
227+
c.Debugf("- %s %s\n", key, destPath)
228+
if _, err := os.Stat(destPath); err == nil {
229+
existFilePath = append(existFilePath, destPath)
210230
}
211231
totalSize += object.Size
212232
}
@@ -217,10 +237,6 @@ func (c Controller) Download(key string) {
217237
}
218238

219239
// check disk available
220-
cwd, err := os.Getwd()
221-
if err != nil {
222-
panic(err)
223-
}
224240
usage, err := disk.Usage(cwd)
225241
if err != nil {
226242
panic(err)
@@ -253,8 +269,9 @@ func (c Controller) Download(key string) {
253269
title := "Downloading"
254270

255271
for i, object := range objects {
256-
c.Debugf("download %s\n", aws.ToString(object.Key))
257-
n, err := c.m.Download(object)
272+
key := aws.ToString(object.Key)
273+
c.Debugf("download %s\n", key)
274+
n, err := c.m.Download(object, destPathMap[key])
258275

259276
if err != nil {
260277
panic(err)

pkg/m/s3.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,17 @@ func (s3m S3Model) ListObjects(key string) []s3types.Object {
235235
}
236236

237237
// Download ...
238-
func (s3m S3Model) Download(object s3types.Object) (n int64, err error) {
239-
filePath := aws.ToString(object.Key)
240-
241-
if err = os.MkdirAll(filepath.Dir(filePath), 0700); err != nil {
238+
func (s3m S3Model) Download(object s3types.Object, destPath string) (n int64, err error) {
239+
if err = os.MkdirAll(filepath.Dir(destPath), 0700); err != nil {
242240
return 0, err
243241
}
244242

245-
_, err = os.Stat(filePath)
243+
_, err = os.Stat(destPath)
246244
if err == nil {
247245
return 0, fmt.Errorf("exists")
248246
}
249247

250-
fp, err := os.Create(filePath)
248+
fp, err := os.Create(destPath)
251249
if err != nil {
252250
return 0, err
253251
}

0 commit comments

Comments
 (0)