public function resolveFileName($folder, UploadedFile $file, $enableObfuscation = true)
{
if (!isset($file->fileSystemName)) {
$file->fileSystemName = $file->getClientOriginalName();
}
if (static::$app['config']->get('cabinet::obfuscate_filenames') && $enableObfuscation) {
$fileName = basename($file->fileSystemName, $file->getClientOriginalExtension()) . '_' . md5(uniqid(mt_rand(), true)) . '.' . $file->getClientOriginalExtension();
$file->fileSystemName = $fileName;
} else {
$fileName = $file->fileSystemName;
}
// If file exists append string and try again.
if (File::isFile($folder . $fileName)) {
// Default file postfix
$i = '0000';
// Get the file bits
$basename = $this->getBasename($file);
$basenamePieces = explode('_', $basename);
// If there's more than one piece then let see if it's our counter.
if (count($basenamePieces) > 1) {
// Pop the last part of the array off.
$last = array_pop($basenamePieces);
// Check to see if the last piece is an int. Must be 4 long. This isn't the best, but it'll do in most cases.
if (strlen($last) == 4 && (is_int($last) || ctype_digit($last))) {
// Add one, which converts this string to an int. Gotta love PHP ;)
$last += 1;
// Prepare to add the proper amount of 0's in front
$b = 4 - strlen($last);
$i = $last;
for ($c = 1; $c <= $b; $c++) {
$i = '0' . $i;
}
} else {
// Put last back on the array
array_push($basenamePieces, $last);
}
// Put the pieces back together without the postfix.
$basename = implode('_', $basenamePieces);
}
// Create the filename
$file->fileSystemName = $basename . '_' . $i . '.' . $file->getClientOriginalExtension();
list($folder, $file) = $this->resolveFileName($folder, $file, false);
}
return array($folder, $file);
}