[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class PhabricatorFileTransformController 4 extends PhabricatorFileController { 5 6 private $transform; 7 private $phid; 8 private $key; 9 10 public function shouldRequireLogin() { 11 return false; 12 } 13 14 public function willProcessRequest(array $data) { 15 $this->transform = $data['transform']; 16 $this->phid = $data['phid']; 17 $this->key = $data['key']; 18 } 19 20 public function processRequest() { 21 $viewer = $this->getRequest()->getUser(); 22 23 // NOTE: This is a public/CDN endpoint, and permission to see files is 24 // controlled by knowing the secret key, not by authentication. 25 26 $file = id(new PhabricatorFileQuery()) 27 ->setViewer(PhabricatorUser::getOmnipotentUser()) 28 ->withPHIDs(array($this->phid)) 29 ->executeOne(); 30 if (!$file) { 31 return new Aphront404Response(); 32 } 33 34 if (!$file->validateSecretKey($this->key)) { 35 return new Aphront403Response(); 36 } 37 38 $xform = id(new PhabricatorTransformedFile()) 39 ->loadOneWhere( 40 'originalPHID = %s AND transform = %s', 41 $this->phid, 42 $this->transform); 43 44 if ($xform) { 45 return $this->buildTransformedFileResponse($xform); 46 } 47 48 $type = $file->getMimeType(); 49 50 if (!$file->isViewableInBrowser() || !$file->isTransformableImage()) { 51 return $this->buildDefaultTransformation($file); 52 } 53 54 // We're essentially just building a cache here and don't need CSRF 55 // protection. 56 $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); 57 58 switch ($this->transform) { 59 case 'thumb-profile': 60 $xformed_file = $this->executeThumbTransform($file, 50, 50); 61 break; 62 case 'thumb-280x210': 63 $xformed_file = $this->executeThumbTransform($file, 280, 210); 64 break; 65 case 'thumb-220x165': 66 $xformed_file = $this->executeThumbTransform($file, 220, 165); 67 break; 68 case 'preview-100': 69 $xformed_file = $this->executePreviewTransform($file, 100); 70 break; 71 case 'preview-220': 72 $xformed_file = $this->executePreviewTransform($file, 220); 73 break; 74 case 'thumb-160x120': 75 $xformed_file = $this->executeThumbTransform($file, 160, 120); 76 break; 77 case 'thumb-60x45': 78 $xformed_file = $this->executeThumbTransform($file, 60, 45); 79 break; 80 default: 81 return new Aphront400Response(); 82 } 83 84 if (!$xformed_file) { 85 return new Aphront400Response(); 86 } 87 88 $xform = new PhabricatorTransformedFile(); 89 $xform->setOriginalPHID($this->phid); 90 $xform->setTransform($this->transform); 91 $xform->setTransformedPHID($xformed_file->getPHID()); 92 $xform->save(); 93 94 return $this->buildTransformedFileResponse($xform); 95 } 96 97 private function buildDefaultTransformation(PhabricatorFile $file) { 98 static $regexps = array( 99 '@application/zip@' => 'zip', 100 '@image/@' => 'image', 101 '@application/pdf@' => 'pdf', 102 '@.*@' => 'default', 103 ); 104 105 $type = $file->getMimeType(); 106 $prefix = 'default'; 107 foreach ($regexps as $regexp => $implied_prefix) { 108 if (preg_match($regexp, $type)) { 109 $prefix = $implied_prefix; 110 break; 111 } 112 } 113 114 switch ($this->transform) { 115 case 'thumb-280x210': 116 $suffix = '280x210'; 117 break; 118 case 'thumb-160x120': 119 $suffix = '160x120'; 120 break; 121 case 'thumb-60x45': 122 $suffix = '60x45'; 123 break; 124 case 'preview-100': 125 $suffix = '.p100'; 126 break; 127 default: 128 throw new Exception('Unsupported transformation type!'); 129 } 130 131 $path = celerity_get_resource_uri( 132 "rsrc/image/icon/fatcow/thumbnails/{$prefix}{$suffix}.png"); 133 134 return id(new AphrontRedirectResponse()) 135 ->setURI($path); 136 } 137 138 private function buildTransformedFileResponse( 139 PhabricatorTransformedFile $xform) { 140 141 $file = id(new PhabricatorFileQuery()) 142 ->setViewer(PhabricatorUser::getOmnipotentUser()) 143 ->withPHIDs(array($xform->getTransformedPHID())) 144 ->executeOne(); 145 if (!$file) { 146 return new Aphront404Response(); 147 } 148 149 // TODO: We could just delegate to the file view controller instead, 150 // which would save the client a roundtrip, but is slightly more complex. 151 152 return $file->getRedirectResponse(); 153 } 154 155 private function executePreviewTransform(PhabricatorFile $file, $size) { 156 $xformer = new PhabricatorImageTransformer(); 157 return $xformer->executePreviewTransform($file, $size); 158 } 159 160 private function executeThumbTransform(PhabricatorFile $file, $x, $y) { 161 $xformer = new PhabricatorImageTransformer(); 162 return $xformer->executeThumbTransform($file, $x, $y); 163 } 164 165 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |