SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | ||
3 | class S3_Controller extends Extension { | |
4 | ||
5 | ||
6 | public function onBeforeInit() { | |
7 | $this->postRequest(); | |
8 | } | |
9 | ||
10 | public function postRequest(SS_HTTPRequest $request, DataModel $model = null) { | |
11 | ||
12 | - | $response = parent::handleRequest($request, $model); |
12 | + | $response = $this->owner->response; |
13 | if ($body = $response->getBody()) { | |
14 | $body = self::replaceCDN($body); | |
15 | $response->setBody($body); | |
16 | } | |
17 | return $response; | |
18 | } | |
19 | ||
20 | ||
21 | static function replaceCDN($body) { | |
22 | if (!$body) { | |
23 | return; | |
24 | } | |
25 | ||
26 | $cdn = "//" . S3_BUCKET . ".s3.amazonaws.com/assets"; // or a working cname record | |
27 | $myDomain = Director::absoluteBaseURL(); | |
28 | echo $myDomain; | |
29 | die; | |
30 | $body = str_replace('src="assets/', 'src="'.$cdn.'/', $body); | |
31 | $body = str_replace('href="assets/', 'href="'.$cdn.'/', $body); | |
32 | $body = str_replace($myDomain . 'assets/', $cdn.'/', $body); //absolute links | |
33 | ||
34 | return $body; | |
35 | } | |
36 | ||
37 | ||
38 | } |