Browse Source
Merge pull request #84 from mutantmonkey/abstract_storage
Merge pull request #84 from mutantmonkey/abstract_storage
use abstracted storage for flexibilitypull/87/head
Andrei Marcu
9 years ago
committed by
GitHub
10 changed files with 154 additions and 77 deletions
-
23backends/backends.go
-
70backends/localfs/localfs.go
-
9delete.go
-
12display.go
-
13fileserve.go
-
27meta.go
-
7server.go
-
21torrent.go
-
17torrent_test.go
-
32upload.go
@ -0,0 +1,23 @@ |
|||||
|
package backends |
||||
|
|
||||
|
import ( |
||||
|
"io" |
||||
|
"net/http" |
||||
|
) |
||||
|
|
||||
|
type ReadSeekCloser interface { |
||||
|
io.Reader |
||||
|
io.Closer |
||||
|
io.Seeker |
||||
|
io.ReaderAt |
||||
|
} |
||||
|
|
||||
|
type StorageBackend interface { |
||||
|
Delete(key string) error |
||||
|
Exists(key string) (bool, error) |
||||
|
Get(key string) ([]byte, error) |
||||
|
Put(key string, r io.Reader) (int64, error) |
||||
|
Open(key string) (ReadSeekCloser, error) |
||||
|
ServeFile(key string, w http.ResponseWriter, r *http.Request) |
||||
|
Size(key string) (int64, error) |
||||
|
} |
@ -0,0 +1,70 @@ |
|||||
|
package localfs |
||||
|
|
||||
|
import ( |
||||
|
"errors" |
||||
|
"io" |
||||
|
"io/ioutil" |
||||
|
"net/http" |
||||
|
"os" |
||||
|
"path" |
||||
|
|
||||
|
"github.com/andreimarcu/linx-server/backends" |
||||
|
) |
||||
|
|
||||
|
type LocalfsBackend struct { |
||||
|
basePath string |
||||
|
} |
||||
|
|
||||
|
func (b LocalfsBackend) Delete(key string) error { |
||||
|
return os.Remove(path.Join(b.basePath, key)) |
||||
|
} |
||||
|
|
||||
|
func (b LocalfsBackend) Exists(key string) (bool, error) { |
||||
|
_, err := os.Stat(path.Join(b.basePath, key)) |
||||
|
return err == nil, err |
||||
|
} |
||||
|
|
||||
|
func (b LocalfsBackend) Get(key string) ([]byte, error) { |
||||
|
return ioutil.ReadFile(path.Join(b.basePath, key)) |
||||
|
} |
||||
|
|
||||
|
func (b LocalfsBackend) Put(key string, r io.Reader) (int64, error) { |
||||
|
dst, err := os.Create(path.Join(b.basePath, key)) |
||||
|
if err != nil { |
||||
|
return 0, err |
||||
|
} |
||||
|
defer dst.Close() |
||||
|
|
||||
|
bytes, err := io.Copy(dst, r) |
||||
|
if bytes == 0 { |
||||
|
b.Delete(key) |
||||
|
return bytes, errors.New("Empty file") |
||||
|
} else if err != nil { |
||||
|
b.Delete(key) |
||||
|
return bytes, err |
||||
|
} |
||||
|
|
||||
|
return bytes, err |
||||
|
} |
||||
|
|
||||
|
func (b LocalfsBackend) Open(key string) (backends.ReadSeekCloser, error) { |
||||
|
return os.Open(path.Join(b.basePath, key)) |
||||
|
} |
||||
|
|
||||
|
func (b LocalfsBackend) ServeFile(key string, w http.ResponseWriter, r *http.Request) { |
||||
|
filePath := path.Join(b.basePath, key) |
||||
|
http.ServeFile(w, r, filePath) |
||||
|
} |
||||
|
|
||||
|
func (b LocalfsBackend) Size(key string) (int64, error) { |
||||
|
fileInfo, err := os.Stat(path.Join(b.basePath, key)) |
||||
|
if err != nil { |
||||
|
return 0, err |
||||
|
} |
||||
|
|
||||
|
return fileInfo.Size(), nil |
||||
|
} |
||||
|
|
||||
|
func NewLocalfsBackend(basePath string) LocalfsBackend { |
||||
|
return LocalfsBackend{basePath: basePath} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue