I am writing a function to generate a file name to avoid repetitions with a suffix similar to "(1)", which are automatically substituted by most browsers and other programs when downloading a file.
I ran into the problem that the file extension can be not only with one dot, like example.zip, in addition to this, there are such as example.tar.gz And I can't imagine how to handle such cases yet.
At first, I thought of just defining everything after the first dot in the line as an extension, but I came across the fact that I already have such cases when files were uploaded to the system with dots in the name, and this implementation option no longer suits me.
I understand that cases with such a "two-level" file extension are extremely rare, but I cannot allow such problems in the future that suffixes will break file extensions like example.tar-1.gz
P.S. I also thought of defining everything after the first dot in the file name as its extension, but a file may also have dots in the name.
I tried to trim the extension after the last point, but there are "two-level" extensions:
TARGET: example.zip
OK: example-1.zip
TARGET: example.tar.gz
BAD: example.tar-1.gz
I also thought of defining everything after the first dot in the file name as its extension, but a file may also have dots in the name:
TARGET: example.tar.gz
OK: example-1.tar.gz
TARGET: product-v.1.0.0.zip
BAD: product-v-1.1.0.0.zip
foo.tar.gzto be recognized as a "two-level" extension, butfoo.bar.gzas just one level. Whether havingtarandgzas individual items on your list would be enough, or whether you would have to explicitly havetar.gzin your list, would depend on whether you wantfoo.tar.gzandfoo.gz.tarto be treated the same, or differently.