1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-05 14:17:49 +02:00

Related to issue #619. Attempt to detect and add file extension when it's missing and prior to media import.

This commit is contained in:
Cameron
2014-06-11 20:43:12 -07:00
parent d2b2af639c
commit d6c8f92efa
3 changed files with 84 additions and 11 deletions

View File

@@ -264,8 +264,13 @@ class e_file
{
$finfo = $this->get_file_info($path."/".$file, ('file' != $this->finfo)); // -> 'all' & 'image'
}
$finfo['path'] = $path."/"; // important: leave this slash here and update other file instead.
$finfo['fname'] = $file;
else
{
$finfo['path'] = $path.DIRECTORY_SEPARATOR; // important: leave this slash here and update other file instead.
$finfo['fname'] = $file;
}
// $finfo['path'] = $path.DIRECTORY_SEPARATOR; // important: leave this slash here and update other file instead.
// $finfo['fname'] = $file;
$ret[] = $finfo;
break;
@@ -277,6 +282,24 @@ class e_file
return $ret;
}
function getFileExtension($mimeType)
{
$extensions = array(
'image/jpeg'=>'.jpg',
'image/png' => '.png',
'image/gif' => '.gif'
);
if(isset($extensions[$mimeType]))
{
return $extensions[$mimeType];
}
}
/**
* Collect file information
* @param string $path_to_file
@@ -291,16 +314,56 @@ class e_file
{
return false;
}
$finfo['pathinfo'] = pathinfo($path_to_file);
if(class_exists('finfo')) // Best Mime detection method.
{
$fin = new finfo(FILEINFO_MIME);
list($mime, $other) = explode(";", $fin->file($path_to_file));
if(!empty($mime))
{
$finfo['mime'] = $mime;
}
}
// Auto-Fix Files without an extensions using known mime-type.
if(empty($finfo['pathinfo']['extension']) && !is_dir($path_to_file) && !empty($finfo['mime']))
{
if($ext = $this->getFileExtension($finfo['mime']))
{
$finfo['pathinfo']['extension'] = $ext;
$newFile = $path_to_file . $ext;
if(!file_exists($newFile))
{
if(rename($path_to_file,$newFile)===true)
{
$finfo['pathinfo'] = pathinfo($newFile);
$path_to_file = $newFile;
}
}
}
}
if($imgcheck && ($tmp = getimagesize($path_to_file)))
{
$finfo['img-width'] = $tmp[0];
$finfo['img-height'] = $tmp[1];
$finfo['mime'] = $tmp['mime'];
if(empty($finfo['mime']))
{
$finfo['mime'] = $tmp['mime'];
}
}
$tmp = stat($path_to_file);
$tmp = stat($path_to_file);
if($tmp)
{
@@ -309,9 +372,17 @@ class e_file
}
// associative array elements: dirname, basename, extension, filename
$finfo['pathinfo'] = pathinfo($path_to_file);
$finfo['mime'] = vartrue($finfo['mime'],'application/'.$finfo['pathinfo']['extension']);
$finfo['fullpath'] = $path_to_file;
$finfo['fname'] = basename($path_to_file);
$finfo['path'] = dirname($path_to_file).DIRECTORY_SEPARATOR;
// $finfo['mime'] = vartrue($finfo['mime'],'application/'.$finfo['pathinfo']['extension']);
return $finfo;
}

View File

@@ -2809,11 +2809,11 @@ class e_form
return $video;
}
if(!preg_match("/[a-zA-z0-9_-\s\(\)]+\.(png|jpg|jpeg|gif|PNG|JPG|JPEG|GIF)$/",$value))
if(!preg_match("/[a-zA-z0-9_-\s\(\)]+\.(png|jpg|jpeg|gif|PNG|JPG|JPEG|GIF)$/",$value) && strpos($value,'.')!==false)
{
$icon = "{e_IMAGE}filemanager/zip_32.png";
$src = $tp->replaceConstants(vartrue($parms['pre']).$icon, 'abs');
return '<img src="'.$src.'" alt="'.$value.'" class="e-thumb" title="'.$value.'" />';
// return '<img src="'.$src.'" alt="'.$value.'" class="e-thumb" title="'.$value.'" />';
}
if(vartrue($parms['thumb']))

View File

@@ -959,13 +959,15 @@ class e_media
$info = e107::getFile()->get_file_info($path,true);
$this->log("File info for $path : ".print_r($info,true));
return array(
'media_type' => vartrue($info['mime']),
'media_datestamp' => time(),
'media_url' => e107::getParser()->createConstants($path, 'rel'),
'media_size' => filesize($path),
'media_url' => e107::getParser()->createConstants($info['fullpath'], 'rel'),
'media_size' => filesize($info['fullpath']),
'media_author' => USERID,
'media_usedby' => '',
'media_tags' => '',