support opening video media

This commit is contained in:
Dan Anglin 2024-06-21 21:47:18 +01:00
parent d933a2dc09
commit 196a4709c6
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 46 additions and 15 deletions

View file

@ -33,6 +33,7 @@ func run() error {
cacheDir string cacheDir string
pager string pager string
imageViewer string imageViewer string
videoPlayer string
maxTerminalWidth int maxTerminalWidth int
noColor *bool noColor *bool
) )
@ -40,7 +41,8 @@ func run() error {
flag.StringVar(&configDir, "config-dir", "", "Specify your config directory") flag.StringVar(&configDir, "config-dir", "", "Specify your config directory")
flag.StringVar(&cacheDir, "cache-dir", "", "Specify your cache directory") flag.StringVar(&cacheDir, "cache-dir", "", "Specify your cache directory")
flag.StringVar(&pager, "pager", "", "Specify your preferred pager to page through long outputs. This is disabled by default.") flag.StringVar(&pager, "pager", "", "Specify your preferred pager to page through long outputs. This is disabled by default.")
flag.StringVar(&imageViewer, "image-viewer", "", "Specify your preferred image viewer for viewing images attached to statuses.") flag.StringVar(&imageViewer, "image-viewer", "", "Specify your favourite image viewers.")
flag.StringVar(&videoPlayer, "video-player", "", "Specify your favourite video player.")
flag.IntVar(&maxTerminalWidth, "max-terminal-width", 80, "Specify the maximum terminal width when displaying resources on screen.") flag.IntVar(&maxTerminalWidth, "max-terminal-width", 80, "Specify the maximum terminal width when displaying resources on screen.")
flag.BoolFunc("no-color", "Disable ANSI colour output when displaying text on screen", func(value string) error { flag.BoolFunc("no-color", "Disable ANSI colour output when displaying text on screen", func(value string) error {
@ -176,6 +178,7 @@ func run() error {
configDir, configDir,
cacheDir, cacheDir,
imageViewer, imageViewer,
videoPlayer,
executor.CommandShow, executor.CommandShow,
executor.CommandSummaryLookup(executor.CommandShow), executor.CommandSummaryLookup(executor.CommandShow),
), ),

View file

@ -35,11 +35,12 @@ type ShowExecutor struct {
pollID string pollID string
fromResourceType string fromResourceType string
imageViewer string imageViewer string
videoPlayer string
limit int limit int
attachmentIDs MultiStringFlagValue attachmentIDs MultiStringFlagValue
} }
func NewShowExecutor(printer *printer.Printer, configDir, cacheRoot, imageViewer, name, summary string) *ShowExecutor { func NewShowExecutor(printer *printer.Printer, configDir, cacheRoot, imageViewer, videoPlayer, name, summary string) *ShowExecutor {
showExe := ShowExecutor{ showExe := ShowExecutor{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError), FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
@ -47,6 +48,7 @@ func NewShowExecutor(printer *printer.Printer, configDir, cacheRoot, imageViewer
configDir: configDir, configDir: configDir,
cacheRoot: cacheRoot, cacheRoot: cacheRoot,
imageViewer: imageViewer, imageViewer: imageViewer,
videoPlayer: videoPlayer,
} }
showExe.BoolVar(&showExe.myAccount, flagMyAccount, false, "Set to true to lookup your account") showExe.BoolVar(&showExe.myAccount, flagMyAccount, false, "Set to true to lookup your account")
@ -477,20 +479,29 @@ func (s *ShowExecutor) showMediaFromStatus(gtsClient *client.Client) error {
return fmt.Errorf("unable to ensure the existence of the directory %q: %w", cacheDir, err) return fmt.Errorf("unable to ensure the existence of the directory %q: %w", cacheDir, err)
} }
attachmentsHashMap := make(map[string]string) type media struct {
filepaths := make([]string, len(s.attachmentIDs)) url string
mediaType string
for _, statusAttachment := range status.MediaAttachments {
attachmentsHashMap[statusAttachment.ID] = statusAttachment.URL
} }
for ind, attachmentID := range s.attachmentIDs { attachmentsHashMap := make(map[string]media)
mediaURL, ok := attachmentsHashMap[attachmentID] imageFiles := make([]string, 0)
videoFiles := make([]string, 0)
for _, statusAttachment := range status.MediaAttachments {
attachmentsHashMap[statusAttachment.ID] = media{
url: statusAttachment.URL,
mediaType: statusAttachment.Type,
}
}
for _, attachmentID := range s.attachmentIDs {
mediaObj, ok := attachmentsHashMap[attachmentID]
if !ok { if !ok {
return fmt.Errorf("unknown media attachment: %s", attachmentID) return fmt.Errorf("unknown media attachment: %s", attachmentID)
} }
split := strings.Split(mediaURL, "/") split := strings.Split(mediaObj.url, "/")
filename := split[len(split)-1] filename := split[len(split)-1]
filePath := filepath.Join(cacheDir, filename) filePath := filepath.Join(cacheDir, filename)
@ -504,16 +515,33 @@ func (s *ShowExecutor) showMediaFromStatus(gtsClient *client.Client) error {
} }
if !fileExists { if !fileExists {
if err := gtsClient.DownloadMedia(mediaURL, filePath); err != nil { if err := gtsClient.DownloadMedia(mediaObj.url, filePath); err != nil {
return fmt.Errorf("unable to download the media attachment for %s: %w", attachmentID, err) return fmt.Errorf(
"unable to download the media attachment for %s: %w",
attachmentID,
err,
)
} }
} }
filepaths[ind] = filePath switch mediaObj.mediaType {
case "image":
imageFiles = append(imageFiles, filePath)
case "video":
videoFiles = append(videoFiles, filePath)
}
} }
if err := utilities.OpenMedia(s.imageViewer, filepaths); err != nil { if len(imageFiles) > 0 {
return fmt.Errorf("unable to open the image viewer: %w", err) if err := utilities.OpenMedia(s.imageViewer, imageFiles); err != nil {
return fmt.Errorf("unable to open the image viewer: %w", err)
}
}
if len(videoFiles) > 0 {
if err := utilities.OpenMedia(s.videoPlayer, videoFiles); err != nil {
return fmt.Errorf("unable to open the video player: %w", err)
}
} }
return nil return nil