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.

170 lines
3.4 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. "github.com/aws/aws-sdk-go/aws"
  12. "github.com/aws/aws-sdk-go/aws/session"
  13. "github.com/aws/aws-sdk-go/service/s3"
  14. "github.com/aws/aws-sdk-go/service/s3/s3manager"
  15. "github.com/zeebo/bencode"
  16. )
  17. type S3Backend struct {
  18. bucket string
  19. svc *S3
  20. }
  21. func (b S3Backend) Delete(key string) error {
  22. input := &s3.DeleteObjectInput{
  23. Bucket: aws.String(b.bucket),
  24. Key: aws.String(key),
  25. }
  26. _, err := b.svc.DeleteObject(input)
  27. return os.Remove(path.Join(b.bucket, key))
  28. }
  29. func (b S3Backend) Exists(key string) (bool, error) {
  30. input := &s3.HeadObjectInput{
  31. Bucket: aws.String(b.bucket),
  32. Key: aws.String(key),
  33. }
  34. _, err := b.svc.HeadObject(input)
  35. return err == nil, err
  36. }
  37. func (b S3Backend) Get(key string) ([]byte, error) {
  38. input := &s3.GetObjectInput{
  39. Bucket: aws.String(b.bucket),
  40. Key: aws.String(key),
  41. }
  42. result, err := b.svc.GetObject(input)
  43. if err != nil {
  44. return []byte{}, err
  45. }
  46. defer result.Body.Close()
  47. return ioutil.ReadAll(result.Body)
  48. }
  49. func (b S3Backend) Put(key string, r io.Reader) (int64, error) {
  50. uploader := s3manager.NewUploaderWithClient(b.svc)
  51. input := &s3manager.UploadInput{
  52. Bucket: aws.String(b.bucket),
  53. Key: aws.String(key),
  54. Body: r,
  55. }
  56. result, err := uploader.Upload(input)
  57. if err != nil {
  58. return 0, err
  59. }
  60. return -1, nil
  61. }
  62. func (b S3Backend) Open(key string) (backends.ReadSeekCloser, error) {
  63. input := &s3.GetObjectInput{
  64. Bucket: aws.String(b.bucket),
  65. Key: aws.String(key),
  66. }
  67. result, err := b.svc.GetObject(input)
  68. if err != nil {
  69. return nil, err
  70. }
  71. return result.Body, nil
  72. }
  73. func (b S3Backend) ServeFile(key string, w http.ResponseWriter, r *http.Request) {
  74. input := &s3.GetObjectInput{
  75. Bucket: aws.String(b.bucket),
  76. Key: aws.String(key),
  77. }
  78. result, err := b.svc.GetObject(input)
  79. if err != nil {
  80. return err
  81. }
  82. defer result.Body.Close()
  83. http.ServeContent(w, r, key, *result.LastModified, result.Body)
  84. return nil
  85. }
  86. func (b S3Backend) Size(key string) (int64, error) {
  87. input := &s3.HeadObjectInput{
  88. Bucket: aws.String(b.bucket),
  89. Key: aws.String(key),
  90. }
  91. result, err := b.svc.HeadObject(input)
  92. if err != nil {
  93. return 0, err
  94. }
  95. return *result.ContentLength, nil
  96. }
  97. func (b S3Backend) GetTorrent(fileName string, url string) (t torrent.Torrent, err error) {
  98. input := &s3.GetObjectTorrentInput{
  99. Bucket: aws.String(b.bucket),
  100. Key: aws.String(fileName),
  101. }
  102. result, err := b.svc.GetObjectTorrent(input)
  103. if err != nil {
  104. return
  105. }
  106. defer result.Body.Close()
  107. data, err := ioutil.ReadAll(result.Body)
  108. if err != nil {
  109. return
  110. }
  111. err = bencode.DecodeBytes(data, &t)
  112. if err != nil {
  113. return
  114. }
  115. t.Info.Name = fileName
  116. t.UrlList = []string{url}
  117. return
  118. }
  119. func (b S3Backend) List() ([]string, error) {
  120. var output []string
  121. input := &s3.ListObjectsInput{
  122. bucket: aws.String(b.bucket),
  123. }
  124. results, err := b.svc.ListObjects(input)
  125. if err != nil {
  126. return nil, err
  127. }
  128. for _, object := range results.Contents {
  129. output = append(output, *object.Key)
  130. }
  131. return output, nil
  132. }
  133. func NewS3Backend(bucket string, region string, endpoint string) S3Backend {
  134. awsConfig := &aws.Config{}
  135. if region != "" {
  136. awsConfig.Region = aws.String(region)
  137. }
  138. if endpoint != "" {
  139. awsConfig.Endpoint = aws.String(endpoint)
  140. }
  141. sess := session.Must(session.NewSession(awsConfig))
  142. svc := s3.New(sess)
  143. return S3Backend{bucket: bucket, svc: svc}
  144. }