file_cache.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package server
  2. import (
  3. "errors"
  4. "heckel.io/ntfy/util"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "regexp"
  9. "sync"
  10. )
  11. var (
  12. fileIDRegex = regexp.MustCompile(`^[-_A-Za-z0-9]+$`)
  13. errInvalidFileID = errors.New("invalid file ID")
  14. errFileExists = errors.New("file exists")
  15. )
  16. type fileCache struct {
  17. dir string
  18. totalSizeCurrent int64
  19. totalSizeLimit int64
  20. fileSizeLimit int64
  21. mu sync.Mutex
  22. }
  23. func newFileCache(dir string, totalSizeLimit int64, fileSizeLimit int64) (*fileCache, error) {
  24. if err := os.MkdirAll(dir, 0700); err != nil {
  25. return nil, err
  26. }
  27. size, err := dirSize(dir)
  28. if err != nil {
  29. return nil, err
  30. }
  31. return &fileCache{
  32. dir: dir,
  33. totalSizeCurrent: size,
  34. totalSizeLimit: totalSizeLimit,
  35. fileSizeLimit: fileSizeLimit,
  36. }, nil
  37. }
  38. func (c *fileCache) Write(id string, in io.Reader, limiters ...util.Limiter) (int64, error) {
  39. if !fileIDRegex.MatchString(id) {
  40. return 0, errInvalidFileID
  41. }
  42. file := filepath.Join(c.dir, id)
  43. if _, err := os.Stat(file); err == nil {
  44. return 0, errFileExists
  45. }
  46. f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
  47. if err != nil {
  48. return 0, err
  49. }
  50. defer f.Close()
  51. limiters = append(limiters, util.NewFixedLimiter(c.Remaining()), util.NewFixedLimiter(c.fileSizeLimit))
  52. limitWriter := util.NewLimitWriter(f, limiters...)
  53. size, err := io.Copy(limitWriter, in)
  54. if err != nil {
  55. os.Remove(file)
  56. return 0, err
  57. }
  58. if err := f.Close(); err != nil {
  59. os.Remove(file)
  60. return 0, err
  61. }
  62. c.mu.Lock()
  63. c.totalSizeCurrent += size
  64. c.mu.Unlock()
  65. return size, nil
  66. }
  67. func (c *fileCache) Remove(ids ...string) error {
  68. for _, id := range ids {
  69. if !fileIDRegex.MatchString(id) {
  70. return errInvalidFileID
  71. }
  72. file := filepath.Join(c.dir, id)
  73. _ = os.Remove(file) // Best effort delete
  74. }
  75. size, err := dirSize(c.dir)
  76. if err != nil {
  77. return err
  78. }
  79. c.mu.Lock()
  80. c.totalSizeCurrent = size
  81. c.mu.Unlock()
  82. return nil
  83. }
  84. func (c *fileCache) Size() int64 {
  85. c.mu.Lock()
  86. defer c.mu.Unlock()
  87. return c.totalSizeCurrent
  88. }
  89. func (c *fileCache) Remaining() int64 {
  90. c.mu.Lock()
  91. defer c.mu.Unlock()
  92. remaining := c.totalSizeLimit - c.totalSizeCurrent
  93. if remaining < 0 {
  94. return 0
  95. }
  96. return remaining
  97. }
  98. func dirSize(dir string) (int64, error) {
  99. entries, err := os.ReadDir(dir)
  100. if err != nil {
  101. return 0, err
  102. }
  103. var size int64
  104. for _, e := range entries {
  105. info, err := e.Info()
  106. if err != nil {
  107. return 0, err
  108. }
  109. size += info.Size()
  110. }
  111. return size, nil
  112. }