public function handleRequest(AphrontRequest $request) { $viewer = $this->getViewer(); $file = id(new PhabricatorFileQuery())->setViewer($viewer)->withIDs(array($request->getURIData('id')))->executeOne(); if (!$file) { return new Aphront404Response(); } $monogram = $file->getMonogram(); $xdst = id(new PhabricatorTransformedFile())->loadAllWhere('transformedPHID = %s', $file->getPHID()); $dst_rows = array(); foreach ($xdst as $source) { $dst_rows[] = array($source->getTransform(), $viewer->renderHandle($source->getOriginalPHID())); } $dst_table = id(new AphrontTableView($dst_rows))->setHeaders(array(pht('Key'), pht('Source')))->setColumnClasses(array('', 'wide'))->setNoDataString(pht('This file was not created by transforming another file.')); $xsrc = id(new PhabricatorTransformedFile())->loadAllWhere('originalPHID = %s', $file->getPHID()); $xsrc = mpull($xsrc, 'getTransformedPHID', 'getTransform'); $src_rows = array(); $xforms = PhabricatorFileTransform::getAllTransforms(); foreach ($xforms as $xform) { $dst_phid = idx($xsrc, $xform->getTransformKey()); if ($xform->canApplyTransform($file)) { $can_apply = pht('Yes'); $view_href = $file->getURIForTransform($xform); $view_href = new PhutilURI($view_href); $view_href->setQueryParam('regenerate', 'true'); $view_text = pht('Regenerate'); $view_link = phutil_tag('a', array('class' => 'small grey button', 'href' => $view_href), $view_text); } else { $can_apply = phutil_tag('em', array(), pht('No')); $view_link = phutil_tag('em', array(), pht('None')); } if ($dst_phid) { $dst_link = $viewer->renderHandle($dst_phid); } else { $dst_link = phutil_tag('em', array(), pht('None')); } $src_rows[] = array($xform->getTransformName(), $xform->getTransformKey(), $can_apply, $dst_link, $view_link); } $src_table = id(new AphrontTableView($src_rows))->setHeaders(array(pht('Name'), pht('Key'), pht('Supported'), pht('Transform'), pht('View')))->setColumnClasses(array('wide', '', '', '', 'action')); $crumbs = $this->buildApplicationCrumbs(); $crumbs->addTextCrumb($monogram, '/' . $monogram); $crumbs->addTextCrumb(pht('Transforms')); $crumbs->setBorder(true); $dst_box = id(new PHUIObjectBoxView())->setHeaderText(pht('File Sources'))->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)->setTable($dst_table); $src_box = id(new PHUIObjectBoxView())->setHeaderText(pht('Available Transforms'))->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)->setTable($src_table); $title = pht('%s Transforms', $file->getName()); $header = id(new PHUIHeaderView())->setHeader($title)->setHeaderIcon('fa-arrows-alt'); $view = id(new PHUITwoColumnView())->setHeader($header)->setFooter(array($dst_box, $src_box)); return $this->newPage()->setTitle($title)->setCrumbs($crumbs)->appendChild($view); }
public function handleRequest(AphrontRequest $request) { $viewer = $this->getViewer(); // NOTE: This is a public/CDN endpoint, and permission to see files is // controlled by knowing the secret key, not by authentication. $is_regenerate = $request->getBool('regenerate'); $source_phid = $request->getURIData('phid'); $file = id(new PhabricatorFileQuery())->setViewer(PhabricatorUser::getOmnipotentUser())->withPHIDs(array($source_phid))->executeOne(); if (!$file) { return new Aphront404Response(); } $secret_key = $request->getURIData('key'); if (!$file->validateSecretKey($secret_key)) { return new Aphront403Response(); } $transform = $request->getURIData('transform'); $xform = $this->loadTransform($source_phid, $transform); if ($xform) { if ($is_regenerate) { $this->destroyTransform($xform); } else { return $this->buildTransformedFileResponse($xform); } } $xforms = PhabricatorFileTransform::getAllTransforms(); if (!isset($xforms[$transform])) { return new Aphront404Response(); } $xform = $xforms[$transform]; // We're essentially just building a cache here and don't need CSRF // protection. $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); $xformed_file = null; if ($xform->canApplyTransform($file)) { try { $xformed_file = $xforms[$transform]->applyTransform($file); } catch (Exception $ex) { // In normal transform mode, we ignore failures and generate a // default transform below. If we're explicitly regenerating the // thumbnail, rethrow the exception. if ($is_regenerate) { throw $ex; } } } if (!$xformed_file) { $xformed_file = $xform->getDefaultTransform($file); } if (!$xformed_file) { return new Aphront400Response(); } $xform = id(new PhabricatorTransformedFile())->setOriginalPHID($source_phid)->setTransform($transform)->setTransformedPHID($xformed_file->getPHID()); 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(); } } return $this->buildTransformedFileResponse($xform); }
public function testGetAllTransforms() { PhabricatorFileTransform::getAllTransforms(); $this->assertTrue(true); }