filesystem.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package filesystem
  2. import (
  3. "crypto/rand"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "regexp"
  9. "strconv"
  10. "github.com/knadh/listmonk/internal/media"
  11. )
  12. const tmpFilePrefix = "listmonk"
  13. // Opts represents filesystem params
  14. type Opts struct {
  15. UploadPath string `koanf:"upload_path"`
  16. UploadURI string `koanf:"upload_uri"`
  17. RootURL string `koanf:"root_url"`
  18. }
  19. // Client implements `media.Store`
  20. type Client struct {
  21. opts Opts
  22. }
  23. // This matches filenames, sans extensions, of the format
  24. // filename_(number). The number is incremented in case
  25. // new file uploads conflict with existing filenames
  26. // on the filesystem.
  27. var fnameRegexp = regexp.MustCompile(`(.+?)_([0-9]+)$`)
  28. // NewDiskStore initialises store for Filesystem provider.
  29. func NewDiskStore(opts Opts) (media.Store, error) {
  30. return &Client{
  31. opts: opts,
  32. }, nil
  33. }
  34. // Put accepts the filename, the content type and file object itself and stores the file in disk.
  35. func (c *Client) Put(filename string, cType string, src io.ReadSeeker) (string, error) {
  36. var out *os.File
  37. // Get the directory path
  38. dir := getDir(c.opts.UploadPath)
  39. filename = assertUniqueFilename(dir, filename)
  40. o, err := os.OpenFile(filepath.Join(dir, filename), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0664)
  41. if err != nil {
  42. return "", err
  43. }
  44. out = o
  45. defer out.Close()
  46. if _, err := io.Copy(out, src); err != nil {
  47. return "", err
  48. }
  49. return filename, nil
  50. }
  51. // Get accepts a filename and retrieves the full path from disk.
  52. func (c *Client) Get(name string) string {
  53. return fmt.Sprintf("%s%s/%s", c.opts.RootURL, c.opts.UploadURI, name)
  54. }
  55. // Delete accepts a filename and removes it from disk.
  56. func (c *Client) Delete(file string) error {
  57. dir := getDir(c.opts.UploadPath)
  58. err := os.Remove(filepath.Join(dir, file))
  59. if err != nil {
  60. return err
  61. }
  62. return nil
  63. }
  64. // assertUniqueFilename takes a file path and check if it exists on the disk. If it doesn't,
  65. // it returns the same name and if it does, it adds a small random hash to the filename
  66. // and returns that.
  67. func assertUniqueFilename(dir, fileName string) string {
  68. var (
  69. ext = filepath.Ext(fileName)
  70. base = fileName[0 : len(fileName)-len(ext)]
  71. num = 0
  72. )
  73. for {
  74. // There's no name conflict.
  75. if _, err := os.Stat(filepath.Join(dir, fileName)); os.IsNotExist(err) {
  76. return fileName
  77. }
  78. // Does the name match the _(num) syntax?
  79. r := fnameRegexp.FindAllStringSubmatch(fileName, -1)
  80. if len(r) == 1 && len(r[0]) == 3 {
  81. num, _ = strconv.Atoi(r[0][2])
  82. }
  83. num++
  84. fileName = fmt.Sprintf("%s_%d%s", base, num, ext)
  85. }
  86. }
  87. // generateRandomString generates a cryptographically random, alphanumeric string of length n.
  88. func generateRandomString(n int) (string, error) {
  89. const dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  90. var bytes = make([]byte, n)
  91. if _, err := rand.Read(bytes); err != nil {
  92. return "", err
  93. }
  94. for k, v := range bytes {
  95. bytes[k] = dictionary[v%byte(len(dictionary))]
  96. }
  97. return string(bytes), nil
  98. }
  99. // getDir returns the current working directory path if no directory is specified,
  100. // else returns the directory path specified itself.
  101. func getDir(dir string) string {
  102. if dir == "" {
  103. dir, _ = os.Getwd()
  104. }
  105. return dir
  106. }