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.

208 lines
4.2 KiB

  1. package localfs
  2. import (
  3. "encoding/json"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "path"
  8. "time"
  9. "github.com/andreimarcu/linx-server/backends"
  10. "github.com/andreimarcu/linx-server/helpers"
  11. "github.com/andreimarcu/linx-server/torrent"
  12. )
  13. type LocalfsBackend struct {
  14. metaPath string
  15. filesPath string
  16. }
  17. type MetadataJSON struct {
  18. DeleteKey string `json:"delete_key"`
  19. Sha256sum string `json:"sha256sum"`
  20. Mimetype string `json:"mimetype"`
  21. Size int64 `json:"size"`
  22. Expiry int64 `json:"expiry"`
  23. ArchiveFiles []string `json:"archive_files,omitempty"`
  24. }
  25. func (b LocalfsBackend) Delete(key string) (err error) {
  26. err = os.Remove(path.Join(b.filesPath, key))
  27. if err != nil {
  28. return
  29. }
  30. err = os.Remove(path.Join(b.metaPath, key))
  31. return
  32. }
  33. func (b LocalfsBackend) Exists(key string) (bool, error) {
  34. _, err := os.Stat(path.Join(b.filesPath, key))
  35. return err == nil, err
  36. }
  37. func (b LocalfsBackend) Head(key string) (metadata backends.Metadata, err error) {
  38. f, err := os.Open(path.Join(b.metaPath, key))
  39. if os.IsNotExist(err) {
  40. return metadata, backends.NotFoundErr
  41. } else if err != nil {
  42. return metadata, backends.BadMetadata
  43. }
  44. defer f.Close()
  45. decoder := json.NewDecoder(f)
  46. mjson := MetadataJSON{}
  47. if err := decoder.Decode(&mjson); err != nil {
  48. return metadata, backends.BadMetadata
  49. }
  50. metadata.DeleteKey = mjson.DeleteKey
  51. metadata.Mimetype = mjson.Mimetype
  52. metadata.ArchiveFiles = mjson.ArchiveFiles
  53. metadata.Sha256sum = mjson.Sha256sum
  54. metadata.Expiry = time.Unix(mjson.Expiry, 0)
  55. metadata.Size = mjson.Size
  56. return
  57. }
  58. func (b LocalfsBackend) Get(key string) (metadata backends.Metadata, f io.ReadCloser, err error) {
  59. metadata, err = b.Head(key)
  60. if err != nil {
  61. return
  62. }
  63. f, err = os.Open(path.Join(b.filesPath, key))
  64. if err != nil {
  65. return
  66. }
  67. return
  68. }
  69. func (b LocalfsBackend) writeMetadata(key string, metadata backends.Metadata) error {
  70. metaPath := path.Join(b.metaPath, key)
  71. mjson := MetadataJSON{
  72. DeleteKey: metadata.DeleteKey,
  73. Mimetype: metadata.Mimetype,
  74. ArchiveFiles: metadata.ArchiveFiles,
  75. Sha256sum: metadata.Sha256sum,
  76. Expiry: metadata.Expiry.Unix(),
  77. Size: metadata.Size,
  78. }
  79. dst, err := os.Create(metaPath)
  80. if err != nil {
  81. return err
  82. }
  83. defer dst.Close()
  84. encoder := json.NewEncoder(dst)
  85. err = encoder.Encode(mjson)
  86. if err != nil {
  87. os.Remove(metaPath)
  88. return err
  89. }
  90. return nil
  91. }
  92. func (b LocalfsBackend) Put(key string, r io.Reader, expiry time.Time, deleteKey string) (m backends.Metadata, err error) {
  93. filePath := path.Join(b.filesPath, key)
  94. dst, err := os.Create(filePath)
  95. if err != nil {
  96. return
  97. }
  98. defer dst.Close()
  99. bytes, err := io.Copy(dst, r)
  100. if bytes == 0 {
  101. os.Remove(filePath)
  102. return m, backends.FileEmptyError
  103. } else if err != nil {
  104. os.Remove(filePath)
  105. return m, err
  106. }
  107. m.Expiry = expiry
  108. m.DeleteKey = deleteKey
  109. m.Size = bytes
  110. m.Mimetype, _ = helpers.DetectMime(dst)
  111. m.Sha256sum, _ = helpers.Sha256sum(dst)
  112. m.ArchiveFiles, _ = helpers.ListArchiveFiles(m.Mimetype, m.Size, dst)
  113. err = b.writeMetadata(key, m)
  114. if err != nil {
  115. os.Remove(filePath)
  116. return
  117. }
  118. return
  119. }
  120. func (b LocalfsBackend) Size(key string) (int64, error) {
  121. fileInfo, err := os.Stat(path.Join(b.filesPath, key))
  122. if err != nil {
  123. return 0, err
  124. }
  125. return fileInfo.Size(), nil
  126. }
  127. func (b LocalfsBackend) GetTorrent(fileName string, url string) (t torrent.Torrent, err error) {
  128. chunk := make([]byte, torrent.TORRENT_PIECE_LENGTH)
  129. t = torrent.Torrent{
  130. Encoding: "UTF-8",
  131. Info: torrent.TorrentInfo{
  132. PieceLength: torrent.TORRENT_PIECE_LENGTH,
  133. Name: fileName,
  134. },
  135. UrlList: []string{url},
  136. }
  137. _, f, err := b.Get(fileName)
  138. if err != nil {
  139. return
  140. }
  141. defer f.Close()
  142. for {
  143. n, err := f.Read(chunk)
  144. if err == io.EOF {
  145. break
  146. } else if err != nil {
  147. return t, err
  148. }
  149. t.Info.Length += n
  150. t.Info.Pieces += string(torrent.HashPiece(chunk[:n]))
  151. }
  152. return
  153. }
  154. func (b LocalfsBackend) List() ([]string, error) {
  155. var output []string
  156. files, err := ioutil.ReadDir(b.filesPath)
  157. if err != nil {
  158. return nil, err
  159. }
  160. for _, file := range files {
  161. output = append(output, file.Name())
  162. }
  163. return output, nil
  164. }
  165. func NewLocalfsBackend(metaPath string, filesPath string) LocalfsBackend {
  166. return LocalfsBackend{
  167. metaPath: metaPath,
  168. filesPath: filesPath,
  169. }
  170. }