public static function getDefaultAttachmentSRC($blnReturnPath = false) { $objFolder = new \Folder('files/submissions/uploads'); if ($blnReturnPath) { return $objFolder->path; } if (\Validator::isUuid($objFolder->getModel()->uuid)) { return class_exists('Contao\\StringUtil') ? \StringUtil::binToUuid($objFolder->getModel()->uuid) : \String::binToUuid($objFolder->getModel()->uuid); } return null; }
/** * Get a unique filename within given target folder, remove uniqid() suffix from file (optional, add $strPrefix) and append file count by name to * file if file with same name already exists in target folder * * @param string $strTarget The target file path * @param string $strPrefix A uniqid prefix from the given target file, that was added to the file before and should be removed again * @param $i integer Internal counter for recursion usage or if you want to add the number to the file * * @return string | false The filename with the target folder and unique id or false if something went wrong (e.g. target does not exist) */ public static function getUniqueFileNameWithinTarget($strTarget, $strPrefix = null, $i = 0) { $objFile = new \File($strTarget, true); $strTarget = ltrim(str_replace(TL_ROOT, '', $strTarget), '/'); $strPath = str_replace('.' . $objFile->extension, '', $strTarget); if ($strPrefix && ($pos = strpos($strPath, $strPrefix)) !== false) { $strPath = str_replace(substr($strPath, $pos, strlen($strPath)), '', $strPath); $strTarget = $strPath . '.' . $objFile->extension; } // Create the parent folder if (!file_exists($objFile->dirname)) { $objFolder = new \Folder(ltrim(str_replace(TL_ROOT, '', $objFile->dirname), '/')); // something went wrong with folder creation if ($objFolder->getModel() === null) { return false; } } if (file_exists(TL_ROOT . '/' . $strTarget)) { // remove suffix if ($i > 0 && StringUtil::endsWith($strPath, '_' . $i)) { $strPath = rtrim($strPath, '_' . $i); } // increment counter & add extension again $i++; // for performance reasons, add new unique id to path to make recursion come to end after 100 iterations if ($i > 100) { return static::getUniqueFileNameWithinTarget(static::addUniqIdToFilename($strPath . '.' . $objFile->extension, null, false)); } return static::getUniqueFileNameWithinTarget($strPath . '_' . $i . '.' . $objFile->extension, $strPrefix, $i); } return $strTarget; }
/** * Adds a new home dir to a member. Therefore a folder named with the members's id is created in $varRootFolder * * @param $varMember object|int The member as object or member id * @param $strBooleanPropertyName string The name of the boolean member property (e.g. "assignDir") * @param $strPropertyName string The name of the member property (e.g. "homeDir") * @param $varRootFolder string|object The base folder as instance of \FilesModel, path string or uuid * @param bool|false $blnOverwrite bool Determines if an existing folder can be overridden * * @return bool|string Returns true, if a directory has already been linked with the member, the folders uuid if successfully added and false if errors occured. */ public static function addHomeDir($varMember, $strBooleanPropertyName = 'assignDir', $strPropertyName = 'homeDir', $varRootFolder = 'files/members', $blnOverwrite = false) { if (($objMember = is_numeric($varMember) ? \MemberModel::findByPk($varMember) : $varMember) === null) { return false; } // already set if ($objMember->{$strBooleanPropertyName} && $objMember->{$strPropertyName} && !$blnOverwrite) { return true; } if (!$varRootFolder instanceof \FilesModel) { if (\Validator::isUuid($varRootFolder)) { $objFolderModel = \FilesModel::findByUuid($varRootFolder); $strPath = $objFolderModel->path; } else { $strPath = $varRootFolder; } } else { $strPath = $varRootFolder->path; } $strPath = str_replace(TL_ROOT, '', $strPath); if (!$strPath) { return false; } $objMember->{$strBooleanPropertyName} = true; $strPath = ltrim($strPath, '/') . '/' . $objMember->id; $objHomeDir = new \Folder($strPath); $objMember->{$strPropertyName} = $objHomeDir->getModel()->uuid; $objMember->save(); return $objHomeDir->getModel()->uuid; }