1
0
mirror of https://github.com/gilbertchen/duplicacy synced 2025-12-20 10:13:20 +00:00

Merge pull request #626 from markfeit/swift-v2

Swift v2
This commit is contained in:
gilbertchen
2022-04-08 22:29:30 -04:00
committed by GitHub

View File

@@ -5,6 +5,7 @@
package duplicacy package duplicacy
import ( import (
"context"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -15,6 +16,7 @@ import (
type SwiftStorage struct { type SwiftStorage struct {
StorageBase StorageBase
ctx context.Context
connection *swift.Connection connection *swift.Connection
container string container string
storageDir string storageDir string
@@ -106,6 +108,8 @@ func CreateSwiftStorage(storageURL string, key string, threads int) (storage *Sw
arguments["protocol"] = "https" arguments["protocol"] = "https"
} }
ctx, _ := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
// Please refer to https://godoc.org/github.com/ncw/swift#Connection // Please refer to https://godoc.org/github.com/ncw/swift#Connection
connection := swift.Connection{ connection := swift.Connection{
Domain: arguments["domain"], Domain: arguments["domain"],
@@ -129,17 +133,18 @@ func CreateSwiftStorage(storageURL string, key string, threads int) (storage *Sw
TrustId: arguments["trust_id"], TrustId: arguments["trust_id"],
} }
err = connection.Authenticate() err = connection.Authenticate(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
_, _, err = connection.Container(container) _, _, err = connection.Container(ctx, container)
if err != nil { if err != nil {
return nil, err return nil, err
} }
storage = &SwiftStorage{ storage = &SwiftStorage{
ctx: ctx,
connection: &connection, connection: &connection,
container: container, container: container,
storageDir: storageDir, storageDir: storageDir,
@@ -168,7 +173,7 @@ func (storage *SwiftStorage) ListFiles(threadIndex int, dir string) (files []str
options.Delimiter = '/' options.Delimiter = '/'
} }
objects, err := storage.connection.ObjectsAll(storage.container, &options) objects, err := storage.connection.ObjectsAll(storage.ctx, storage.container, &options)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@@ -190,12 +195,12 @@ func (storage *SwiftStorage) ListFiles(threadIndex int, dir string) (files []str
// DeleteFile deletes the file or directory at 'filePath'. // DeleteFile deletes the file or directory at 'filePath'.
func (storage *SwiftStorage) DeleteFile(threadIndex int, filePath string) (err error) { func (storage *SwiftStorage) DeleteFile(threadIndex int, filePath string) (err error) {
return storage.connection.ObjectDelete(storage.container, storage.storageDir+filePath) return storage.connection.ObjectDelete(storage.ctx, storage.container, storage.storageDir+filePath)
} }
// MoveFile renames the file. // MoveFile renames the file.
func (storage *SwiftStorage) MoveFile(threadIndex int, from string, to string) (err error) { func (storage *SwiftStorage) MoveFile(threadIndex int, from string, to string) (err error) {
return storage.connection.ObjectMove(storage.container, storage.storageDir+from, return storage.connection.ObjectMove(storage.ctx, storage.container, storage.storageDir+from,
storage.container, storage.storageDir+to) storage.container, storage.storageDir+to)
} }
@@ -207,7 +212,7 @@ func (storage *SwiftStorage) CreateDirectory(threadIndex int, dir string) (err e
// GetFileInfo returns the information about the file or directory at 'filePath'. // GetFileInfo returns the information about the file or directory at 'filePath'.
func (storage *SwiftStorage) GetFileInfo(threadIndex int, filePath string) (exist bool, isDir bool, size int64, err error) { func (storage *SwiftStorage) GetFileInfo(threadIndex int, filePath string) (exist bool, isDir bool, size int64, err error) {
object, _, err := storage.connection.Object(storage.container, storage.storageDir+filePath) object, _, err := storage.connection.Object(storage.ctx, storage.container, storage.storageDir+filePath)
if err != nil { if err != nil {
if err == swift.ObjectNotFound { if err == swift.ObjectNotFound {
@@ -223,7 +228,7 @@ func (storage *SwiftStorage) GetFileInfo(threadIndex int, filePath string) (exis
// DownloadFile reads the file at 'filePath' into the chunk. // DownloadFile reads the file at 'filePath' into the chunk.
func (storage *SwiftStorage) DownloadFile(threadIndex int, filePath string, chunk *Chunk) (err error) { func (storage *SwiftStorage) DownloadFile(threadIndex int, filePath string, chunk *Chunk) (err error) {
file, _, err := storage.connection.ObjectOpen(storage.container, storage.storageDir+filePath, false, nil) file, _, err := storage.connection.ObjectOpen(storage.ctx, storage.container, storage.storageDir+filePath, false, nil)
if err != nil { if err != nil {
return err return err
} }
@@ -234,7 +239,7 @@ func (storage *SwiftStorage) DownloadFile(threadIndex int, filePath string, chun
// UploadFile writes 'content' to the file at 'filePath'. // UploadFile writes 'content' to the file at 'filePath'.
func (storage *SwiftStorage) UploadFile(threadIndex int, filePath string, content []byte) (err error) { func (storage *SwiftStorage) UploadFile(threadIndex int, filePath string, content []byte) (err error) {
reader := CreateRateLimitedReader(content, storage.UploadRateLimit/storage.threads) reader := CreateRateLimitedReader(content, storage.UploadRateLimit/storage.threads)
_, err = storage.connection.ObjectPut(storage.container, storage.storageDir+filePath, reader, true, "", "application/duplicacy", nil) _, err = storage.connection.ObjectPut(storage.ctx, storage.container, storage.storageDir+filePath, reader, true, "", "application/duplicacy", nil)
return err return err
} }