Fix trailing slashes which leads to segfault.

When filename has trailing slashes, say '/path/to/a/dir/', the current implementation will failed to detect this case and set the filename to '/path/to/a/dir/' instead of 'dir', which will lead to segfault in chat_onFileData.