Skip to content

Commit 56a965f

Browse files
author
Matthias Lill
committed
Serve files using a CDN Hosts address
1 parent ff34e93 commit 56a965f

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

Config/bootstrap.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
*
88
*/
99
Configure::write(
10-
'DatabaseBlobFile',
11-
array(
12-
'sizes' => array(
10+
'DatabaseBlobFile',
11+
array(
12+
'cdn_host' => '',
13+
'sizes' => array(
1314
'md1' => array('resizewidth',67),
1415
'md2' => array('resizewidth',165),
1516
'md3' => array('resizewidth',263),
@@ -23,5 +24,5 @@
2324
'md11' => array('resizewidth',1043),
2425
'md12' => array('resizewidth',1170),
2526
),
26-
)
27+
)
2728
);

Model/Behavior/BlobFileBehavior.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ public function beforeFind(Model $model, $query) {
2222

2323
foreach ($columns as $field => $type) {
2424
if ($type == 'binary') {
25+
$cdnHost = Configure::read('DatabaseBlobFile.cdn_host');
2526
$model->virtualFields[$field] = 'LENGTH('.$model->name.'.'.$field.')';
26-
$model->virtualFields[$field.'_basepath'] = "CONCAT('/file/','".$model->name."','/',".$model->name.'.'.$model->primaryKey.",'/','".$field."','/', date_format(".$model->name.".modified,'%y%m%d%h%i'))";
27+
$model->virtualFields[$field.'_basepath'] = "CONCAT('".$cdnHost."/file/','".$model->name."','/',".$model->name.'.'.$model->primaryKey.",'/','".$field."','/', date_format(".$model->name.".modified,'%y%m%d%h%i'))";
2728
}
2829
}
2930
return $query;

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ resize to max width and height, then crop to center
8585

8686
Every file which has been served once will be stored inside the webroot for caching with a timestamp. This is supposed to work with a deployment processes which resets the app folder to its correct state with every deployment.
8787

88+
### Cache using CDN
89+
90+
You can set a CDN host address inside your config file. This will reduce request to your server and files stored inside your webroot.
91+
8892
## Roadmap
8993

9094
- Support for more file types than just images

0 commit comments

Comments
 (0)