automatic detection of file types
authorChristian Weiske <cweiske@cweiske.de>
Tue, 5 Jun 2012 14:11:52 +0000 (16:11 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 5 Jun 2012 14:11:52 +0000 (16:11 +0200)
data/templates/edit-file.htm
src/phorkie/HtmlHelper.php
src/phorkie/Repository/Post.php

index a1993c5b2e79c7d4e1a543121c3c7681e4aa9f8c..aca61f926d09a7575502dd30a6c45af3f2993a88 100644 (file)
@@ -1,4 +1,11 @@
  <div class="well filegroup" id="filegroup{{fileid}}">
+  {% if not file or file.isText %}
+  <textarea name="files[{{fileid}}][content]" id="content_{{fileid}}" cols="80" rows="15" class="content">{{file.getContent}}</textarea>
+  {% else %}
+  <p style="text-align: center">
+   Binary files cannot be edited.
+  </p>
+  {% endif %}
   <div class="row-fluid">
    <div class="span6">
     <input type="hidden" name="files[{{fileid}}][original_name]" value="{{file.getFilename}}"/>
     <p class="muted">Type determined from filename</p>
    </div>
   </div>
-  {% if not file or file.isText %}
-  <textarea name="files[{{fileid}}][content]" id="content_{{fileid}}" cols="80" rows="15" class="content">{{file.getContent}}</textarea>
-  {% else %}
-  <p style="text-align: center">
-   Binary files cannot be edited.
-  </p>
-  {% endif %}
   <div class="row-fluid">
    <div class="span9">
     <label for="upload_{{fileid}}" class="inline">Replace with upload:</label>
index ebda58bcc2e3c497d7c69dc60e014e6a5a9c0239..af046cf2c8caa03fc7495f93136298404d8a0a35 100644 (file)
@@ -5,7 +5,7 @@ class HtmlHelper
 {
     public function getLanguageOptions(File $file = null)
     {
-        $html = '';
+        $html = '<option value="_auto_">* automatic *</option>';
         $fileExt = null;
         if ($file !== null) {
             $fileExt = $file->getExt();
index 438bf5a3647bb2d06dc2a547a55c0f07f23a4c27..d104a8c1f3eb17254dac43c325bb8f7beef44669 100644 (file)
@@ -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;
+    }
 }
 
 ?>