mediaãã©ã°ã¤ã³å¿ç¨(2) ã¢ãããã¼ããã¡ã¤ã«åã®å¤æ´
mediaãã©ã°ã¤ã³ã§ã¢ãããã¼ããããã¡ã¤ã«åããä»»æã«å¤æ´ãã¦ã¿ã¾ããã
å ã ã®ãã¡ã¤ã«åã®ã¾ã¾ã ã¨ãæ¥æ¬èªã®ãã¡ã¤ã«åã§ä¸æ£ã¨ã©ã¼ã«ãªãã±ã¼ã¹ããã£ããããµã¼ãå´ã§ãã¡ã¤ã«ãæ±ããªããªãå¯è½æ§ãããã®ã§ããã¡ã¤ã«åå¤æ´ã¯å¿ é ã§ãã
Attachmentã¢ãã«ãæ¡å¼µãã¦å®è£ ãã¾ããã
参考サイトããã«ã
ä¿åãã¡ã¤ã«åã®å¤æ´
media/models/behaviors/transfer.php ã®ã³ã¡ã³ããã¿ãã¨ã ãããåããã
ã¨ããã®ã§ãã½ã¼ã¹ãè¦ã¦è¦ã¾ããã
ãã¶ãããã®ãã¨ã§ãããã
media/models/behaviors/transfer.php
/**
* Default settings
(ä¸ç¥ï¼
* destinationFile
* A path (withouth leading slash) relative to `baseDirectory`.
*
* These markers can be used:
* :DS: Directory separator `'/'` or `'\'`
* :uuid: An uuid generated by String::uuid()
* :day: The current day
* :month: The current month
* :year: The current year
* :Model.name:
* :Model.alias:
* :Model.xyz: Where `xyz` is a field of the submitted record
* :Source.basename: e.g. `'logo.png'`
* :Source.filename: e.g. `'logo'`
* :Source.extension: e.g. `'png'`
* :Source.mimeType: e.g. `'image_png'`
* :Medium.name: Lowercased medium name of the source file (e.g. `'image'`)
* :Medium.short: Short medium name of the source file (e.g. `'img'`)
*
ããã©ã«ãã®è¨å®ã
var $_defaultSettings = array( 'trustClient' => false, 'destinationFile' => ':Medium.short::DS::Source.basename:',
ãªã®ã§ãç»åãã¢ããããã¨
ããimg/å
ã®ãã¡ã¤ã«å.æ¡å¼µå
ã¨ãªãçå±ã«ããã¾ãã
ããã¤ãã¢ã®è¨å®ã¯ãã¢ãã«ã§ä½¿ãæã®$actAsã§å¤æ´ã§ãã¾ãã
http://book.cakephp.org/ja/view/90/Using-Behaviors
Attachmentã¢ãã«ã§ããè¨å®ãæ示ããã¦ãã¾ããã
media/models/attachment.php
var $actsAs = array( (ä¸ç¥ï¼ 'Media.Transfer' => array( 'trustClient' => false, 'destinationFile' => ':Medium.short::DS::Source.basename:',
ã¨ããããtransferããã¤ãã¢ã§ã¯ã$_defaultSettingsãsetup()ã¡ã½ããã§èªã¿è¾¼ã¿ã»ãããã¦ãã¾ã£ã¦ãã¾ãã
function setup(&$Model, $config = null) { (ä¸ç¥ï¼ $this->settings[$Model->alias] = $config + $this->_defaultSettings; $this->runtime[$Model->alias] = $this->_defaultRuntime; }
Attachmentã¢ãã«ãå¼ã³åºããå¾ã«$actsAsã®è¨å®ãå¤æ´ãã¦ããããæå³ãããã¾ããã»ã»ã»
mediaãã©ã°ã¤ã³èªä½ã¯å¤æ´ããããªãã£ãã®ã§ãAttachmentã®æ¡å¼µã¢ãã«ãä½ã£ã¦$actsAsãè¨å®ãã¾ããã
attachment_ex.php
<?php /** * Extend of Attachment Model File */ app::import('Media.Attachment'); class AttachmentEx extends Attachment { var $name = 'AttachmentEx'; function __construct($id = false, $table = null, $ds = null) { $this->actsAs['Media.Transfer']['destinationFile'] = ':Medium.short::DS::uuid:'.'.'.':Source.extension:'; parent::__construct($id, $table, $ds); } }
__construct()ã®å¤æ°ã¯ãæ¡å¼µå ã辿ã£ã¦model.phpããã¼ã¹ã«è¨å®ãã¦ãã¾ãã
AttachmentEx対å¿ã®ã½ã¼ã¹å¤æ´ç¹
--- a/app/models/user.php +++ b/app/models/user.php @@ -14,7 +14,7 @@ class User extends AppModel { var $hasMany = array( 'Attachment' => array( - 'className' => 'Media.Attachment', + 'className' => 'Media.AttachmentEx', 'foreignKey' => 'foreign_key',
以ä¸ã®æ¹ä¿®ã§ãã¢ãããã¼ããã¡ã¤ã«åãString::uuid()ã§çæããæååã«ãããã¨ãã§ãã¾ããã
mysql> SELECT * FROM attachments\G *************************** 1. row *************************** id: 30 model: User foreign_key: 1 dirname: transfer/img basename: 4b7bfb71-3f10-43c9-a6a7-0a72c0a80b08.jpg checksum: 44f459734b6067be6aefd27b75a5ded3 group: attachment alternative: created: 2010-02-17 23:21:37 modified: 2010-02-17 23:21:37 1 row in set (0.00 sec) [cake@cake cake-frame]$ ls app/webroot/media/transfer/img/4b7bfb71-3f10-43c9-a6a7-0a72c0a80b08.jpg app/webroot/media/transfer/img/4b7bfb71-3f10-43c9-a6a7-0a72c0a80b08.jpg