public static function generateMacro($viewer, $macro_name, $upper_text, $lower_text)
 {
     $macro = id(new PhabricatorMacroQuery())->setViewer($viewer)->withNames(array($macro_name))->needFiles(true)->executeOne();
     if (!$macro) {
         return false;
     }
     $file = $macro->getFile();
     $upper_text = strtoupper($upper_text);
     $lower_text = strtoupper($lower_text);
     $mixed_text = md5($upper_text) . ':' . md5($lower_text);
     $hash = 'meme' . hash('sha256', $mixed_text);
     $xform = id(new PhabricatorTransformedFile())->loadOneWhere('originalphid=%s and transform=%s', $file->getPHID(), $hash);
     if ($xform) {
         $memefile = id(new PhabricatorFileQuery())->setViewer($viewer)->withPHIDs(array($xform->getTransformedPHID()))->executeOne();
         if ($memefile) {
             return $memefile->getBestURI();
         }
     }
     $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
     $transformers = new PhabricatorImageTransformer();
     $newfile = $transformers->executeMemeTransform($file, $upper_text, $lower_text);
     $xfile = new PhabricatorTransformedFile();
     $xfile->setOriginalPHID($file->getPHID());
     $xfile->setTransformedPHID($newfile->getPHID());
     $xfile->setTransform($hash);
     $xfile->save();
     return $newfile->getBestURI();
 }
 public function processRequest()
 {
     $xform = id(new PhabricatorTransformedFile())->loadOneWhere('originalPHID = %s AND transform = %s', $this->phid, $this->transform);
     if ($xform) {
         return $this->buildTransformedFileResponse($xform);
     }
     $file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $this->phid);
     if (!$file) {
         return new Aphront404Response();
     }
     $type = $file->getMimeType();
     if (!$file->isViewableInBrowser() || !$file->isTransformableImage()) {
         return $this->buildDefaultTransformation($file);
     }
     switch ($this->transform) {
         case 'thumb-160x120':
             $xformed_file = $this->executeThumbTransform($file, 160, 120);
             break;
         case 'thumb-60x45':
             $xformed_file = $this->executeThumbTransform($file, 60, 45);
             break;
         default:
             return new Aphront400Response();
     }
     if (!$xformed_file) {
         return new Aphront400Response();
     }
     $xform = new PhabricatorTransformedFile();
     $xform->setOriginalPHID($this->phid);
     $xform->setTransform($this->transform);
     $xform->setTransformedPHID($xformed_file->getPHID());
     $xform->save();
     return $this->buildTransformedFileResponse($xform);
 }
 public function processRequest()
 {
     $viewer = $this->getRequest()->getUser();
     // NOTE: This is a public/CDN endpoint, and permission to see files is
     // controlled by knowing the secret key, not by authentication.
     $file = id(new PhabricatorFileQuery())->setViewer(PhabricatorUser::getOmnipotentUser())->withPHIDs(array($this->phid))->executeOne();
     if (!$file) {
         return new Aphront404Response();
     }
     if (!$file->validateSecretKey($this->key)) {
         return new Aphront403Response();
     }
     $xform = id(new PhabricatorTransformedFile())->loadOneWhere('originalPHID = %s AND transform = %s', $this->phid, $this->transform);
     if ($xform) {
         return $this->buildTransformedFileResponse($xform);
     }
     $type = $file->getMimeType();
     if (!$file->isViewableInBrowser() || !$file->isTransformableImage()) {
         return $this->buildDefaultTransformation($file);
     }
     // We're essentially just building a cache here and don't need CSRF
     // protection.
     $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
     switch ($this->transform) {
         case 'thumb-profile':
             $xformed_file = $this->executeThumbTransform($file, 50, 50);
             break;
         case 'thumb-280x210':
             $xformed_file = $this->executeThumbTransform($file, 280, 210);
             break;
         case 'thumb-220x165':
             $xformed_file = $this->executeThumbTransform($file, 220, 165);
             break;
         case 'preview-100':
             $xformed_file = $this->executePreviewTransform($file, 100);
             break;
         case 'preview-220':
             $xformed_file = $this->executePreviewTransform($file, 220);
             break;
         case 'thumb-160x120':
             $xformed_file = $this->executeThumbTransform($file, 160, 120);
             break;
         case 'thumb-60x45':
             $xformed_file = $this->executeThumbTransform($file, 60, 45);
             break;
         default:
             return new Aphront400Response();
     }
     if (!$xformed_file) {
         return new Aphront400Response();
     }
     $xform = new PhabricatorTransformedFile();
     $xform->setOriginalPHID($this->phid);
     $xform->setTransform($this->transform);
     $xform->setTransformedPHID($xformed_file->getPHID());
     $xform->save();
     return $this->buildTransformedFileResponse($xform);
 }
 public function processRequest()
 {
     $xform = id(new PhabricatorTransformedFile())->loadOneWhere('originalPHID = %s AND transform = %s', $this->phid, $this->transform);
     if ($xform) {
         return $this->buildTransformedFileResponse($xform);
     }
     $file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $this->phid);
     if (!$file) {
         return new Aphront404Response();
     }
     $type = $file->getMimeType();
     if (!$file->isViewableInBrowser() || !$file->isTransformableImage()) {
         return $this->buildDefaultTransformation($file);
     }
     // We're essentially just building a cache here and don't need CSRF
     // protection.
     $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
     switch ($this->transform) {
         case 'thumb-220x165':
             $xformed_file = $this->executeThumbTransform($file, 220, 165);
             break;
         case 'preview-220':
             $xformed_file = $this->executePreviewTransform($file, 220);
             break;
         case 'thumb-160x120':
             $xformed_file = $this->executeThumbTransform($file, 160, 120);
             break;
         case 'thumb-60x45':
             $xformed_file = $this->executeThumbTransform($file, 60, 45);
             break;
         default:
             return new Aphront400Response();
     }
     if (!$xformed_file) {
         return new Aphront400Response();
     }
     $xform = new PhabricatorTransformedFile();
     $xform->setOriginalPHID($this->phid);
     $xform->setTransform($this->transform);
     $xform->setTransformedPHID($xformed_file->getPHID());
     $xform->save();
     return $this->buildTransformedFileResponse($xform);
 }