Differential D12965 Diff 31242 src/applications/files/controller/PhabricatorFileTransformController.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/controller/PhabricatorFileTransformController.php
Show All 24 Lines | public function handleRequest(AphrontRequest $request) { | ||||
} | } | ||||
$secret_key = $request->getURIData('key'); | $secret_key = $request->getURIData('key'); | ||||
if (!$file->validateSecretKey($secret_key)) { | if (!$file->validateSecretKey($secret_key)) { | ||||
return new Aphront403Response(); | return new Aphront403Response(); | ||||
} | } | ||||
$transform = $request->getURIData('transform'); | $transform = $request->getURIData('transform'); | ||||
$xform = id(new PhabricatorTransformedFile()) | $xform = $this->loadTransform($source_phid, $transform); | ||||
->loadOneWhere( | |||||
'originalPHID = %s AND transform = %s', | |||||
$source_phid, | |||||
$transform); | |||||
if ($xform) { | if ($xform) { | ||||
if ($is_regenerate) { | if ($is_regenerate) { | ||||
$this->destroyTransform($xform); | $this->destroyTransform($xform); | ||||
} else { | } else { | ||||
return $this->buildTransformedFileResponse($xform); | return $this->buildTransformedFileResponse($xform); | ||||
} | } | ||||
} | } | ||||
Show All 29 Lines | public function handleRequest(AphrontRequest $request) { | ||||
if (!$xformed_file) { | if (!$xformed_file) { | ||||
return new Aphront400Response(); | return new Aphront400Response(); | ||||
} | } | ||||
$xform = id(new PhabricatorTransformedFile()) | $xform = id(new PhabricatorTransformedFile()) | ||||
->setOriginalPHID($source_phid) | ->setOriginalPHID($source_phid) | ||||
->setTransform($transform) | ->setTransform($transform) | ||||
->setTransformedPHID($xformed_file->getPHID()) | ->setTransformedPHID($xformed_file->getPHID()); | ||||
->save(); | |||||
try { | |||||
$xform->save(); | |||||
} catch (AphrontDuplicateKeyQueryException $ex) { | |||||
// If we collide when saving, we've raced another endpoint which was | |||||
// transforming the same file. Just throw our work away and use that | |||||
// transform instead. | |||||
$this->destroyTransform($xform); | |||||
$xform = $this->loadTransform($source_phid, $transform); | |||||
if (!$xform) { | |||||
return new Aphront404Response(); | |||||
btrahan: Is this more like a 500? | |||||
Not Done Inline ActionsMy thinking on 404 is that we "should" only get here if the thing was just deleted, and we'd 404 if we were a fraction of a second quicker (on line 105, or after redirecting on line 111), and 404'ing in those cases makes fairly good sense to me. In theory, we don't need to 404 here and could just loop back to the top and rebuild the transform, or loop here trying to either save our own transform or load a conflicting one, but those are both complicated and introduce the possibility that we might loop forever if there's a bug somewhere. epriestley: My thinking on 404 is that we "should" only get here if the thing was just deleted, and we'd… | |||||
} | |||||
} | |||||
return $this->buildTransformedFileResponse($xform); | return $this->buildTransformedFileResponse($xform); | ||||
} | } | ||||
private function buildTransformedFileResponse( | private function buildTransformedFileResponse( | ||||
PhabricatorTransformedFile $xform) { | PhabricatorTransformedFile $xform) { | ||||
$file = id(new PhabricatorFileQuery()) | $file = id(new PhabricatorFileQuery()) | ||||
Show All 15 Lines | private function destroyTransform(PhabricatorTransformedFile $xform) { | ||||
$file = id(new PhabricatorFileQuery()) | $file = id(new PhabricatorFileQuery()) | ||||
->setViewer($engine->getViewer()) | ->setViewer($engine->getViewer()) | ||||
->withPHIDs(array($xform->getTransformedPHID())) | ->withPHIDs(array($xform->getTransformedPHID())) | ||||
->executeOne(); | ->executeOne(); | ||||
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); | $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); | ||||
if (!$file) { | if (!$file) { | ||||
if ($xform->getID()) { | |||||
$xform->delete(); | $xform->delete(); | ||||
} | |||||
} else { | } else { | ||||
$engine->destroyObject($file); | $engine->destroyObject($file); | ||||
} | } | ||||
unset($unguarded); | unset($unguarded); | ||||
} | } | ||||
private function loadTransform($source_phid, $transform) { | |||||
return id(new PhabricatorTransformedFile())->loadOneWhere( | |||||
'originalPHID = %s AND transform = %s', | |||||
$source_phid, | |||||
$transform); | |||||
} | |||||
} | } |
Is this more like a 500?