|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright 2012 Facebook, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * @group phame |
| 21 | + */ |
| 22 | +final class PhameResourceController extends CelerityResourceController { |
| 23 | + |
| 24 | + private $id; |
| 25 | + private $hash; |
| 26 | + private $name; |
| 27 | + private $root; |
| 28 | + |
| 29 | + protected function getRootDirectory() { |
| 30 | + return $this->root; |
| 31 | + } |
| 32 | + |
| 33 | + public function willProcessRequest(array $data) { |
| 34 | + $this->id = $data['id']; |
| 35 | + $this->hash = $data['hash']; |
| 36 | + $this->name = $data['name']; |
| 37 | + } |
| 38 | + |
| 39 | + public function processRequest() { |
| 40 | + $request = $this->getRequest(); |
| 41 | + $user = $request->getUser(); |
| 42 | + |
| 43 | + // We require a visible blog associated with a given skin to serve |
| 44 | + // resources, so you can't go fishing around where you shouldn't be. |
| 45 | + |
| 46 | + $blog = id(new PhameBlogQuery()) |
| 47 | + ->setViewer($user) |
| 48 | + ->withIDs(array($this->id)) |
| 49 | + ->executeOne(); |
| 50 | + if (!$blog) { |
| 51 | + return new Aphront404Response(); |
| 52 | + } |
| 53 | + |
| 54 | + $skin = $blog->getSkinRenderer($request); |
| 55 | + $spec = $skin->getSpecification(); |
| 56 | + |
| 57 | + $this->root = $spec->getRootDirectory().DIRECTORY_SEPARATOR; |
| 58 | + return $this->serveResource($this->name, $package_hash = null); |
| 59 | + } |
| 60 | + |
| 61 | + protected function buildResourceTransformer() { |
| 62 | + $xformer = new CelerityResourceTransformer(); |
| 63 | + $xformer->setMinify(false); |
| 64 | + $xformer->setTranslateURICallback(array($this, 'translateResourceURI')); |
| 65 | + return $xformer; |
| 66 | + } |
| 67 | + |
| 68 | + public function translateResourceURI(array $matches) { |
| 69 | + $uri = trim($matches[1], "'\" \r\t\n"); |
| 70 | + |
| 71 | + if (Filesystem::pathExists($this->root.$uri)) { |
| 72 | + $hash = filemtime($this->root.$uri); |
| 73 | + } else { |
| 74 | + $hash = '-'; |
| 75 | + } |
| 76 | + |
| 77 | + $uri = '/phame/r/'.$this->id.'/'.$hash.'/'.$uri; |
| 78 | + return 'url('.$uri.')'; |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments