OSDN Git Service

Versionファイルのアクセス時出力実装
authorCake <cake_67@users.sourceforge.jp>
Mon, 22 Feb 2010 02:13:31 +0000 (11:13 +0900)
committerCake <cake_67@users.sourceforge.jp>
Mon, 22 Feb 2010 02:13:31 +0000 (11:13 +0900)
app/config/bootstrap.php
app/config/conf/upfile.php [new file with mode: 0644]
app/controllers/app_controller.php
app/libs/core_plus.php
app/plugins/media/libs/upfile.php [new file with mode: 0644]
app/plugins/media/models/attachment_ex.php
app/plugins/media/views/helpers/upfile.php [new file with mode: 0644]
app/views/elements/home.ctp

index 4f1f953..67cf8b9 100644 (file)
@@ -48,5 +48,8 @@ require_once ('conf/config.php');
 /* media plugin */
 require_once(APP.'plugins'.DS.'media'.DS.'config'.DS.'core.php');
 
+/* Upload画像出力サイズなどの設定 */
+require_once ('conf/upfile.php');
+
 /* 汎用関数 */
 require_once (APP. DS. 'libs'. DS. 'core_plus.php');
diff --git a/app/config/conf/upfile.php b/app/config/conf/upfile.php
new file mode 100644 (file)
index 0000000..02eab8a
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+/**
+ * Media Plugin用
+ * Uploadファイルの出力設定
+ * 運用途中で変更した場合は、app/webroot/media/filter以下のディレクトリとファイルを全て削除して下さい。
+ * 
+ * より詳細な設定が必要な場合は、app/plugin/media/config/core.phpを編集して下さい
+ */
+
+/* 
+ * 'xxs'~'xl'
+ * 設定名。任意で設定を増やす事も可能(要表示側ソースの改修)
+ * 
+ * 'convert' => $MimeType
+ *     $MimeType形式に変換して出力
+ * 
+ * 'fitCrop' => array($width, $height)
+ *     幅$width, 高さ$height(px)に縮小/拡大してトリミング(原画の幅か高さの長い方をカット)
+ * 'fit' => array($width, $height)
+ *     幅$width, 高さ$height(px)に縮小(アスペクト比保持、原画の幅か高さの長い方を設定値以内に縮小)
+ * 'fitOutside' => array($width, $height)
+ *     幅$width, 高さ$height(px)に縮小(アスペクト比保持、原画の幅か高さの短い方を設定値以内に縮小)
+ * 'zoomCrop' => array($width, $height)
+ *     画像の中心から幅$width, 高さ$height(px)でトリミング
+ * 
+ * 'compress' => $compress
+ *     圧縮率。0~10で指定。指定ない場合1.5。
+ */
+
+/* 画像 */
+Configure::write('Media.filter.image', array(
+       'xxs' => array('convert' => 'image/png', 'fitCrop' => array(16, 16)),
+       'xs'  => array('convert' => 'image/png', 'fitCrop' => array(32, 32)),
+       's'   => array('convert' => 'image/png', 'fit' => array(64, 64)),
+       'm'   => array('convert' => 'image/png', 'fit' => array(120, 120)),
+       'l'   => array('convert' => 'image/png', 'fit' => array(180, 180)),
+       'bg'  => array('convert' => 'image/png', 'fitOutside' => array(600, 400)),
+));
+
index 71fbf17..04384e4 100644 (file)
@@ -42,6 +42,7 @@ class AppController extends Controller
                'Javascript',
                'Settings',
                'Media.Medium',
+               'Media.Upfile',
                'Token'
        );
 
