diff --git a/local/local.go b/local/local.go index fed260b..abf3793 100644 --- a/local/local.go +++ b/local/local.go @@ -18,6 +18,7 @@ import ( type SyncContext struct { DryRun bool + KeepCache bool TempDir string LbrynetAddr string ChannelID string @@ -55,6 +56,7 @@ func AddCommand(rootCmd *cobra.Command) { 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.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().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") @@ -128,6 +130,14 @@ func localCmd(cmd *cobra.Command, args []string) { 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") } @@ -228,6 +238,7 @@ func getAbbrevDescription(v SourceVideo) string { type VideoSource interface { GetVideo(id string) (*SourceVideo, error) + DeleteLocalCache(id string) error } type VideoPublisher interface { diff --git a/local/ytdl.go b/local/ytdl.go index 712aa44..7baa893 100644 --- a/local/ytdl.go +++ b/local/ytdl.go @@ -49,6 +49,7 @@ func (y *Ytdl) GetVideoMetadata(videoID string) (*ytdl.YtdlVideo, error) { return metadata, nil } + func (y *Ytdl) GetVideoMetadataFile(videoID string) (string, error) { basePath := path.Join(y.DownloadDir, videoID) metadataPath := basePath + ".info.json" @@ -101,6 +102,42 @@ func (y *Ytdl) GetVideoFile(videoID string) (string, error) { 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) { files, err := ioutil.ReadDir(videoDir) if err != nil { diff --git a/local/ytdlVideoSource.go b/local/ytdlVideoSource.go index a01b371..b68c18f 100644 --- a/local/ytdlVideoSource.go +++ b/local/ytdlVideoSource.go @@ -70,3 +70,7 @@ func (s *YtdlVideoSource) GetVideo(id string) (*SourceVideo, error) { return &sourceVideo, nil } + +func (s *YtdlVideoSource) DeleteLocalCache(id string) error { + return s.downloader.DeleteVideoFiles(id) +}