Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"authorUrl": "https://bagerbach.com",
"fundingUrl": "https://buymeacoffee.com/chhoumann",
"isDesktopOnly": false
}
}
63 changes: 63 additions & 0 deletions src/downloadEpisode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Notice, TFile, requestUrl } from "obsidian";
import { downloadedEpisodes } from "./store";
import { DownloadPathTemplateEngine } from "./TemplateEngine";
import type { Episode } from "./types/Episode";
import type { LocalEpisode } from "./types/LocalEpisode";
import { encodeUrlForRequest } from "./utility/encodeUrlForRequest";
import { isLocalFile } from "./utility/isLocalFile";
import getUrlExtension from "./utility/getUrlExtension";
import getExtensionFromContentType from "./utility/getExtensionFromContentType";

Expand Down Expand Up @@ -177,6 +179,56 @@ async function createEpisodeFile({
downloadedEpisodes.addEpisode(episode, filePath, blob.size);
}

function resolveLocalEpisodeFilePath(episode: LocalEpisode): string | null {
const downloadedEpisode = downloadedEpisodes.getEpisode(episode);
const candidatePaths = [
episode.filePath,
downloadedEpisode?.filePath,
getLocalFilePathFromLink(episode.url),
];

for (const possiblePath of candidatePaths) {
if (!possiblePath) continue;

const file = app.vault.getAbstractFileByPath(possiblePath);
if (file instanceof TFile) {
return file.path;
}
}

return null;
}

function getLocalFilePathFromLink(link: string): string | null {
if (!link) return null;

const trimmedLink = link.trim();
if (!trimmedLink) return null;

const innerLink = trimmedLink.match(/^\[\[(.*)\]\]$/)?.[1] ?? trimmedLink;
const [target] = innerLink.split("|");
const normalizedTarget = target?.trim();

if (!normalizedTarget) {
return null;
}

const directFile = app.vault.getAbstractFileByPath(normalizedTarget);
if (directFile instanceof TFile) {
return directFile.path;
}

const linkedFile = app.metadataCache?.getFirstLinkpathDest(
normalizedTarget,
"",
);
if (linkedFile instanceof TFile) {
return linkedFile.path;
}

return null;
}

async function inferFileExtensionFromDownload(
episode: Episode,
blob: Blob,
Expand All @@ -198,6 +250,17 @@ export async function downloadEpisode(
episode: Episode,
downloadPathTemplate: string,
): Promise<string> {
if (isLocalFile(episode)) {
const localFilePath = resolveLocalEpisodeFilePath(episode);
if (!localFilePath) {
throw new Error(
`Unable to locate the local audio file for "${episode.title}". Try playing the file again.`,
);
}

return localFilePath;
}

const basename = DownloadPathTemplateEngine(downloadPathTemplate, episode);
const fileExtension = await getFileExtension(episode.streamUrl);
const filePath = `${basename}.${fileExtension}`;
Expand Down
1 change: 1 addition & 0 deletions src/getContextMenuHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default function getContextMenuHandler(app: App): EventRef {
streamUrl: await createMediaUrlObjectFromFilePath(
file.path
),
filePath: file.path,
episodeDate: new Date(file.stat.ctime),
};

Expand Down
7 changes: 4 additions & 3 deletions src/types/LocalEpisode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Episode } from "./Episode";

export interface LocalEpisode extends Episode {
podcastName: "local file",
description: "",
content: ""
podcastName: "local file",
description: "",
content: "",
filePath?: string,
}