file_cache.go 2.6 KB

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