storage.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package storage
  2. import (
  3. "os"
  4. "time"
  5. )
  6. // DoesNotExistError is used as a generic error to return when a target path does not
  7. // exist in storage. Equivalent to os.ErrorNotExist such that it will work with os.IsNotExist(err)
  8. var DoesNotExistError = os.ErrNotExist
  9. // StorageInfo is a data object containing basic information about the path in storage.
  10. type StorageInfo struct {
  11. Name string // base name of the file
  12. Size int64 // length in bytes for regular files
  13. ModTime time.Time // modification time
  14. }
  15. // Storage provides an API for storing binary data
  16. type Storage interface {
  17. // FullPath returns the storage working path combined with the path provided
  18. FullPath(path string) string
  19. // Stat returns the StorageStats for the specific path.
  20. Stat(path string) (*StorageInfo, error)
  21. // Read uses the relative path of the storage combined with the provided path to
  22. // read the contents.
  23. Read(path string) ([]byte, error)
  24. // Write uses the relative path of the storage combined with the provided path
  25. // to write a new file or overwrite an existing file.
  26. Write(path string, data []byte) error
  27. // Remove uses the relative path of the storage combined with the provided path to
  28. // remove a file from storage permanently.
  29. Remove(path string) error
  30. // Exists uses the relative path of the storage combined with the provided path to
  31. // determine if the file exists.
  32. Exists(path string) (bool, error)
  33. // List uses the relative path of the storage combined with the provided path to return
  34. // storage information for the files.
  35. List(path string) ([]*StorageInfo, error)
  36. }
  37. // IsNotExist returns true if the error provided from a storage object is DoesNotExist
  38. func IsNotExist(err error) bool {
  39. if err == nil {
  40. return false
  41. }
  42. return err.Error() == DoesNotExistError.Error()
  43. }