tryStripHashFromFilename method

String tryStripHashFromFilename(
  1. String fileName
)

Removes a trailing "<sep>hash" segment from a fileName, preserving any extension. Matches both "name<sep>hash.ext" and "name<sep>hash".

If no hash is found, returns fileName unchanged.

Examples: logo@abc.png -> logo.png logo@abc -> logo logo.png -> logo.png (no change)

Implementation

String tryStripHashFromFilename(final String fileName) {
  final ext = p.url.extension(fileName);
  final base = p.url.basenameWithoutExtension(fileName);

  final at = base.lastIndexOf(separator);
  if (at <= 0) return fileName; // no hash or starts with separator

  final cleanBase = base.substring(0, at);
  return p.url.setExtension(cleanBase, ext);
}