index 567a2ca..3738a46 100644 (file)
@@ -5,6 +5,10 @@
 
 class CorePlus extends Object {
 
+       var $models = array();
+       var $behavoirs = array();
+
+
        // $value[$key]が存在しかつ空ではない
        function is_valid($value, $key)
        {
@@ -59,8 +63,38 @@ class CorePlus extends Object {
        /* set Outer Model */
        function set_model($modelName)
        {
+               if (isset($this->models[$modelName])) {
+                       return $this->models[$modelName];
+               }
+
+               $name = $modelName;
+               if (strpos($modelName, '.')) {
+                       list($plugin, $name) = explode('.', $modelName, 2);
+               }
+
                App::import('Model', $modelName);
-               return new $modelName;
+               $model = new $name;
+               
+               return $this->models[$modelName] = $model;
+       }
+
+       /* set Outer Behavoir */
+       function set_behavoir($behavoirName)
+       {
+               if (isset($this->behavoirs[$behavoirName])) {
+                       return $this->behavoirs[$behavoirName];
+               }
+
+               $name = $behavoirName;
+               if (strpos($behavoirName, '.')) {
+                       list($plugin, $name) = explode('.', $behavoirName, 2);
+               }
+               $class = $name . 'Behavior';
+
+               App::import('Behavior', $behavoirName);
+               $behavoir = new $class;
+
+               return $this->behavoirs[$behavoirName] = $behavoir;
        }
 
 }
diff --git a/app/plugins/media/libs/upfile.php b/app/plugins/media/libs/upfile.php
new file mode 100644 (file)
index 0000000..61a3c65
--- /dev/null
@@ -0,0 +1,43 @@
+<?php 
+/* 
+ * ファイルアップロード処理
+ */
+
+App::import('Core', 'Shell');
+
+class Upfile extends Object {
+
+       /* Versionファイルの個別作成 */
+       function make_version($path, $orig_filepath, $model_name)
+       {
+               // Confirgureの設定
+               $name = Medium::name($orig_filepath);
+               $filters = Configure::read('Media.filter.'. strtolower($name));
+               if (empty($filters)) {
+                       return false;
+               }
+               $dir = substr_replace($path, '', 0, strlen('filter/'));
+               if (empty($dir)) {
+                       return false;
+               }
+               if (!isset($filters[$dir])) {
+                       return false;
+               }
+               Configure::write('Media.filter.'. strtolower($name), array($dir => $filters[$dir]));
+
+               if (!isset($model_name)) {
+                       return null;
+               }
+               $Model = CorePlus::set_model($model_name);
+               $Media = CorePlus::set_behavoir('Media.Media');
+
+               $Media->setup($Model);
+               $result = $Media->make($Model, $orig_filepath);
+
+               // Configure戻す
+               Configure::write('Media.filter.'. strtolower($name), $filters);
+
+               return $result;
+       }
+
+}
index f231d5f..f904aa5 100644 (file)
@@ -10,7 +10,10 @@ class AttachmentEx extends Attachment {
        function __construct($id = false, $table = null, $ds = null) {
 
                // Uploadファイルの保存設定
+               // ファイル名
                $this->actsAs['Media.Transfer']['destinationFile'] = ':Medium.short::DS::uuid:'.'.'.':Source.extension:';
+               // VersionFile作成
+               $this->actsAs['Media.Media']['makeVersions'] = false;
 
                // validate
                $this->validate['file']['extension'] = array('rule' => array('checkExtension', false, array(
diff --git a/app/plugins/media/views/helpers/upfile.php b/app/plugins/media/views/helpers/upfile.php
new file mode 100644 (file)
index 0000000..1b69c9c
--- /dev/null
@@ -0,0 +1,46 @@
+<?php 
+/**
+ * mediaプラグインアップロードファイル出力用ヘルパー
+ * Mediumヘルパー拡張
+ */
+
+class UpfileHelper extends MediumHelper {
+/**
+ * Resolves partial path
+ * Generates Version File if it dose not exist.
+ * Touches the Version File if it exists.
+ *
+ * Examples:
+ *     css/cake.generic         >>> MEDIA_STATIC/css/cake.generic.css
+ *  transfer/img/image.jpg   >>> MEDIA_TRANSFER/img/image.jpg
+ *     s/img/image.jpg          >>> MEDIA_FILTER/s/static/img/image.jpg
+ *
+ * @param string|array $path Either a string or an array with dirname and basename keys
+ * @return string|boolean False on error or if path couldn't be resolbed otherwise
+ *                                                     an absolute path to the file
+ */
+       function file($path)
+       {
+               $args = func_get_args();
+
+               // ファイルがある場合、ファイルパスを返す
+               $file = parent::file($path, $args[1]);
+               if (is_file($file) && is_readable($file)) {
+
+                       return $file;
+               // $pathがfilter/*指定でファイルがない場合、作成
+               } elseif (substr($path, 0, 7) == 'filter/') {
+                       require_once(APP.'plugins'.DS.'media'.DS.'libs'.DS.'upfile.php');
+                       $make_result = Upfile::make_version($path, $args[1]['dirname']. DS. $args[1]['basename'], $args[2]);
+
+                       // cacheを削除して$file読み込み
+                       if ($make_result === true) {
+                               parent::__destruct();
+                               return self::file($path, $args[1]);
+                       }
+               }
+
+               return null;
+       }
+
+}
index 650cb0a..a239892 100644 (file)
        if (isset($target_user['Attachment'][0])) {
                $img_name = isset($target_user['Attachment'][0]['alternative']) ? $target_user['Attachment'][0]['alternative'] : $target_user['Attachment'][0]['basename']; 
 
-               echo $medium->embed($medium->file('filter/s', $target_user['Attachment'][0]));
+               $file = $upfile->file('filter/m', $target_user['Attachment'][0], 'User');
 
+               if (!empty($file)) {
+                       echo $medium->embed(
+                               $file,
+                               array(
+                                       'restrict' => array('image')
+                               )
+                       );
+               } else {
+                       echo "&nbsp;";
+               }
        } else {
                echo "&nbsp;";
        }