Skip to content

Commit e8a4831

Browse files
committed
refactor storage constructor
1 parent c1fdcc0 commit e8a4831

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

internal/storage/storage.go

+2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import "io"
55

66
// Storage is an interface for reading/writing torrent files.
77
type Storage interface {
8+
// Open a file. If the file does not exist, it will be created.
89
Open(name string, size int64) (f File, exists bool, err error)
10+
// RootDir is the absolute path of the storage root.
911
RootDir() string
1012
}
1113

torrent/session.go

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"github.com/cenkalti/rain/internal/resourcemanager"
2323
"github.com/cenkalti/rain/internal/resumer/boltdbresumer"
2424
"github.com/cenkalti/rain/internal/semaphore"
25+
"github.com/cenkalti/rain/internal/storage"
26+
"github.com/cenkalti/rain/internal/storage/filestorage"
2527
"github.com/cenkalti/rain/internal/tracker"
2628
"github.com/cenkalti/rain/internal/trackermanager"
2729
"github.com/juju/ratelimit"
@@ -443,6 +445,10 @@ func (s *Session) StopAll() error {
443445
return nil
444446
}
445447

448+
func (s *Session) newStorage(id string) (storage.Storage, error) {
449+
return filestorage.New(s.getDataDir(id), s.config.FilePermissions)
450+
}
451+
446452
func (s *Session) getDataDir(torrentID string) string {
447453
if s.config.DataDirIncludesTorrentID {
448454
return filepath.Join(s.config.DataDir, torrentID)

torrent/session_add.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/cenkalti/rain/internal/metainfo"
1515
"github.com/cenkalti/rain/internal/resumer"
1616
"github.com/cenkalti/rain/internal/resumer/boltdbresumer"
17-
"github.com/cenkalti/rain/internal/storage/filestorage"
17+
"github.com/cenkalti/rain/internal/storage"
1818
"github.com/cenkalti/rain/internal/webseedsource"
1919
"github.com/gofrs/uuid"
2020
"github.com/nictuku/dht"
@@ -236,7 +236,7 @@ func (s *Session) addMagnet(link string, opt *AddTorrentOptions) (*Torrent, erro
236236
return t2, err
237237
}
238238

239-
func (s *Session) add(opt *AddTorrentOptions) (id string, port int, sto *filestorage.FileStorage, err error) {
239+
func (s *Session) add(opt *AddTorrentOptions) (id string, port int, sto storage.Storage, err error) {
240240
port, err = s.getPort()
241241
if err != nil {
242242
return
@@ -266,7 +266,7 @@ func (s *Session) add(opt *AddTorrentOptions) (id string, port int, sto *filesto
266266
}
267267
id = base64.RawURLEncoding.EncodeToString(u1[:])
268268
}
269-
sto, err = filestorage.New(s.getDataDir(id), s.config.FilePermissions)
269+
sto, err = s.newStorage(id)
270270
if err != nil {
271271
return
272272
}

torrent/session_load.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/cenkalti/rain/internal/metainfo"
99
"github.com/cenkalti/rain/internal/resumer"
1010
"github.com/cenkalti/rain/internal/resumer/boltdbresumer"
11-
"github.com/cenkalti/rain/internal/storage/filestorage"
1211
"github.com/cenkalti/rain/internal/webseedsource"
1312
"go.etcd.io/bbolt"
1413
)
@@ -84,7 +83,7 @@ func (s *Session) loadExistingTorrent(id string) (tt *Torrent, hasStarted bool,
8483
bf = bf3
8584
}
8685
}
87-
sto, err := filestorage.New(s.getDataDir(id), s.config.FilePermissions)
86+
sto, err := s.newStorage(id)
8887
if err != nil {
8988
return
9089
}

0 commit comments

Comments
 (0)