setup.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package cmd
  2. import (
  3. "fmt"
  4. "io"
  5. "io/fs"
  6. "os"
  7. "path"
  8. "path/filepath"
  9. "github.com/owncast/owncast/logging"
  10. "github.com/owncast/owncast/services/config"
  11. "github.com/owncast/owncast/static"
  12. "github.com/owncast/owncast/utils"
  13. "github.com/pkg/errors"
  14. log "github.com/sirupsen/logrus"
  15. )
  16. func (app *Application) createDirectories() {
  17. // Create the data directory if needed
  18. if !utils.DoesFileExists("data") {
  19. if err := os.Mkdir("./data", 0o700); err != nil {
  20. log.Fatalln("Cannot create data directory", err)
  21. }
  22. }
  23. // Recreate the temp dir
  24. if utils.DoesFileExists(app.configservice.TempDir) {
  25. err := os.RemoveAll(app.configservice.TempDir)
  26. if err != nil {
  27. log.Fatalln("Unable to remove temp dir! Check permissions.", app.configservice.TempDir, err)
  28. }
  29. }
  30. if err := os.Mkdir(app.configservice.TempDir, 0o700); err != nil {
  31. log.Fatalln("Unable to create temp dir!", err)
  32. }
  33. }
  34. func (app *Application) configureLogging(enableDebugFeatures bool, enableVerboseLogging bool, logDirectory string) {
  35. logging.Setup(enableDebugFeatures, enableVerboseLogging, logDirectory)
  36. log.SetFormatter(&log.TextFormatter{
  37. FullTimestamp: true,
  38. })
  39. }
  40. // setupEmojiDirectory sets up the custom emoji directory by copying all built-in
  41. // emojis if the directory does not yet exist.
  42. func (app *Application) setupEmojiDirectory() (err error) {
  43. type emojiDirectory struct {
  44. path string
  45. isDir bool
  46. }
  47. // Migrate old (pre 0.1.0) emoji to new location if they exist.
  48. app.migrateCustomEmojiLocations()
  49. if utils.DoesFileExists(app.configservice.CustomEmojiPath) {
  50. return nil
  51. }
  52. if err = os.MkdirAll(app.configservice.CustomEmojiPath, 0o750); err != nil {
  53. return fmt.Errorf("unable to create custom emoji directory: %w", err)
  54. }
  55. staticFS := static.GetEmoji()
  56. files := []emojiDirectory{}
  57. walkFunction := func(path string, d os.DirEntry, err error) error {
  58. if path == "." {
  59. return nil
  60. }
  61. if d.Name() == "LICENSE.md" {
  62. return nil
  63. }
  64. files = append(files, emojiDirectory{path: path, isDir: d.IsDir()})
  65. return nil
  66. }
  67. if err := fs.WalkDir(staticFS, ".", walkFunction); err != nil {
  68. log.Errorln("unable to fetch emojis: " + err.Error())
  69. return errors.Wrap(err, "unable to fetch embedded emoji files")
  70. }
  71. if err != nil {
  72. return fmt.Errorf("unable to read built-in emoji files: %w", err)
  73. }
  74. // Now copy all built-in emojis to the custom emoji directory
  75. for _, path := range files {
  76. emojiPath := filepath.Join(app.configservice.CustomEmojiPath, path.path)
  77. if path.isDir {
  78. if err := os.Mkdir(emojiPath, 0o700); err != nil {
  79. return errors.Wrap(err, "unable to create emoji directory, check permissions?: "+path.path)
  80. }
  81. continue
  82. }
  83. memFile, staticOpenErr := staticFS.Open(path.path)
  84. if staticOpenErr != nil {
  85. return errors.Wrap(staticOpenErr, "unable to open emoji file from embedded filesystem")
  86. }
  87. // nolint:gosec
  88. diskFile, err := os.Create(emojiPath)
  89. if err != nil {
  90. return fmt.Errorf("unable to create custom emoji file on disk: %w", err)
  91. }
  92. if err != nil {
  93. _ = diskFile.Close()
  94. return fmt.Errorf("unable to open built-in emoji file: %w", err)
  95. }
  96. if _, err = io.Copy(diskFile, memFile); err != nil {
  97. _ = diskFile.Close()
  98. _ = os.Remove(emojiPath)
  99. return fmt.Errorf("unable to copy built-in emoji file to disk: %w", err)
  100. }
  101. if err = diskFile.Close(); err != nil {
  102. _ = os.Remove(emojiPath)
  103. return fmt.Errorf("unable to close custom emoji file on disk: %w", err)
  104. }
  105. }
  106. return nil
  107. }
  108. // MigrateCustomEmojiLocations migrates custom emoji from the old location to the new location.
  109. func (app *Application) migrateCustomEmojiLocations() {
  110. oldLocation := path.Join("webroot", "img", "emoji")
  111. newLocation := path.Join("data", "emoji")
  112. if !utils.DoesFileExists(oldLocation) {
  113. return
  114. }
  115. log.Println("Moving custom emoji directory from", oldLocation, "to", newLocation)
  116. if err := utils.Move(oldLocation, newLocation); err != nil {
  117. log.Errorln("error moving custom emoji directory", err)
  118. }
  119. }
  120. func (app *Application) resetDirectories() {
  121. log.Trace("Resetting file directories to a clean slate.")
  122. // Wipe hls data directory
  123. utils.CleanupDirectory(app.configservice.HLSStoragePath)
  124. // Remove the previous thumbnail
  125. logo := app.configRepository.GetLogoPath()
  126. if utils.DoesFileExists(logo) {
  127. err := utils.Copy(path.Join("data", logo), filepath.Join(config.DataDirectory, "thumbnail.jpg"))
  128. if err != nil {
  129. log.Warnln(err)
  130. }
  131. }
  132. }