do not show text field for binary files
authorChristian Weiske <cweiske@cweiske.de>
Sun, 15 Apr 2012 20:12:44 +0000 (22:12 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Sun, 15 Apr 2012 20:12:44 +0000 (22:12 +0200)
data/templates/edit-file.htm
src/phorkie/File.php
src/phorkie/Repository/Post.php

index 53c2c014f4b6dc9e427a9b07faae5adae7d7bb59..a1993c5b2e79c7d4e1a543121c3c7681e4aa9f8c 100644 (file)
     <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 519413d565411db7d45356fdf4979d11f364d5ab..53925eeee131618b8cbb4545a2c54f24d53a9c5f 100644 (file)
@@ -115,6 +115,22 @@ class File
         $tm = new Tool_Manager();
         return $tm->getSuitable($this);
     }
+
+    /**
+     * Tells if the file contains textual content and is editable.
+     *
+     * @return boolean
+     */
+    public function isText()
+    {
+        $ext = $this->getExt();
+        if (!isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
+            return false;
+        }
+
+        $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
+        return substr($type, 0, 5) === 'text/';
+    }
 }
 
 ?>
\ No newline at end of file
index 471cbd80a707c0a5e620409b3292c0081d65ac85..045bba10c3399b7024ce254a7f69ebb0fc70c26b 100644 (file)
@@ -94,7 +94,7 @@ class Repository_Post
                     ->addArgument($file->getFilename())
                     ->execute();
                 $bChanged = true;
-            } else if ($bNew || $file->getContent() != $arFile['content']) {
+            } else if ($bNew || (isset($arFile['content']) && $file->getContent() != $arFile['content'])) {
                 file_put_contents($file->getPath(), $arFile['content']);
                 $command = $vc->getCommand('add')
                     ->addArgument($file->getFilename())