From 3543bfd4d6c2ff90417d775b4eac74e1f2bd8d10 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Tue, 5 Jun 2012 16:11:52 +0200 Subject: [PATCH] automatic detection of file types --- data/templates/edit-file.htm | 14 +++++++------- src/phorkie/HtmlHelper.php | 2 +- src/phorkie/Repository/Post.php | 27 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/data/templates/edit-file.htm b/data/templates/edit-file.htm index a1993c5..aca61f9 100644 --- a/data/templates/edit-file.htm +++ b/data/templates/edit-file.htm @@ -1,4 +1,11 @@
+ {% if not file or file.isText %} + + {% else %} +

+ Binary files cannot be edited. +

+ {% endif %}
@@ -14,13 +21,6 @@

Type determined from filename

- {% if not file or file.isText %} - - {% else %} -

- Binary files cannot be edited. -

- {% endif %}
diff --git a/src/phorkie/HtmlHelper.php b/src/phorkie/HtmlHelper.php index ebda58b..af046cf 100644 --- a/src/phorkie/HtmlHelper.php +++ b/src/phorkie/HtmlHelper.php @@ -5,7 +5,7 @@ class HtmlHelper { public function getLanguageOptions(File $file = null) { - $html = ''; + $html = ''; $fileExt = null; if ($file !== null) { $fileExt = $file->getExt(); diff --git a/src/phorkie/Repository/Post.php b/src/phorkie/Repository/Post.php index 438bf5a..d104a8c 100644 --- a/src/phorkie/Repository/Post.php +++ b/src/phorkie/Repository/Post.php @@ -48,6 +48,11 @@ class Repository_Post $orignalName = Tools::sanitizeFilename($arFile['original_name']); $name = Tools::sanitizeFilename($arFile['name']); + if ($arFile['type'] == '_auto_') { + //FIXME: upload + $arFile['type'] = $this->getType($arFile['content']); + } + if ($name == '') { if ($bUpload) { $name = Tools::sanitizeFilename($_FILES['files']['name'][$num]['upload']); @@ -163,6 +168,28 @@ class Repository_Post return $prefix . $num; } + + protected function getType($content) + { + $tmp = tempnam(sys_get_temp_dir(), 'phorkie-autodetect-'); + file_put_contents($tmp, $content); + $type = \MIME_Type_PlainDetect::autoDetect($tmp); + unlink($tmp); + + return $this->findExtForType($type); + } + + protected function findExtForType($type) + { + $ext = 'text/plain'; + foreach ($GLOBALS['phorkie']['languages'] as $lext => $arLang) { + if ($arLang['mime'] == $type) { + $ext = $lext; + break; + } + } + return $ext; + } } ?> -- 2.30.2