integrate prefix with store method

This commit is contained in:
ridhozhr 2019-05-22 22:04:17 +07:00
parent fe62533f1e
commit 53762be170
1 changed files with 19 additions and 11 deletions

View File

@ -66,7 +66,7 @@ func (store GCSStore) NewUpload(info tusd.FileInfo) (id string, err error) {
} }
ctx := context.Background() ctx := context.Background()
err = store.writeInfo(ctx, info.ID, info) err = store.writeInfo(ctx, store.keyWithPrefix(info.ID), info)
if err != nil { if err != nil {
return info.ID, err 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) { 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{ filterParams := GCSFilterParams{
Bucket: store.Bucket, Bucket: store.Bucket,
Prefix: prefix, 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{ objectParams := GCSObjectParams{
Bucket: store.Bucket, Bucket: store.Bucket,
ID: cid, ID: cid,
@ -119,7 +119,7 @@ const CONCURRENT_SIZE_REQUESTS = 32
func (store GCSStore) GetInfo(id string) (tusd.FileInfo, error) { func (store GCSStore) GetInfo(id string) (tusd.FileInfo, error) {
info := tusd.FileInfo{} info := tusd.FileInfo{}
i := fmt.Sprintf("%s.info", id) i := fmt.Sprintf("%s.info", store.keyWithPrefix(id))
params := GCSObjectParams{ params := GCSObjectParams{
Bucket: store.Bucket, Bucket: store.Bucket,
@ -145,7 +145,7 @@ func (store GCSStore) GetInfo(id string) (tusd.FileInfo, error) {
return info, err return info, err
} }
prefix := fmt.Sprintf("%s", id) prefix := fmt.Sprintf("%s", store.keyWithPrefix(id))
filterParams := GCSFilterParams{ filterParams := GCSFilterParams{
Bucket: store.Bucket, Bucket: store.Bucket,
Prefix: prefix, Prefix: prefix,
@ -207,7 +207,7 @@ func (store GCSStore) GetInfo(id string) (tusd.FileInfo, error) {
} }
info.Offset = offset info.Offset = offset
err = store.writeInfo(ctx, id, info) err = store.writeInfo(ctx, store.keyWithPrefix(id), info)
if err != nil { if err != nil {
return info, err 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 { func (store GCSStore) FinishUpload(id string) error {
prefix := fmt.Sprintf("%s_", id) prefix := fmt.Sprintf("%s_", store.keyWithPrefix(id))
filterParams := GCSFilterParams{ filterParams := GCSFilterParams{
Bucket: store.Bucket, Bucket: store.Bucket,
Prefix: prefix, Prefix: prefix,
@ -252,7 +252,7 @@ func (store GCSStore) FinishUpload(id string) error {
composeParams := GCSComposeParams{ composeParams := GCSComposeParams{
Bucket: store.Bucket, Bucket: store.Bucket,
Destination: id, Destination: store.keyWithPrefix(id),
Sources: names, Sources: names,
} }
@ -273,7 +273,7 @@ func (store GCSStore) FinishUpload(id string) error {
objectParams := GCSObjectParams{ objectParams := GCSObjectParams{
Bucket: store.Bucket, Bucket: store.Bucket,
ID: id, ID: store.keyWithPrefix(id),
} }
err = store.Service.SetObjectMetadata(ctx, objectParams, info.MetaData) 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 { func (store GCSStore) Terminate(id string) error {
filterParams := GCSFilterParams{ filterParams := GCSFilterParams{
Bucket: store.Bucket, Bucket: store.Bucket,
Prefix: id, Prefix: store.keyWithPrefix(id),
} }
ctx := context.Background() ctx := context.Background()
@ -302,7 +302,7 @@ func (store GCSStore) Terminate(id string) error {
func (store GCSStore) GetReader(id string) (io.Reader, error) { func (store GCSStore) GetReader(id string) (io.Reader, error) {
params := GCSObjectParams{ params := GCSObjectParams{
Bucket: store.Bucket, Bucket: store.Bucket,
ID: id, ID: store.keyWithPrefix(id),
} }
ctx := context.Background() ctx := context.Background()
@ -313,3 +313,11 @@ func (store GCSStore) GetReader(id string) (io.Reader, error) {
return r, nil return r, nil
} }
func (store GCSStore) keyWithPrefix(key string) string {
prefix := store.ObjectPrefix
if prefix != "" && !strings.HasSuffix(prefix, "/") {
prefix += "/"
}
return prefix + key
}