Skip to content

Commit

Permalink
insert context to every func
Browse files Browse the repository at this point in the history
  • Loading branch information
logica0419 committed Jan 13, 2024
1 parent f0c6924 commit a87f54b
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions utils/storage/swift.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package storage

import (
"context"
"fmt"
"io"
"net/url"
"os"
"time"

"github.com/ncw/swift"
"github.com/ncw/swift/v2"

"github.com/traPtitech/traQ/model"
"github.com/traPtitech/traQ/utils"
Expand Down Expand Up @@ -38,11 +39,13 @@ func NewSwiftFileStorage(container, userName, apiKey, tenant, tenantID, authURL,
mutexes: utils.NewKeyMutex(256),
}

if err := m.connection.Authenticate(); err != nil {
ctx := context.Background()

if err := m.connection.Authenticate(ctx); err != nil {
return nil, err
}

containers, err := m.connection.ContainerNamesAll(nil)
containers, err := m.connection.ContainerNamesAll(ctx, nil)
if err != nil {
return nil, err
}
Expand All @@ -59,8 +62,10 @@ func NewSwiftFileStorage(container, userName, apiKey, tenant, tenantID, authURL,
func (fs *SwiftFileStorage) OpenFileByKey(key string, fileType model.FileType) (reader io.ReadSeekCloser, err error) {
cacheName := fs.getCacheFilePath(key)

ctx := context.Background()

if !fs.cacheable(fileType) {
file, _, err := fs.connection.ObjectOpen(fs.container, key, true, nil)
file, _, err := fs.connection.ObjectOpen(ctx, fs.container, key, true, nil)
if err != nil {
if err == swift.ObjectNotFound {
return nil, ErrFileNotFound
Expand All @@ -73,7 +78,7 @@ func (fs *SwiftFileStorage) OpenFileByKey(key string, fileType model.FileType) (
fs.mutexes.Lock(key)
if _, err := os.Stat(cacheName); os.IsNotExist(err) {
defer fs.mutexes.Unlock(key)
remote, _, err := fs.connection.ObjectOpen(fs.container, key, true, nil)
remote, _, err := fs.connection.ObjectOpen(ctx, fs.container, key, true, nil)
if err != nil {
if err == swift.ObjectNotFound {
return nil, ErrFileNotFound
Expand Down Expand Up @@ -123,15 +128,15 @@ func (fs *SwiftFileStorage) SaveByKey(src io.Reader, key, name, contentType stri
}
}

_, err = fs.connection.ObjectPut(fs.container, key, src, true, "", contentType, swift.Headers{
_, err = fs.connection.ObjectPut(context.Background(), fs.container, key, src, true, "", contentType, swift.Headers{
"Content-Disposition": fmt.Sprintf("attachment; filename*=UTF-8''%s", url.PathEscape(name)),
})
return
}

// DeleteByKey ファイルを削除します
func (fs *SwiftFileStorage) DeleteByKey(key string, _ model.FileType) (err error) {
err = fs.connection.ObjectDelete(fs.container, key)
err = fs.connection.ObjectDelete(context.Background(), fs.container, key)
if err != nil {
if err == swift.ObjectNotFound {
return ErrFileNotFound
Expand Down

0 comments on commit a87f54b

Please sign in to comment.