Attached Files |
unique_filename_709.patch [^] (7,541 bytes) 2011-10-17 12:40
[Show Content]
Index: kernel/db/db_event_handler.php
===================================================================
--- kernel/db/db_event_handler.php (revision 14670)
+++ kernel/db/db_event_handler.php (working copy)
@@ -2839,10 +2839,10 @@
return ;
}
- $upload_formatter =& $this->Application->recallObject('kUploadFormatter');
- /* @var $upload_formatter kUploadFormatter */
+ $file_helper =& $this->Application->recallObject('FileHelper');
+ /* @var $file_helper FileHelper */
- $fname = $upload_formatter->_ensureUniqueFilename($tmp_path, $fname);
+ $fname = $file_helper->ensureUniqueFilename($tmp_path, $fname);
if ($storage_format) {
$image_helper =& $this->Application->recallObject('ImageHelper');
Index: kernel/utility/formatters/upload_formatter.php
===================================================================
--- kernel/utility/formatters/upload_formatter.php (revision 14670)
+++ kernel/utility/formatters/upload_formatter.php (working copy)
@@ -98,9 +98,12 @@
}
}
+ $file_helper =& $this->Application->recallObject('FileHelper');
+ /* @var $file_helper FileHelper */
+
for ($i=0; $i<min($max_files, count($swf_uploaded_ids)); $i++) {
$real_name = $swf_uploaded_names[$i];
- $real_name = $this->_ensureUniqueFilename($this->FullPath, $real_name, $files2delete);
+ $real_name = $file_helper->ensureUniqueFilename($this->FullPath, $real_name, $files2delete);
$file_name = $this->FullPath.$real_name;
$tmp_file = WRITEABLE . '/tmp/' . $swf_uploaded_ids[$i].'_'.$swf_uploaded_names[$i];
@@ -152,7 +155,11 @@
$object->SetError($field_name, 'cant_save_file', 'la_error_cant_save_file');
}
else {
- $real_name = $this->_ensureUniqueFilename($this->FullPath, $value['name']);
+
+ $file_helper =& $this->Application->recallObject('FileHelper');
+ /* @var $file_helper FileHelper */
+
+ $real_name = $file_helper->ensureUniqueFilename($this->FullPath, $value['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');
@@ -345,33 +352,6 @@
return sprintf($format, $value);
}
-
- /**
- * Ensures, that uploaded file will not overwrite any of previously uploaded files with same name
- *
- * @param string $path
- * @param string $name
- * @param Array $forbidden_names
- * @return string
- */
- function _ensureUniqueFilename($path, $name, $forbidden_names = Array())
- {
- $parts = pathinfo($name);
- $ext = '.' . $parts['extension'];
- $filename = mb_substr($parts['basename'], 0, -mb_strlen($ext));
- $new_name = $filename . $ext;
-
- while (file_exists($path . '/' . $new_name) || in_array(rtrim($path, '/') . '/' . $new_name, $forbidden_names)) {
- if (preg_match('/(' . preg_quote($filename, '/') . '_)([0-9]*)(' . preg_quote($ext, '/') . ')/', $new_name, $regs)) {
- $new_name = $regs[1] . ($regs[2] + 1) . $regs[3];
- }
- else {
- $new_name = $filename . '_1' . $ext;
- }
- }
-
- return $new_name;
- }
}
class kPictureFormatter extends kUploadFormatter
Index: units/helpers/file_helper.php
===================================================================
--- units/helpers/file_helper.php (revision 14670)
+++ units/helpers/file_helper.php (working copy)
@@ -21,7 +21,7 @@
*
* @param kCatDBItem $object
*/
- function LoadItemFiles(&$object)
+ public function LoadItemFiles(&$object)
{
$max_file_count = $this->Application->ConfigValue($object->Prefix.'_MaxImageCount'); // file count equals to image count (temporary measure)
@@ -47,7 +47,7 @@
*
* @param kCatDBItem $object
*/
- function SaveItemFiles(&$object)
+ public function SaveItemFiles(&$object)
{
$table_name = $this->Application->getUnitOption('#file', 'TableName');
$max_file_count = $this->Application->getUnitOption($object->Prefix, 'FileCount'); // $this->Application->ConfigValue($object->Prefix.'_MaxImageCount');
@@ -108,7 +108,7 @@
*
* @param Array $field_values
*/
- function PreserveItemFiles(&$field_values)
+ public function PreserveItemFiles(&$field_values)
{
foreach ($field_values as $field_name => $field_value) {
if (!is_array($field_value)) continue;
@@ -126,7 +126,7 @@
* @param string $prefix
* @param bool $is_image
*/
- function createItemFiles($prefix, $is_image = false)
+ public function createItemFiles($prefix, $is_image = false)
{
$items_info = $this->Application->GetVar($prefix);
if ($items_info) {
@@ -145,7 +145,7 @@
* @param Array $fields_values
* @param bool $is_image
*/
- function createUploadFields($prefix, $fields_values, $is_image = false)
+ public function createUploadFields($prefix, $fields_values, $is_image = false)
{
$field_options = Array (
'type' => 'string',
@@ -239,7 +239,7 @@
*
* @param string $filename
*/
- function DownloadFile($filename)
+ public function DownloadFile($filename)
{
header('Content-type: ' . kUtil::mimeContentType($filename));
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
@@ -254,7 +254,7 @@
* @param string $path
* @return bool
*/
- function CheckFolder($path)
+ public function CheckFolder($path)
{
$result = true;
@@ -293,7 +293,7 @@
* @param string $destination
* @return bool
*/
- function copyFolderRecursive($source, $destination)
+ public function copyFolderRecursive($source, $destination)
{
if ( substr($source, -1) == DIRECTORY_SEPARATOR ) {
$source = substr($source, 0, -1);
@@ -338,7 +338,7 @@
* @param string $destination
* @return bool
*/
- function copyFolder($source, $destination)
+ public function copyFolder($source, $destination)
{
if ( substr($source, -1) == DIRECTORY_SEPARATOR ) {
$source = substr($source, 0, -1);
@@ -377,7 +377,7 @@
* @param string $url
* @return string
*/
- function pathToUrl($url)
+ public function pathToUrl($url)
{
$url = str_replace(DIRECTORY_SEPARATOR, '/', preg_replace('/^' . preg_quote(FULL_PATH, '/') . '(.*)/', '\\1', $url, 1));
$url = implode('/', array_map('rawurlencode', explode('/', $url)));
@@ -391,7 +391,7 @@
* @param string $url
* @return string
*/
- function urlToPath($url)
+ public function urlToPath($url)
{
$base_url = rtrim($this->Application->BaseURL(), '/');
@@ -401,4 +401,31 @@
return str_replace('/', DIRECTORY_SEPARATOR, rawurldecode($path));
}
+
+ /**
+ * Ensures, that new file will not overwrite any of previously created files with same name
+ *
+ * @param string $path
+ * @param string $name
+ * @param Array $forbidden_names
+ * @return string
+ */
+ public function ensureUniqueFilename($path, $name, $forbidden_names = Array())
+ {
+ $parts = pathinfo($name);
+ $ext = '.' . $parts['extension'];
+ $filename = mb_substr($parts['basename'], 0, -mb_strlen($ext));
+ $new_name = $filename . $ext;
+
+ while (file_exists($path . '/' . $new_name) || in_array(rtrim($path, '/') . '/' . $new_name, $forbidden_names)) {
+ if (preg_match('/(' . preg_quote($filename, '/') . '_)([0-9]*)(' . preg_quote($ext, '/') . ')/', $new_name, $regs)) {
+ $new_name = $regs[1] . ($regs[2] + 1) . $regs[3];
+ }
+ else {
+ $new_name = $filename . '_1' . $ext;
+ }
+ }
+
+ return $new_name;
+ }
}
\ No newline at end of file
unique_filename_refactoring_v2.patch [^] (8,633 bytes) 2011-10-19 04:28
[Show Content]
Index: kernel/db/db_event_handler.php
===================================================================
--- kernel/db/db_event_handler.php (revision 14657)
+++ kernel/db/db_event_handler.php (working copy)
@@ -92,7 +92,7 @@
}
/**
- * Allows to override standart permission mapping
+ * Allows to override standard permission mapping
*
*/
function mapPermissions()
@@ -2839,10 +2839,10 @@
return ;
}
- $upload_formatter =& $this->Application->recallObject('kUploadFormatter');
- /* @var $upload_formatter kUploadFormatter */
+ $file_helper =& $this->Application->recallObject('FileHelper');
+ /* @var $file_helper FileHelper */
- $fname = $upload_formatter->_ensureUniqueFilename($tmp_path, $fname);
+ $fname = $file_helper->ensureUniqueFilename($tmp_path, $fname);
if ($storage_format) {
$image_helper =& $this->Application->recallObject('ImageHelper');
Index: kernel/utility/formatters/upload_formatter.php
===================================================================
--- kernel/utility/formatters/upload_formatter.php (revision 14653)
+++ kernel/utility/formatters/upload_formatter.php (working copy)
@@ -100,7 +100,7 @@
for ($i=0; $i<min($max_files, count($swf_uploaded_ids)); $i++) {
$real_name = $swf_uploaded_names[$i];
- $real_name = $this->_ensureUniqueFilename($this->FullPath, $real_name, $files2delete);
+ $real_name = $this->fileHelper->ensureUniqueFilename($this->FullPath, $real_name, $files2delete);
$file_name = $this->FullPath.$real_name;
$tmp_file = WRITEABLE . '/tmp/' . $swf_uploaded_ids[$i].'_'.$swf_uploaded_names[$i];
@@ -152,7 +152,8 @@
$object->SetError($field_name, 'cant_save_file', 'la_error_cant_save_file');
}
else {
- $real_name = $this->_ensureUniqueFilename($this->FullPath, $value['name']);
+ $real_name = $this->fileHelper->ensureUniqueFilename($this->FullPath, $value['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');
@@ -345,33 +346,6 @@
return sprintf($format, $value);
}
-
- /**
- * Ensures, that uploaded file will not overwrite any of previously uploaded files with same name
- *
- * @param string $path
- * @param string $name
- * @param Array $forbidden_names
- * @return string
- */
- function _ensureUniqueFilename($path, $name, $forbidden_names = Array())
- {
- $parts = pathinfo($name);
- $ext = '.' . $parts['extension'];
- $filename = mb_substr($parts['basename'], 0, -mb_strlen($ext));
- $new_name = $filename . $ext;
-
- while (file_exists($path . '/' . $new_name) || in_array(rtrim($path, '/') . '/' . $new_name, $forbidden_names)) {
- if (preg_match('/(' . preg_quote($filename, '/') . '_)([0-9]*)(' . preg_quote($ext, '/') . ')/', $new_name, $regs)) {
- $new_name = $regs[1] . ($regs[2] + 1) . $regs[3];
- }
- else {
- $new_name = $filename . '_1' . $ext;
- }
- }
-
- return $new_name;
- }
}
class kPictureFormatter extends kUploadFormatter
Index: units/helpers/file_helper.php
===================================================================
--- units/helpers/file_helper.php (revision 14653)
+++ units/helpers/file_helper.php (working copy)
@@ -17,11 +17,13 @@
class FileHelper extends kHelper {
/**
- * Puts existing item images (from subitem) to virtual fields (in main item)
+ * Puts existing item images (from sub-item) to virtual fields (in main item)
*
* @param kCatDBItem $object
+ * @return void
+ * @access public
*/
- function LoadItemFiles(&$object)
+ public function LoadItemFiles(&$object)
{
$max_file_count = $this->Application->ConfigValue($object->Prefix.'_MaxImageCount'); // file count equals to image count (temporary measure)
@@ -46,8 +48,10 @@
* Saves newly uploaded images to external image table
*
* @param kCatDBItem $object
+ * @return void
+ * @access public
*/
- function SaveItemFiles(&$object)
+ public function SaveItemFiles(&$object)
{
$table_name = $this->Application->getUnitOption('#file', 'TableName');
$max_file_count = $this->Application->getUnitOption($object->Prefix, 'FileCount'); // $this->Application->ConfigValue($object->Prefix.'_MaxImageCount');
@@ -104,11 +108,13 @@
}
/**
- * Preserves cloned item images/files to be rewrited with original item images/files
+ * Preserves cloned item images/files to be rewritten with original item images/files
*
* @param Array $field_values
+ * @return void
+ * @access public
*/
- function PreserveItemFiles(&$field_values)
+ public function PreserveItemFiles(&$field_values)
{
foreach ($field_values as $field_name => $field_value) {
if (!is_array($field_value)) continue;
@@ -125,12 +131,14 @@
*
* @param string $prefix
* @param bool $is_image
+ * @return void
+ * @access public
*/
- function createItemFiles($prefix, $is_image = false)
+ public function createItemFiles($prefix, $is_image = false)
{
$items_info = $this->Application->GetVar($prefix);
if ($items_info) {
- list ($id, $fields_values) = each($items_info);
+ list (, $fields_values) = each($items_info);
$this->createUploadFields($prefix, $fields_values, $is_image);
}
else {
@@ -144,8 +152,10 @@
* @param string $prefix
* @param Array $fields_values
* @param bool $is_image
+ * @return void
+ * @access public
*/
- function createUploadFields($prefix, $fields_values, $is_image = false)
+ public function createUploadFields($prefix, $fields_values, $is_image = false)
{
$field_options = Array (
'type' => 'string',
@@ -238,8 +248,10 @@
* Downloads file to user
*
* @param string $filename
+ * @return void
+ * @access public
*/
- function DownloadFile($filename)
+ public function DownloadFile($filename)
{
header('Content-type: ' . kUtil::mimeContentType($filename));
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
@@ -253,8 +265,9 @@
*
* @param string $path
* @return bool
+ * @access public
*/
- function CheckFolder($path)
+ public function CheckFolder($path)
{
$result = true;
@@ -292,8 +305,9 @@
* @param string $source
* @param string $destination
* @return bool
+ * @access public
*/
- function copyFolderRecursive($source, $destination)
+ public function copyFolderRecursive($source, $destination)
{
if ( substr($source, -1) == DIRECTORY_SEPARATOR ) {
$source = substr($source, 0, -1);
@@ -337,8 +351,9 @@
* @param string $source
* @param string $destination
* @return bool
+ * @access public
*/
- function copyFolder($source, $destination)
+ public function copyFolder($source, $destination)
{
if ( substr($source, -1) == DIRECTORY_SEPARATOR ) {
$source = substr($source, 0, -1);
@@ -376,8 +391,9 @@
*
* @param string $url
* @return string
+ * @access public
*/
- function pathToUrl($url)
+ public function pathToUrl($url)
{
$url = str_replace(DIRECTORY_SEPARATOR, '/', preg_replace('/^' . preg_quote(FULL_PATH, '/') . '(.*)/', '\\1', $url, 1));
$url = implode('/', array_map('rawurlencode', explode('/', $url)));
@@ -390,8 +406,9 @@
*
* @param string $url
* @return string
+ * @access public
*/
- function urlToPath($url)
+ public function urlToPath($url)
{
$base_url = rtrim($this->Application->BaseURL(), '/');
@@ -401,4 +418,31 @@
return str_replace('/', DIRECTORY_SEPARATOR, rawurldecode($path));
}
+
+ /**
+ * Ensures, that new file will not overwrite any of previously created files with same name
+ *
+ * @param string $path
+ * @param string $name
+ * @param Array $forbidden_names
+ * @return string
+ */
+ public function ensureUniqueFilename($path, $name, $forbidden_names = Array())
+ {
+ $parts = pathinfo($name);
+ $ext = '.' . $parts['extension'];
+ $filename = mb_substr($parts['basename'], 0, -mb_strlen($ext));
+ $new_name = $filename . $ext;
+
+ while ( file_exists($path . '/' . $new_name) || in_array(rtrim($path, '/') . '/' . $new_name, $forbidden_names) ) {
+ if ( preg_match('/(' . preg_quote($filename, '/') . '_)([0-9]*)(' . preg_quote($ext, '/') . ')/', $new_name, $regs) ) {
+ $new_name = $regs[1] . ($regs[2] + 1) . $regs[3];
+ }
+ else {
+ $new_name = $filename . '_1' . $ext;
+ }
+ }
+
+ return $new_name;
+ }
}
\ No newline at end of file
|