1

I have this kind of link in my Node.js script:

148414929_307508464041827_8013797938118488137_n.mp4.m4a?_nc_ht=scontent-mxp1-1.cdninstagram.com&_nc_ohc=_--i1eVUUXoAX9lJQ-u&ccb=7-4&oe=60835C8D&oh=61973532a48cb4fb62ac6711e7eba82f&_nc_sid=fa

I'm trying using this code to get the name to save it as an audio file but I'm not able to get the file extension .mp4.m4a from the URL:

const filename = path.basename(data.message.voice_media.media.audio.audio_src);

How I can get the file extension to remove the last part of the URL correctly? I'm able to save the files and if I remove the part of the name before the desired extension it will play without problems.

UPDATE

As suggested in the comments, I've read the linked question but in my case, I don't need to get only the file extension but the first part of the URL that already contains the needed audio extension that is: 148414929_307508464041827_8013797938118488137_n.mp4.m4a.

sergdenisov
  • 8,327
  • 9
  • 48
  • 63
newbiedev
  • 2,607
  • 3
  • 17
  • 65
  • Does this answer your question? [Node.js get file extension](https://stackoverflow.com/questions/10865347/node-js-get-file-extension) – sergdenisov Mar 23 '21 at 16:27
  • no, it will not work. Using the `path.extname()` will return to me the wrong part of the url and not the filename that is `148414929_307508464041827_8013797938118488137_n.mp4.m4a` – newbiedev Mar 23 '21 at 16:35
  • Do you have an absolute URL? Something like `https://test.com/148414929_307508464041827_8013797938118488137_n.mp4.m4a?_nc_ht=scontent-mxp1-1.cdninstagram.com&_nc_ohc=_--i1eVUUXoAX9lJQ-u&ccb=7-4&oe=60835C8D&oh=61973532a48cb4fb62ac6711e7eba82f&_nc_sid=fa`? – sergdenisov Mar 23 '21 at 16:59
  • yes, the initial url before passing it to `basename` is absolute. – newbiedev Mar 23 '21 at 17:04
  • 1
    After your edit, it seems like you need not an extension, but a pathname. Try this one: `new URL('https://test.com/148414929_307508464041827_8013797938118488137_n.mp4.m4a?_nc_ht=scontent-mxp1-1.cdninstagram.com&_nc_ohc=_--i1eVUUXoAX9lJQ-u&ccb=7-4&oe=60835C8D&oh=61973532a48cb4fb62ac6711e7eba82f&_nc_sid=fa').pathname.replace('/', '')` – sergdenisov Mar 23 '21 at 17:09
  • yeah, works fine, thank you!If you add an answer I will accept it – newbiedev Mar 23 '21 at 17:18
  • 1
    Added some improvements and posted. – sergdenisov Mar 23 '21 at 17:45

1 Answers1

3

I recommend using URL() constructor (works both in browsers and Node.js) because you can be sure your URL is valid:

const url = 'https://test.com/path/148414929_307508464041827_8013797938118488137_n.mp4.m4a?_nc_ht=scontent-mxp1-1.cdninstagram.com&_nc_ohc=_--i1eVUUXoAX9lJQ-u&ccb=7-4&oe=60835C8D&oh=61973532a48cb4fb62ac6711e7eba82f&_nc_sid=fa';

let filename = '';
try {
  filename = new URL(url).pathname.split('/').pop();
} catch (e) {
  console.error(e);
}
console.log(`filename: ${filename}`);
sergdenisov
  • 8,327
  • 9
  • 48
  • 63
  • why you've used `.split()` instead of replace like the comment? – newbiedev Mar 23 '21 at 19:37
  • 2
    @newbiedev because it doesn’t work with a case when your file lies not on the root location. I specially pointed it out in my example. I mean when you have not `/file.txt`, but `/path/file.txt`. – sergdenisov Mar 23 '21 at 20:00
  • 1
    ok,the input url is something like `https://scontent-mxp1-1.cdninstagram.com/v/t69.10824-16/164227760_3862884277124080_4358374852660862001_n.mp4.m4a?_nc_ht=scontent-mxp1-1.cdninstagram.com&_nc_ohc=vsuob2J4YmYAX_eQu61&ccb=7-4&oe=6084423C&oh=1b146860997bfc0480342ffe797b7b&_nc_sid=195af5` so I suppose your solution will be fine – newbiedev Mar 24 '21 at 03:27