Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F726884
D373.id918.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Jan 6, 2:24 AM
Size
3 KB
Mime Type
text/x-diff
Expires
Tue, Jan 7, 2:24 AM (2 d, 20 h ago)
Engine
blob
Format
Raw Data
Handle
536973
Attached To
D373: INP-1775 - Rename uploaded images to match their image type (when "storage_format" is used)
D373.id918.diff
View Options
Index: core/kernel/utility/formatters/upload_formatter.php
===================================================================
--- core/kernel/utility/formatters/upload_formatter.php
+++ core/kernel/utility/formatters/upload_formatter.php
@@ -242,16 +242,27 @@
$object->SetError($field_name, 'cant_save_file', 'la_error_cant_save_file');
}
else {
- $real_name = $this->_getRealFilename($value['name'], $options, $object);
- $file_name = $this->FullPath . $real_name;
+ $tmp_path = WRITEABLE . '/tmp/';
+ $filename = $this->fileHelper->ensureUniqueFilename($tmp_path, $value['name'] . '.tmp');
+ $tmp_file_path = $tmp_path . $filename;
- $moved = move_uploaded_file($value['tmp_name'], $file_name);
+ $moved = move_uploaded_file($value['tmp_name'], $tmp_file_path);
$storage_format = isset($options['storage_format']) ? $options['storage_format'] : false;
if ( $storage_format ) {
/** @var kUploadHelper $upload_helper */
$upload_helper = $this->Application->recallObject('kUploadHelper');
- $moved = $upload_helper->resizeUploadedFile($file_name, $storage_format);
+ $moved = $upload_helper->resizeUploadedFile($tmp_file_path, $storage_format);
+ }
+
+ if ( $moved ) {
+ $real_name = $this->_getRealFilename(
+ kUtil::removeTempExtension(basename($tmp_file_path)),
+ $options,
+ $object
+ );
+ $file_name = $this->FullPath . $real_name;
+ $moved = rename($tmp_file_path, $file_name);
}
if ( !$moved ) {
Index: core/units/helpers/upload_helper.php
===================================================================
--- core/units/helpers/upload_helper.php
+++ core/units/helpers/upload_helper.php
@@ -105,7 +105,7 @@
$this->deleteTempFiles($thumbs_path);
}
- return preg_replace('/^' . preg_quote($id, '/') . '_/', '', $filename);
+ return preg_replace('/^' . preg_quote($id, '/') . '_/', '', basename($file_path));
}
/**
@@ -116,7 +116,7 @@
*
* @return boolean
*/
- public function resizeUploadedFile($file_path, $format)
+ public function resizeUploadedFile(&$file_path, $format)
{
/** @var ImageHelper $image_helper */
$image_helper = $this->Application->recallObject('ImageHelper');
@@ -132,10 +132,48 @@
$image_helper->ResizeImage($resize_file_path, $format)
);
+ $file_path = $this->replaceFileExtension(
+ $file_path,
+ pathinfo($resized_file_path, PATHINFO_EXTENSION)
+ );
+
return rename($resized_file_path, $file_path);
}
/**
+ * Replace extension of uploaded file.
+ *
+ * @param string $file_path File path.
+ * @param string $new_file_extension New file extension.
+ *
+ * @return string
+ */
+ protected function replaceFileExtension($file_path, $new_file_extension)
+ {
+ $file_path_without_temp_file_extension = kUtil::removeTempExtension($file_path);
+ $current_file_extension = pathinfo($file_path_without_temp_file_extension, PATHINFO_EXTENSION);
+
+ // Format of resized file wasn't changed.
+ if ( $current_file_extension === $new_file_extension ) {
+ return $file_path;
+ }
+
+ $ret = preg_replace(
+ '/\.' . preg_quote($current_file_extension, '/') . '$/',
+ '.' . $new_file_extension,
+ $file_path_without_temp_file_extension
+ );
+
+ // Add ".tmp" later, since it was removed.
+ if ( $file_path_without_temp_file_extension !== $file_path ) {
+ $ret .= '.tmp';
+ }
+
+ // After file extension change resulting filename might not be unique in that folder anymore.
+ return $this->fileHelper->ensureUniqueFilename(pathinfo($ret, PATHINFO_DIRNAME), $ret);
+ }
+
+ /**
* Sends headers to ensure, that response is never cached.
*
* @return void
Event Timeline
Log In to Comment