Index: branches/5.2.x/core/kernel/utility/formatters/upload_formatter.php =================================================================== diff -u -N -r14748 -r14868 --- branches/5.2.x/core/kernel/utility/formatters/upload_formatter.php (.../upload_formatter.php) (revision 14748) +++ branches/5.2.x/core/kernel/utility/formatters/upload_formatter.php (.../upload_formatter.php) (revision 14868) @@ -1,6 +1,6 @@ FullPath = FULL_PATH.$this->DestinationPath; } - $this->fileHelper->CheckFolder($this->FullPath); - // SWF Uploader if (is_array($value) && isset($value['tmp_ids'])) { if ($value['tmp_deleted']) { @@ -100,19 +98,21 @@ } } - for ($i=0; $igetStorageEngineFile($swf_uploaded_names[$i], $options, $object->Prefix); + $real_name = $this->getStorageEngineFolder($real_name, $options) . $real_name; + $real_name = $this->fileHelper->ensureUniqueFilename($this->FullPath, $real_name, $files2delete); - $file_name = $this->FullPath.$real_name; + $file_name = $this->FullPath . $real_name; - $tmp_file = WRITEABLE . '/tmp/' . $swf_uploaded_ids[$i].'_'.$swf_uploaded_names[$i]; + $tmp_file = WRITEABLE . '/tmp/' . $swf_uploaded_ids[$i] . '_' . $swf_uploaded_names[$i]; rename($tmp_file, $file_name); @chmod($file_name, 0666); - $fret[] = getArrayValue($options, 'upload_dir') ? $real_name : $this->DestinationPath.$real_name; + $fret[] = getArrayValue($options, 'upload_dir') ? $real_name : $this->DestinationPath . $real_name; } - $fret = array_merge($existing, $fret); - return implode('|', $fret); + + return implode('|', array_merge($existing, $fret)); } // SWF Uploader END @@ -154,9 +154,12 @@ $object->SetError($field_name, 'cant_save_file', 'la_error_cant_save_file'); } else { - $real_name = $this->fileHelper->ensureUniqueFilename($this->FullPath, $value['name']); + $real_name = $this->getStorageEngineFile($value['name'], $options, $object->Prefix); + $real_name = $this->getStorageEngineFolder($real_name, $options) . $real_name; + $real_name = $this->fileHelper->ensureUniqueFilename($this->FullPath, $real_name); $file_name = $this->FullPath . $real_name; + if ( !move_uploaded_file($value['tmp_name'], $file_name) ) { $object->SetError($field_name, 'cant_save_file', 'la_error_cant_save_file'); } @@ -348,6 +351,81 @@ return sprintf($format, $value); } + + /** + * Creates & returns folder, based on storage engine specified in field options + * + * @param string $file_name + * @param array $options + * @return string + * @access protected + */ + protected function getStorageEngineFolder($file_name, $options) + { + $storage_engine = (string)getArrayValue($options, 'storage_engine'); + + if ( !$storage_engine ) { + return ''; + } + + switch ($storage_engine) { + case StorageEngine::HASH: + $folder_path = kUtil::getHashPathForLevel($file_name); + break; + + case StorageEngine::TIMESTAMP: + $folder_path = adodb_date('Y-m/d/'); + break; + + default: + throw new Exception('Unknown storage engine "' . $storage_engine . '".'); + break; + } + + return $folder_path; + } + + /** + * Applies prefix & suffix to uploaded filename, based on storage engine in field options + * + * @param string $name + * @param array $options + * @param string $unit_prefix + * @return string + * @access protected + */ + protected function getStorageEngineFile($name, $options, $unit_prefix) + { + $prefix = $this->getStorageEngineFilePart(getArrayValue($options, 'filename_prefix'), $unit_prefix); + $suffix = $this->getStorageEngineFilePart(getArrayValue($options, 'filename_suffix'), $unit_prefix); + + $parts = pathinfo($name); + + return ($prefix ? $prefix . '_' : '') . $parts['filename'] . ($suffix ? '_' . $suffix : '') . '.' . $parts['extension']; + } + + /** + * Creates prefix/suffix to join with uploaded file + * + * Added "u" before user_id to keep this value after FileHelper::ensureUniqueFilename method call + * + * @param string $option + * @param string $unit_prefix + * @return string + * @access protected + */ + protected function getStorageEngineFilePart($option, $unit_prefix) + { + $replace_from = Array ( + StorageEngine::PS_DATE_TIME, StorageEngine::PS_PREFIX, StorageEngine::PS_USER + ); + + $replace_to = Array ( + adodb_date('Ymd-His'), $unit_prefix, 'u' . $this->Application->RecallVar('user_id') + ); + + return str_replace($replace_from, $replace_to, $option); + } } class kPictureFormatter extends kUploadFormatter