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.

70 lines
1.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. )
  11. type LocalfsBackend struct {
  12. basePath string
  13. }
  14. func (b LocalfsBackend) Delete(key string) error {
  15. return os.Remove(path.Join(b.basePath, key))
  16. }
  17. func (b LocalfsBackend) Exists(key string) (bool, error) {
  18. _, err := os.Stat(path.Join(b.basePath, key))
  19. return err == nil, err
  20. }
  21. func (b LocalfsBackend) Get(key string) ([]byte, error) {
  22. return ioutil.ReadFile(path.Join(b.basePath, key))
  23. }
  24. func (b LocalfsBackend) Put(key string, r io.Reader) (int64, error) {
  25. dst, err := os.Create(path.Join(b.basePath, key))
  26. if err != nil {
  27. return 0, err
  28. }
  29. defer dst.Close()
  30. bytes, err := io.Copy(dst, r)
  31. if bytes == 0 {
  32. b.Delete(key)
  33. return bytes, errors.New("Empty file")
  34. } else if err != nil {
  35. b.Delete(key)
  36. return bytes, err
  37. }
  38. return bytes, err
  39. }
  40. func (b LocalfsBackend) Open(key string) (backends.ReadSeekCloser, error) {
  41. return os.Open(path.Join(b.basePath, key))
  42. }
  43. func (b LocalfsBackend) ServeFile(key string, w http.ResponseWriter, r *http.Request) {
  44. filePath := path.Join(b.basePath, key)
  45. http.ServeFile(w, r, filePath)
  46. }
  47. func (b LocalfsBackend) Size(key string) (int64, error) {
  48. fileInfo, err := os.Stat(path.Join(b.basePath, key))
  49. if err != nil {
  50. return 0, err
  51. }
  52. return fileInfo.Size(), nil
  53. }
  54. func NewLocalfsBackend(basePath string) LocalfsBackend {
  55. return LocalfsBackend{basePath: basePath}
  56. }