diff --git a/src/api/files.ts b/src/api/files.ts index 9bcab04..6c6ba8c 100644 --- a/src/api/files.ts +++ b/src/api/files.ts @@ -3,8 +3,7 @@ import { getPutioClient } from "./withPutioClient"; export const fetchFiles = async (id: number) => { const response = await getPutioClient().Files.Query(id, { - streamUrl: true, - mp4StreamUrl: true, + mp4Status: true, }); return { diff --git a/src/api/withPutioClient.tsx b/src/api/withPutioClient.tsx index 803c9ed..13889a2 100644 --- a/src/api/withPutioClient.tsx +++ b/src/api/withPutioClient.tsx @@ -1,8 +1,9 @@ -import { getPreferenceValues, showToast } from "@raycast/api"; +import { showToast } from "@raycast/api"; import { Detail } from "@raycast/api"; import { useState, useMemo } from "react"; import PutioAPI, { IAccountInfo } from "@putdotio/api-client"; import { localizeError, localizedErrorToToastOptions } from "./localizeError"; +import { getAuthToken } from "../utils"; let putioClient: PutioAPI | null = null; let accountInfo: IAccountInfo | null = null; @@ -12,7 +13,7 @@ export const withPutioClient = (component: JSX.Element) => { useMemo(() => { (async function () { - const { token } = getPreferenceValues(); + const token = getAuthToken(); putioClient = new PutioAPI({ clientID: 6311 }); putioClient.setToken(token); diff --git a/src/components/FileListItemActions.tsx b/src/components/FileListItemActions.tsx index 5851b2c..443b731 100644 --- a/src/components/FileListItemActions.tsx +++ b/src/components/FileListItemActions.tsx @@ -1,44 +1,20 @@ -import { exec } from "child_process"; import { ActionPanel, Action, Icon, confirmAlert, showToast, Toast, Alert } from "@raycast/api"; -import { useCachedPromise } from "@raycast/utils"; import type { IFile } from "@putdotio/api-client"; -import { getPutioAccountInfo, getPutioClient } from "../api/withPutioClient"; +import { getPutioAccountInfo } from "../api/withPutioClient"; import { Files } from "../files"; import { RenameFile } from "../rename-file"; import { deleteFile } from "../api/files"; -import { useIsVlcInstalled } from "../utils"; - -const fetchFileDownloadURL = async (file: IFile) => { - try { - switch (file.file_type) { - case "IMAGE": - case "VIDEO": { - const response = await getPutioClient().get(`/files/${file.id}/url`); - return response.data.url as string; - } - - default: - return null; - } - } catch (error) { - return null; - } -}; - -const fetchFileURLs = async (file: IFile) => { - const download = await fetchFileDownloadURL(file); - - return { - download, - browse: `https://put.io/files/${file.id}`, - stream: file.stream_url, - mp4Stream: file.mp4_stream_url, - }; -}; +import { getAuthToken, useVLC } from "../utils"; +import { FileURLProvider } from "@putdotio/utilities"; +import PutioAPIClient from "@putdotio/api-client"; export const FileListItemNavigationActions = ({ file }: { file: IFile }) => { - const isVlcInstalled = useIsVlcInstalled(); - const { data: urls } = useCachedPromise(fetchFileURLs, [file]); + const vlc = useVLC(); + const fileURLProvider = new FileURLProvider(PutioAPIClient.DEFAULT_OPTIONS.baseURL!, getAuthToken()); + const browseURL = `https://put.io/files/${file.id}`; + const downloadURL = fileURLProvider.getDownloadURL(file.id); + const streamURL = fileURLProvider.getStreamURL(file); + const mp4StreamURL = fileURLProvider.getMP4StreamURL(file); return ( <> @@ -46,46 +22,49 @@ export const FileListItemNavigationActions = ({ file }: { file: IFile }) => { } icon={Icon.ArrowRight} /> ) : null} - {urls?.browse && } - {urls?.download && } + - {urls?.browse && ( - - )} + {downloadURL && } + + - {urls?.download && ( + {downloadURL && ( )} - {urls?.stream && } + {streamURL && } - {urls?.mp4Stream && } + {mp4StreamURL && ( + + )} - {isVlcInstalled && urls?.stream && ( + {vlc.isInstalled && streamURL && ( { - exec(`vlc "${urls.stream}"`); - }} - title="Open in Vlc" + icon={Icon.FilmStrip} + onAction={() => vlc.open(streamURL)} + // eslint-disable-next-line @raycast/prefer-title-case -- VLC is a proper noun + title="Open in VLC" /> )} - {isVlcInstalled && urls?.mp4Stream && ( + {vlc.isInstalled && mp4StreamURL && ( { - exec(`vlc "${urls.mp4Stream}"`); - }} - title="Open Mp4 in Vlc" + icon={Icon.FilmStrip} + onAction={() => vlc.open(mp4StreamURL)} + // eslint-disable-next-line @raycast/prefer-title-case -- VLC is a proper noun + title="Open MP4 in VLC" /> )} diff --git a/src/utils/index.ts b/src/utils/index.ts index 5e4de61..1738a86 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,19 +1,26 @@ +import { getPreferenceValues } from "@raycast/api"; import { useEffect, useState } from "react"; -import { getApplications } from "@raycast/api"; +import { getApplications, open } from "@raycast/api"; -export const useIsVlcInstalled = () => { - const [isVlcInstalled, setIsVlcInstalled] = useState(false); +export const getAuthToken = () => { + const { token } = getPreferenceValues<{ token: string }>(); + return token; +}; + +export const useVLC = () => { + const [installed, setInstalled] = useState(false); useEffect(() => { const checkIfVlcInstalled = async () => { const applications = await getApplications(); - const vlcIsInstalled = applications.some(({ bundleId }) => bundleId === "org.videolan.vlc"); - - setIsVlcInstalled(vlcIsInstalled); + setInstalled(applications.some(({ bundleId }) => bundleId === "org.videolan.vlc")); }; checkIfVlcInstalled(); }, []); - return isVlcInstalled; + return { + isInstalled: installed, + open: (url: string) => open(url, "org.videolan.vlc"), + }; };