Three nested if statements seems like a lot. How about just adding code to split the original string into filename and path components, and then apply the existing test to the path?

Something like:

  var path = filename.substring(0, filename.lastIndexOf('/')+1);

I think that works even for the case of relative filenames with no / characters at all.