mimecache.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (C) 2019-2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package webdavd
  15. import "sync"
  16. type mimeCache struct {
  17. maxSize int
  18. sync.RWMutex
  19. mimeTypes map[string]string
  20. }
  21. var (
  22. mimeTypeCache mimeCache
  23. customMimeTypeMapping map[string]string
  24. )
  25. func (c *mimeCache) addMimeToCache(key, value string) {
  26. c.Lock()
  27. defer c.Unlock()
  28. if key == "" || value == "" {
  29. return
  30. }
  31. if len(c.mimeTypes) >= c.maxSize {
  32. return
  33. }
  34. c.mimeTypes[key] = value
  35. }
  36. func (c *mimeCache) getMimeFromCache(key string) string {
  37. c.RLock()
  38. defer c.RUnlock()
  39. if val, ok := c.mimeTypes[key]; ok {
  40. return val
  41. }
  42. return ""
  43. }