How about checking extension here before trimming it? If no extension, just return filename without calling replace.