Get full filepath from a link to a file

I’m writing a plugin which will show an audio player given a link to the file in the current vault in a custom code block. I’m struggling to figure out how to find the full path to the file. the path should take into consideration the default location for new attachments setting.


[[myfile.mp3]]

Would this help?

Almost. I got this far, which returns a correct rlative path to the audio file:

	const path = this.app.metadataCache.getFirstLinkpathDest( getLinkpath('Goin Down.mp3' ), 'Goin Down.mp3' )

which gives me: test/Assets/Goin Down.mp3, a correct relative path to the file. But when I pass it to Wavesurfer I get: app://obsidian.md/audicle/test/Assets/Goin%20Down.mp3, ERR_FILE_NOT_FOUND, which seems reasonable because it needs a url, but then there’s the api:// thing

Inspecting a native audio embed it also uses api:// protocol so presumably this is an Obsidian thing. So my question now is, how do I get an api:// style url from the string that I now have.

Got it:

			const path = this.app.metadataCache.getFirstLinkpathDest( getLinkpath('goin Down.mpr' ), 'Goin Down.mp3' )
			const url = this.app.vault.getResourcePath( path )

gives me: app://e1bf0a97fcf63d82ada732400804026943eb/Users/kimaldis/Documents/Obsidian%20Vaults/audicle/test/Assets/Goin%20Down.mp3?1692101131139

Which Wavesurfewr is happy with.

Oh I have to mention that the function getLinkpath() does nothing with your case. According to the API doc and my test, it will only convert "My note#Heading" to "My note".

The first param of the getFirstLinkPathDest() is the name of your target file, for example 'Goin Down.mp3', which also presents in the internal links. The second param sourcePath is used to resolve relative links. You can put the default attachment folder (access via app.vault.config.attachmentFolderPath) into it to get a more accurate match for your specific case.