From 53762be170e52c3cda4bfefde4bd28c709f3622a Mon Sep 17 00:00:00 2001 From: ridhozhr Date: Wed, 22 May 2019 22:04:17 +0700 Subject: [PATCH] integrate prefix with store method --- gcsstore/gcsstore.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/gcsstore/gcsstore.go b/gcsstore/gcsstore.go index 2117179..9171376 100644 --- a/gcsstore/gcsstore.go +++ b/gcsstore/gcsstore.go @@ -66,7 +66,7 @@ func (store GCSStore) NewUpload(info tusd.FileInfo) (id string, err error) { } ctx := context.Background() - err = store.writeInfo(ctx, info.ID, info) + err = store.writeInfo(ctx, store.keyWithPrefix(info.ID), info) if err != nil { return info.ID, err } @@ -75,7 +75,7 @@ func (store GCSStore) NewUpload(info tusd.FileInfo) (id string, err error) { } func (store GCSStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) { - prefix := fmt.Sprintf("%s_", id) + prefix := fmt.Sprintf("%s_", store.keyWithPrefix(id)) filterParams := GCSFilterParams{ Bucket: store.Bucket, Prefix: prefix, @@ -101,7 +101,7 @@ func (store GCSStore) WriteChunk(id string, offset int64, src io.Reader) (int64, } } - cid := fmt.Sprintf("%s_%d", id, maxIdx+1) + cid := fmt.Sprintf("%s_%d", store.keyWithPrefix(id), maxIdx+1) objectParams := GCSObjectParams{ Bucket: store.Bucket, ID: cid, @@ -119,7 +119,7 @@ const CONCURRENT_SIZE_REQUESTS = 32 func (store GCSStore) GetInfo(id string) (tusd.FileInfo, error) { info := tusd.FileInfo{} - i := fmt.Sprintf("%s.info", id) + i := fmt.Sprintf("%s.info", store.keyWithPrefix(id)) params := GCSObjectParams{ Bucket: store.Bucket, @@ -145,7 +145,7 @@ func (store GCSStore) GetInfo(id string) (tusd.FileInfo, error) { return info, err } - prefix := fmt.Sprintf("%s", id) + prefix := fmt.Sprintf("%s", store.keyWithPrefix(id)) filterParams := GCSFilterParams{ Bucket: store.Bucket, Prefix: prefix, @@ -207,7 +207,7 @@ func (store GCSStore) GetInfo(id string) (tusd.FileInfo, error) { } info.Offset = offset - err = store.writeInfo(ctx, id, info) + err = store.writeInfo(ctx, store.keyWithPrefix(id), info) if err != nil { return info, err } @@ -238,7 +238,7 @@ func (store GCSStore) writeInfo(ctx context.Context, id string, info tusd.FileIn } func (store GCSStore) FinishUpload(id string) error { - prefix := fmt.Sprintf("%s_", id) + prefix := fmt.Sprintf("%s_", store.keyWithPrefix(id)) filterParams := GCSFilterParams{ Bucket: store.Bucket, Prefix: prefix, @@ -252,7 +252,7 @@ func (store GCSStore) FinishUpload(id string) error { composeParams := GCSComposeParams{ Bucket: store.Bucket, - Destination: id, + Destination: store.keyWithPrefix(id), Sources: names, } @@ -273,7 +273,7 @@ func (store GCSStore) FinishUpload(id string) error { objectParams := GCSObjectParams{ Bucket: store.Bucket, - ID: id, + ID: store.keyWithPrefix(id), } err = store.Service.SetObjectMetadata(ctx, objectParams, info.MetaData) @@ -287,7 +287,7 @@ func (store GCSStore) FinishUpload(id string) error { func (store GCSStore) Terminate(id string) error { filterParams := GCSFilterParams{ Bucket: store.Bucket, - Prefix: id, + Prefix: store.keyWithPrefix(id), } ctx := context.Background() @@ -302,7 +302,7 @@ func (store GCSStore) Terminate(id string) error { func (store GCSStore) GetReader(id string) (io.Reader, error) { params := GCSObjectParams{ Bucket: store.Bucket, - ID: id, + ID: store.keyWithPrefix(id), } ctx := context.Background() @@ -313,3 +313,11 @@ func (store GCSStore) GetReader(id string) (io.Reader, error) { return r, nil } + +func (store GCSStore) keyWithPrefix(key string) string { + prefix := store.ObjectPrefix + if prefix != "" && !strings.HasSuffix(prefix, "/") { + prefix += "/" + } + return prefix + key +}