3 final class PhabricatorFileTransformController
4 extends PhabricatorFileController
{
6 public function shouldRequireLogin() {
10 public function handleRequest(AphrontRequest
$request) {
11 $viewer = $this->getViewer();
13 // NOTE: This is a public/CDN endpoint, and permission to see files is
14 // controlled by knowing the secret key, not by authentication.
16 $is_regenerate = $request->getBool('regenerate');
18 $source_phid = $request->getURIData('phid');
19 $file = id(new PhabricatorFileQuery())
20 ->setViewer(PhabricatorUser
::getOmnipotentUser())
21 ->withPHIDs(array($source_phid))
24 return new Aphront404Response();
27 $secret_key = $request->getURIData('key');
28 if (!$file->validateSecretKey($secret_key)) {
29 return new Aphront403Response();
32 $transform = $request->getURIData('transform');
33 $xform = $this->loadTransform($source_phid, $transform);
37 $this->destroyTransform($xform);
39 return $this->buildTransformedFileResponse($xform);
43 $xforms = PhabricatorFileTransform
::getAllTransforms();
44 if (!isset($xforms[$transform])) {
45 return new Aphront404Response();
48 $xform = $xforms[$transform];
50 // We're essentially just building a cache here and don't need CSRF
52 $unguarded = AphrontWriteGuard
::beginScopedUnguardedWrites();
55 if ($xform->canApplyTransform($file)) {
57 $xformed_file = $xforms[$transform]->applyTransform($file);
58 } catch (Exception
$ex) {
59 // In normal transform mode, we ignore failures and generate a
60 // default transform below. If we're explicitly regenerating the
61 // thumbnail, rethrow the exception.
69 $xformed_file = $xform->getDefaultTransform($file);
73 return new Aphront400Response();
76 $xform = id(new PhabricatorTransformedFile())
77 ->setOriginalPHID($source_phid)
78 ->setTransform($transform)
79 ->setTransformedPHID($xformed_file->getPHID());
83 } catch (AphrontDuplicateKeyQueryException
$ex) {
84 // If we collide when saving, we've raced another endpoint which was
85 // transforming the same file. Just throw our work away and use that
87 $this->destroyTransform($xform);
88 $xform = $this->loadTransform($source_phid, $transform);
90 return new Aphront404Response();
94 return $this->buildTransformedFileResponse($xform);
97 private function buildTransformedFileResponse(
98 PhabricatorTransformedFile
$xform) {
100 $file = id(new PhabricatorFileQuery())
101 ->setViewer(PhabricatorUser
::getOmnipotentUser())
102 ->withPHIDs(array($xform->getTransformedPHID()))
105 return new Aphront404Response();
108 // TODO: We could just delegate to the file view controller instead,
109 // which would save the client a roundtrip, but is slightly more complex.
111 return $file->getRedirectResponse();
114 private function destroyTransform(PhabricatorTransformedFile
$xform) {
115 $engine = new PhabricatorDestructionEngine();
116 $file = id(new PhabricatorFileQuery())
117 ->setViewer($engine->getViewer())
118 ->withPHIDs(array($xform->getTransformedPHID()))
121 $unguarded = AphrontWriteGuard
::beginScopedUnguardedWrites();
124 if ($xform->getID()) {
128 $engine->destroyObject($file);
134 private function loadTransform($source_phid, $transform) {
135 return id(new PhabricatorTransformedFile())->loadOneWhere(
136 'originalPHID = %s AND transform = %s',