local.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package storageproviders
  2. import (
  3. "os"
  4. "path/filepath"
  5. "sort"
  6. "github.com/pkg/errors"
  7. log "github.com/sirupsen/logrus"
  8. "github.com/owncast/owncast/config"
  9. "github.com/owncast/owncast/core/data"
  10. )
  11. // LocalStorage represents an instance of the local storage provider for HLS video.
  12. type LocalStorage struct {
  13. host string
  14. }
  15. // NewLocalStorage returns a new LocalStorage instance.
  16. func NewLocalStorage() *LocalStorage {
  17. return &LocalStorage{}
  18. }
  19. // Setup configures this storage provider.
  20. func (s *LocalStorage) Setup() error {
  21. s.host = data.GetVideoServingEndpoint()
  22. return nil
  23. }
  24. // SegmentWritten is called when a single segment of video is written.
  25. func (s *LocalStorage) SegmentWritten(localFilePath string) {
  26. if _, err := s.Save(localFilePath, 0); err != nil {
  27. log.Warnln(err)
  28. }
  29. }
  30. // VariantPlaylistWritten is called when a variant hls playlist is written.
  31. func (s *LocalStorage) VariantPlaylistWritten(localFilePath string) {
  32. if _, err := s.Save(localFilePath, 0); err != nil {
  33. log.Errorln(err)
  34. return
  35. }
  36. }
  37. // MasterPlaylistWritten is called when the master hls playlist is written.
  38. func (s *LocalStorage) MasterPlaylistWritten(localFilePath string) {
  39. // If we're using a remote serving endpoint, we need to rewrite the master playlist
  40. if s.host != "" {
  41. if err := rewritePlaylistLocations(localFilePath, s.host, ""); err != nil {
  42. log.Warnln(err)
  43. }
  44. } else {
  45. if _, err := s.Save(localFilePath, 0); err != nil {
  46. log.Warnln(err)
  47. }
  48. }
  49. }
  50. // Save will save a local filepath using the storage provider.
  51. func (s *LocalStorage) Save(filePath string, retryCount int) (string, error) {
  52. return filePath, nil
  53. }
  54. func (s *LocalStorage) Cleanup() error {
  55. // Determine how many files we should keep on disk
  56. maxNumber := data.GetStreamLatencyLevel().SegmentCount
  57. buffer := 10
  58. baseDirectory := config.HLSStoragePath
  59. files, err := getAllFilesRecursive(baseDirectory)
  60. if err != nil {
  61. return errors.Wrap(err, "unable find old video files for cleanup")
  62. }
  63. // Delete old private HLS files on disk
  64. for directory := range files {
  65. files := files[directory]
  66. if len(files) < maxNumber+buffer {
  67. continue
  68. }
  69. filesToDelete := files[maxNumber+buffer:]
  70. log.Traceln("Deleting", len(filesToDelete), "old files from", baseDirectory, "for video variant", directory)
  71. for _, file := range filesToDelete {
  72. fileToDelete := filepath.Join(baseDirectory, directory, file.Name())
  73. err := os.Remove(fileToDelete)
  74. if err != nil {
  75. return errors.Wrap(err, "unable to delete old video files")
  76. }
  77. }
  78. }
  79. return nil
  80. }
  81. func getAllFilesRecursive(baseDirectory string) (map[string][]os.FileInfo, error) {
  82. files := make(map[string][]os.FileInfo)
  83. var directory string
  84. err := filepath.Walk(baseDirectory, func(path string, info os.FileInfo, err error) error {
  85. if err != nil {
  86. return err
  87. }
  88. if info.IsDir() {
  89. directory = info.Name()
  90. }
  91. if filepath.Ext(info.Name()) == ".ts" {
  92. files[directory] = append(files[directory], info)
  93. }
  94. return nil
  95. })
  96. if err != nil {
  97. return nil, err
  98. }
  99. // Sort by date so we can delete old files
  100. for directory := range files {
  101. sort.Slice(files[directory], func(i, j int) bool {
  102. return files[directory][i].ModTime().UnixNano() > files[directory][j].ModTime().UnixNano()
  103. })
  104. }
  105. return files, nil
  106. }