ãmediaãã©ã°ã¤ã³å¿ç¨(4) Versionãã¡ã¤ã«ã表示æã«ä½æãã
ããã©ã«ãã ã¨ããã¡ã¤ã«ã®ã¢ãããã¼ãæã«Versionãã¡ã¤ã«ãå ¨ã¦ä½æãã¾ãã
ããã ã¨ã
- Versionãã¡ã¤ã«ã®å®¹éãããã
- ãã¨ããVersionãã¡ã¤ã«ã®è¨å®ã追å ããå ´åã以åããã®ãã¡ã¤ã«ã«å¯¾å¿ã§ããªã
çãããã®ã§ãVersionãã¡ã¤ã«ãViewã§å¼ã³åºããã¨ãã«ä½æããããã«æ¹ä¿®ãã¦ã¿ã¾ããã
ãã¡ãã£ã¨ãããããï¼æ¹åã®ä½å°ãããããã§ããã»ã»ã»ã»
ã¢ãããã¼ãæã®Versionãã¡ã¤ã«ä½æåæ¢
Versionãã¡ã¤ã«ã®ä½æãè¡ãªããå¦ãã®è¨å®ã¯ãMediaããã¤ãã¢ã®$_defaultSettingsã«ããã¾ãã
var $_defaultSettings = array( 'metadataLevel' => 1, 'baseDirectory' => MEDIA, 'makeVersions' => true, // âãã 'filterDirectory' => MEDIA_FILTER, 'createDirectory' => true, );
ããããアップロードファイル名の変更ã¨åæ§ã«ãæ¡å¼µAttachmentã¢ãã«ã§è¨å®å¤æ´ãã¦ãVersionãã¡ã¤ã«ã®ä½æè¨å®ãOFFã«ãã¾ãã
plugins/media/models/attachment_ex.php
@@ -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;
ããã§ãã¢ãããã¼ãããæã«transferãã£ã¬ã¯ããªã«ãã¡ã¤ã«ã¯ä¿ç®¡ããã¾ãããfiltersã«ã¯åºåãããªããªãã¾ãã
[cake@cake cake-frame]$ ls app/webroot/media/transfer/img/
4b7cde08-c068-430f-80d8-0a72c0a80b08.jpg
[cake@cake cake-frame]$ ls app/webroot/media/filter/s/transfer/img/
[cake@cake cake-frame]$
Versionãã¡ã¤ã«ã®é²è¦§æåºå
次ã«ãversionãã¡ã¤ã«ã«ã¢ã¯ã»ã¹ãâããªããã°ä½æãã¦ãåºåãã®å¦çã追å ãã¾ãã
Versionãã¡ã¤ã«ã®ä½æã¡ã½ããã¯ãmediaãã©ã°ã¤ã³ã®mediaããã¤ãã¢ã«ããã¾ãã
afterSave()ã¡ã½ããããã³ã¼ã«ããã¦ããã¨ã§è¦å½ãã¤ãã¾ããã
mediumãã«ãã¼ã§ãã¡ã¤ã«è¡¨ç¤ºã®å ¸åä¾ã¯ã
- file()ã§ãã¹ãåå¾
- embed()ã§ã¿ã°ãä½æ
ã®äºæ®µéã§ãã
echo $medium->embed($medium->file('filter/s', $target_user['Attachment'][0]));
$target_user['Attachment'][0]ã®ä¾
id
ã79
dirname
ãtransfer/img
basename
ã4b81d7d9-0e28-451d-b4e4-20e0c0a80b08.jpg
alternative
ãnull
mediumãã«ãã¼ãæ¡å¼µãã¦ãfile()ã¡ã½ããã§Versionãã¡ã¤ã«ãåºåããç¬èªãã«ãã¼ãä½æãã¾ããã
Versionãã¡ã¤ã«ä½æã®ã¡ã½ããã¯ãMVCã¨ã¯ç¬ç«ã®ã©ã¤ãã©ãªã¨ãã¦è¿½å ãã¾ããã
ç´°ããå¦çãããããããã¾ãããè¦ç¹ã¯ã
- core.phpã§æå®ã®Versionãã¡ã¤ã«åºåè¨å®ãèªã¿è¾¼ã
- æå®ãµã¤ãºä»¥å¤ã®åºåè¨å®ã¯åé¤ãã¦ãmake()ã¡ã½ããå®è¡
ã®2ç¹ã§ãã
追å ã½ã¼ã¹
media/views/helpers/upfile.php
<?php class UpfileHelper extends MediumHelper { 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; } }
media/libs/upfile.php
<?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; } }
Uploadãã«ãã¼ã使ç¨ããããã®è¿½å æ¹ä¿®
app/controllers/app_controller.php
var $helpers = array( 'Html', 'Form', 'Media.Medium', + 'Media.Upfile', 'Token' );
app/views/elements/home.ctp
-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 " "; +}