Clean up local cache after publishing stream

This commit is contained in:
pseudoscalar 2021-11-05 11:22:20 -05:00
parent e564dc8445
commit 9f6b15e841
3 changed files with 52 additions and 0 deletions

View file

@ -18,6 +18,7 @@ import (
type SyncContext struct { type SyncContext struct {
DryRun bool DryRun bool
KeepCache bool
TempDir string TempDir string
LbrynetAddr string LbrynetAddr string
ChannelID string ChannelID string
@ -55,6 +56,7 @@ func AddCommand(rootCmd *cobra.Command) {
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
} }
cmd.Flags().BoolVar(&syncContext.DryRun, "dry-run", false, "Display information about the stream publishing, but do not publish the stream") cmd.Flags().BoolVar(&syncContext.DryRun, "dry-run", false, "Display information about the stream publishing, but do not publish the stream")
cmd.Flags().BoolVar(&syncContext.KeepCache, "keep-cache", false, "Don't delete local files after publishing.")
cmd.Flags().StringVar(&syncContext.TempDir, "temp-dir", getEnvDefault("TEMP_DIR", ""), "directory to use for temporary files") cmd.Flags().StringVar(&syncContext.TempDir, "temp-dir", getEnvDefault("TEMP_DIR", ""), "directory to use for temporary files")
cmd.Flags().Float64Var(&syncContext.PublishBid, "publish-bid", 0.01, "Bid amount for the stream claim") cmd.Flags().Float64Var(&syncContext.PublishBid, "publish-bid", 0.01, "Bid amount for the stream claim")
cmd.Flags().StringVar(&syncContext.LbrynetAddr, "lbrynet-address", getEnvDefault("LBRYNET_ADDRESS", ""), "JSONRPC address of the local LBRYNet daemon") cmd.Flags().StringVar(&syncContext.LbrynetAddr, "lbrynet-address", getEnvDefault("LBRYNET_ADDRESS", ""), "JSONRPC address of the local LBRYNet daemon")
@ -128,6 +130,14 @@ func localCmd(cmd *cobra.Command, args []string) {
log.Errorf("Error while wating for stream to reflect: %v", err) log.Errorf("Error while wating for stream to reflect: %v", err)
} }
} }
if !syncContext.KeepCache {
log.Infof("Deleting local files.")
err = videoSource.DeleteLocalCache(videoID)
if err != nil {
log.Errorf("Error deleting local files for video %s: %v", videoID, err)
}
}
log.Info("Done") log.Info("Done")
} }
@ -228,6 +238,7 @@ func getAbbrevDescription(v SourceVideo) string {
type VideoSource interface { type VideoSource interface {
GetVideo(id string) (*SourceVideo, error) GetVideo(id string) (*SourceVideo, error)
DeleteLocalCache(id string) error
} }
type VideoPublisher interface { type VideoPublisher interface {

View file

@ -49,6 +49,7 @@ func (y *Ytdl) GetVideoMetadata(videoID string) (*ytdl.YtdlVideo, error) {
return metadata, nil return metadata, nil
} }
func (y *Ytdl) GetVideoMetadataFile(videoID string) (string, error) { func (y *Ytdl) GetVideoMetadataFile(videoID string) (string, error) {
basePath := path.Join(y.DownloadDir, videoID) basePath := path.Join(y.DownloadDir, videoID)
metadataPath := basePath + ".info.json" metadataPath := basePath + ".info.json"
@ -101,6 +102,42 @@ func (y *Ytdl) GetVideoFile(videoID string) (string, error) {
return *videoPath, nil return *videoPath, nil
} }
func (y *Ytdl) DeleteVideoFiles(videoID string) error {
files, err := ioutil.ReadDir(y.DownloadDir)
if err != nil {
return err
}
for _, f := range files {
if f.IsDir() {
continue
}
if strings.Contains(f.Name(), videoID) {
videoPath := path.Join(y.DownloadDir, f.Name())
err = os.Remove(videoPath)
if err != nil {
log.Errorf("Error while deleting file %s: %v", y.DownloadDir, err)
return err
}
}
}
return nil
}
func deleteFile(path string) error {
_, err := os.Stat(path)
if err != nil && !os.IsNotExist(err) {
log.Errorf("Error determining if file %s exists: %v", path, err)
return err
} else if err != nil {
log.Debugf("File %s does not exist. Skipping deletion.", path)
return nil
}
return os.Remove(path)
}
func findDownloadedVideo(videoDir, videoID string) (*string, error) { func findDownloadedVideo(videoDir, videoID string) (*string, error) {
files, err := ioutil.ReadDir(videoDir) files, err := ioutil.ReadDir(videoDir)
if err != nil { if err != nil {

View file

@ -70,3 +70,7 @@ func (s *YtdlVideoSource) GetVideo(id string) (*SourceVideo, error) {
return &sourceVideo, nil return &sourceVideo, nil
} }
func (s *YtdlVideoSource) DeleteLocalCache(id string) error {
return s.downloader.DeleteVideoFiles(id)
}