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
pager string
imageViewer string
videoPlayer string
maxTerminalWidth int
noColor *bool
)
@ -40,7 +41,8 @@ func run() error {
flag.StringVar(&configDir, "config-dir", "", "Specify your config 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(&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.BoolFunc("no-color", "Disable ANSI colour output when displaying text on screen", func(value string) error {
@ -176,6 +178,7 @@ func run() error {
configDir,
cacheDir,
imageViewer,
videoPlayer,
executor.CommandShow,
executor.CommandSummaryLookup(executor.CommandShow),
),

View file

@ -35,11 +35,12 @@ type ShowExecutor struct {
pollID string
fromResourceType string
imageViewer string
videoPlayer string
limit int
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{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
@ -47,6 +48,7 @@ func NewShowExecutor(printer *printer.Printer, configDir, cacheRoot, imageViewer
configDir: configDir,
cacheRoot: cacheRoot,
imageViewer: imageViewer,
videoPlayer: videoPlayer,
}
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)
}
attachmentsHashMap := make(map[string]string)
filepaths := make([]string, len(s.attachmentIDs))
for _, statusAttachment := range status.MediaAttachments {
attachmentsHashMap[statusAttachment.ID] = statusAttachment.URL
type media struct {
url string
mediaType string
}
for ind, attachmentID := range s.attachmentIDs {
mediaURL, ok := attachmentsHashMap[attachmentID]
attachmentsHashMap := make(map[string]media)
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 {
return fmt.Errorf("unknown media attachment: %s", attachmentID)
}
split := strings.Split(mediaURL, "/")
split := strings.Split(mediaObj.url, "/")
filename := split[len(split)-1]
filePath := filepath.Join(cacheDir, filename)
@ -504,17 +515,34 @@ func (s *ShowExecutor) showMediaFromStatus(gtsClient *client.Client) error {
}
if !fileExists {
if err := gtsClient.DownloadMedia(mediaURL, filePath); err != nil {
return fmt.Errorf("unable to download the media attachment for %s: %w", attachmentID, err)
if err := gtsClient.DownloadMedia(mediaObj.url, filePath); err != nil {
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 {
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
}