2022-01-07 16:49:28 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-07-08 17:00:04 +03:00
|
|
|
"fmt"
|
2023-01-07 17:34:02 +03:00
|
|
|
"heckel.io/ntfy/log"
|
2022-01-07 16:49:28 +03:00
|
|
|
"heckel.io/ntfy/util"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-07-08 17:00:04 +03:00
|
|
|
fileIDRegex = regexp.MustCompile(fmt.Sprintf(`^[-_A-Za-z0-9]{%d}$`, messageIDLength))
|
2022-01-07 16:49:28 +03:00
|
|
|
errInvalidFileID = errors.New("invalid file ID")
|
2022-01-08 20:14:43 +03:00
|
|
|
errFileExists = errors.New("file exists")
|
2022-01-07 16:49:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type fileCache struct {
|
|
|
|
dir string
|
|
|
|
totalSizeCurrent int64
|
|
|
|
totalSizeLimit int64
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
2022-12-21 21:19:07 +03:00
|
|
|
func newFileCache(dir string, totalSizeLimit int64) (*fileCache, error) {
|
2022-01-07 16:49:28 +03:00
|
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-07 17:15:33 +03:00
|
|
|
size, err := dirSize(dir)
|
2022-01-07 16:49:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &fileCache{
|
|
|
|
dir: dir,
|
|
|
|
totalSizeCurrent: size,
|
|
|
|
totalSizeLimit: totalSizeLimit,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-01-13 01:03:28 +03:00
|
|
|
func (c *fileCache) Write(id string, in io.Reader, limiters ...util.Limiter) (int64, error) {
|
2022-01-07 16:49:28 +03:00
|
|
|
if !fileIDRegex.MatchString(id) {
|
|
|
|
return 0, errInvalidFileID
|
|
|
|
}
|
2023-02-06 07:34:27 +03:00
|
|
|
log.Tag(tagFileCache).Field("message_id", id).Debug("Writing attachment")
|
2022-01-07 16:49:28 +03:00
|
|
|
file := filepath.Join(c.dir, id)
|
2022-01-08 20:14:43 +03:00
|
|
|
if _, err := os.Stat(file); err == nil {
|
|
|
|
return 0, errFileExists
|
|
|
|
}
|
2022-01-07 16:49:28 +03:00
|
|
|
f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2022-12-21 21:19:07 +03:00
|
|
|
limiters = append(limiters, util.NewFixedLimiter(c.Remaining()))
|
2022-01-07 16:49:28 +03:00
|
|
|
limitWriter := util.NewLimitWriter(f, limiters...)
|
|
|
|
size, err := io.Copy(limitWriter, in)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(file)
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
os.Remove(file)
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
c.mu.Lock()
|
|
|
|
c.totalSizeCurrent += size
|
2023-03-16 05:34:06 +03:00
|
|
|
mset(metricAttachmentsTotalSize, c.totalSizeCurrent)
|
2022-01-07 16:49:28 +03:00
|
|
|
c.mu.Unlock()
|
|
|
|
return size, nil
|
|
|
|
}
|
|
|
|
|
2022-01-08 20:14:43 +03:00
|
|
|
func (c *fileCache) Remove(ids ...string) error {
|
2022-01-07 17:15:33 +03:00
|
|
|
for _, id := range ids {
|
2022-01-10 06:06:31 +03:00
|
|
|
if !fileIDRegex.MatchString(id) {
|
|
|
|
return errInvalidFileID
|
2022-01-07 17:15:33 +03:00
|
|
|
}
|
2023-02-06 07:34:27 +03:00
|
|
|
log.Tag(tagFileCache).Field("message_id", id).Debug("Deleting attachment")
|
2022-01-10 06:06:31 +03:00
|
|
|
file := filepath.Join(c.dir, id)
|
2023-01-07 17:34:02 +03:00
|
|
|
if err := os.Remove(file); err != nil {
|
2023-02-06 07:34:27 +03:00
|
|
|
log.Tag(tagFileCache).Field("message_id", id).Err(err).Debug("Error deleting attachment")
|
2023-01-07 17:34:02 +03:00
|
|
|
}
|
2022-01-07 17:15:33 +03:00
|
|
|
}
|
|
|
|
size, err := dirSize(c.dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.mu.Lock()
|
|
|
|
c.totalSizeCurrent = size
|
|
|
|
c.mu.Unlock()
|
2023-03-16 05:34:06 +03:00
|
|
|
mset(metricAttachmentsTotalSize, size)
|
2022-01-10 06:06:31 +03:00
|
|
|
return nil
|
2022-01-07 17:15:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fileCache) Size() int64 {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
return c.totalSizeCurrent
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fileCache) Remaining() int64 {
|
2022-01-07 16:49:28 +03:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
remaining := c.totalSizeLimit - c.totalSizeCurrent
|
|
|
|
if remaining < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return remaining
|
|
|
|
}
|
2022-01-07 17:15:33 +03:00
|
|
|
|
|
|
|
func dirSize(dir string) (int64, error) {
|
|
|
|
entries, err := os.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
var size int64
|
|
|
|
for _, e := range entries {
|
|
|
|
info, err := e.Info()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
size += info.Size()
|
|
|
|
}
|
|
|
|
return size, nil
|
|
|
|
}
|