Skip to content

Commit

Permalink
refactor: use file-url-provider
Browse files Browse the repository at this point in the history
  • Loading branch information
altaywtf committed Sep 19, 2024
1 parent 80b6752 commit 3b3ff16
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 70 deletions.
3 changes: 1 addition & 2 deletions src/api/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions src/api/withPutioClient.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,7 +13,7 @@ export const withPutioClient = (component: JSX.Element) => {

useMemo(() => {
(async function () {
const { token } = getPreferenceValues<Preferences>();
const token = getAuthToken();
putioClient = new PutioAPI({ clientID: 6311 });
putioClient.setToken(token);

Expand Down
97 changes: 38 additions & 59 deletions src/components/FileListItemActions.tsx
Original file line number Diff line number Diff line change
@@ -1,91 +1,70 @@
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 (
<>
{file.file_type === "FOLDER" ? (
<Action.Push title="Open" target={<Files id={file.id} name={file.name} />} icon={Icon.ArrowRight} />
) : null}

{urls?.browse && <Action.OpenInBrowser title="Open in Browser" url={urls.browse} icon="putio.png" />}
{urls?.download && <Action.OpenInBrowser title="Download in Browser" url={urls.download} icon="putio.png" />}
<Action.OpenInBrowser title="Open in Browser" url={browseURL} icon="putio.png" />

{urls?.browse && (
<Action.CopyToClipboard
title="Copy URL"
content={urls.browse}
shortcut={{ modifiers: ["cmd", "shift"], key: "c" }}
/>
)}
{downloadURL && <Action.OpenInBrowser title="Download in Browser" url={downloadURL} icon="putio.png" />}

<Action.CopyToClipboard
title="Copy URL"
content={browseURL}
shortcut={{ modifiers: ["cmd", "shift"], key: "c" }}
/>

{urls?.download && (
{downloadURL && (
<Action.CopyToClipboard
title="Copy Download URL"
content={urls.download}
content={downloadURL}
shortcut={{ modifiers: ["cmd", "shift"], key: "d" }}
/>
)}

{urls?.stream && <Action.CopyToClipboard title="Copy Stream URL" content={urls.stream} />}
{streamURL && <Action.CopyToClipboard title="Copy Stream URL" content={streamURL} />}

{urls?.mp4Stream && <Action.CopyToClipboard title="Copy Mp4 Stream URL" content={urls.mp4Stream} />}
{mp4StreamURL && (
<Action.CopyToClipboard
// eslint-disable-next-line @raycast/prefer-title-case -- MP4 is a proper noun
title="Copy MP4 Stream URL"
content={mp4StreamURL}
/>
)}

{isVlcInstalled && urls?.stream && (
{vlc.isInstalled && streamURL && (
<Action
icon={Icon.AppWindow}
onAction={() => {
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 && (
<Action
icon={Icon.AppWindow}
onAction={() => {
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"
/>
)}
</>
Expand Down
21 changes: 14 additions & 7 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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"),
};
};

0 comments on commit 3b3ff16

Please sign in to comment.