Contains the Concourse pipeline definition for building a line-server container
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.3 KiB

  1. package localfs
  2. import (
  3. "errors"
  4. "io"
  5. "io/ioutil"
  6. "net/http"
  7. "os"
  8. "path"
  9. "github.com/andreimarcu/linx-server/backends"
  10. "github.com/andreimarcu/linx-server/torrent"
  11. )
  12. type LocalfsBackend struct {
  13. basePath string
  14. }
  15. func (b LocalfsBackend) Delete(key string) error {
  16. return os.Remove(path.Join(b.basePath, key))
  17. }
  18. func (b LocalfsBackend) Exists(key string) (bool, error) {
  19. _, err := os.Stat(path.Join(b.basePath, key))
  20. return err == nil, err
  21. }
  22. func (b LocalfsBackend) Get(key string) ([]byte, error) {
  23. return ioutil.ReadFile(path.Join(b.basePath, key))
  24. }
  25. func (b LocalfsBackend) Put(key string, r io.Reader) (int64, error) {
  26. dst, err := os.Create(path.Join(b.basePath, key))
  27. if err != nil {
  28. return 0, err
  29. }
  30. defer dst.Close()
  31. bytes, err := io.Copy(dst, r)
  32. if bytes == 0 {
  33. b.Delete(key)
  34. return bytes, errors.New("Empty file")
  35. } else if err != nil {
  36. b.Delete(key)
  37. return bytes, err
  38. }
  39. return bytes, err
  40. }
  41. func (b LocalfsBackend) Open(key string) (backends.ReadSeekCloser, error) {
  42. return os.Open(path.Join(b.basePath, key))
  43. }
  44. func (b LocalfsBackend) ServeFile(key string, w http.ResponseWriter, r *http.Request) error {
  45. filePath := path.Join(b.basePath, key)
  46. http.ServeFile(w, r, filePath)
  47. return nil
  48. }
  49. func (b LocalfsBackend) Size(key string) (int64, error) {
  50. fileInfo, err := os.Stat(path.Join(b.basePath, key))
  51. if err != nil {
  52. return 0, err
  53. }
  54. return fileInfo.Size(), nil
  55. }
  56. func (b LocalfsBackend) GetTorrent(fileName string, url string) (t torrent.Torrent, err error) {
  57. chunk := make([]byte, torrent.TORRENT_PIECE_LENGTH)
  58. t = torrent.Torrent{
  59. Encoding: "UTF-8",
  60. Info: torrent.TorrentInfo{
  61. PieceLength: torrent.TORRENT_PIECE_LENGTH,
  62. Name: fileName,
  63. },
  64. UrlList: []string{url},
  65. }
  66. f, err := b.Open(fileName)
  67. if err != nil {
  68. return
  69. }
  70. defer f.Close()
  71. for {
  72. n, err := f.Read(chunk)
  73. if err == io.EOF {
  74. break
  75. } else if err != nil {
  76. return t, err
  77. }
  78. t.Info.Length += n
  79. t.Info.Pieces += string(torrent.HashPiece(chunk[:n]))
  80. }
  81. return
  82. }
  83. func (b LocalfsBackend) List() ([]string, error) {
  84. var output []string
  85. files, err := ioutil.ReadDir(b.basePath)
  86. if err != nil {
  87. return nil, err
  88. }
  89. for _, file := range files {
  90. output = append(output, file.Name())
  91. }
  92. return output, nil
  93. }
  94. func NewLocalfsBackend(basePath string) LocalfsBackend {
  95. return LocalfsBackend{basePath: basePath}
  96. }