2013-01-20 03:43:35 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorMacroMemeController
|
|
|
|
extends PhabricatorMacroController {
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$macro_name = $request->getStr('macro');
|
|
|
|
$upper_text = $request->getStr('uppertext');
|
|
|
|
$lower_text = $request->getStr('lowertext');
|
|
|
|
$user = $request->getUser();
|
2013-06-08 00:08:34 +02:00
|
|
|
|
|
|
|
$uri = PhabricatorMacroMemeController::generateMacro($user, $macro_name,
|
|
|
|
$upper_text, $lower_text);
|
|
|
|
if ($uri === false) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
return id(new AphrontRedirectResponse())->setURI($uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function generateMacro($user, $macro_name, $upper_text,
|
|
|
|
$lower_text) {
|
2013-03-22 21:07:20 +01:00
|
|
|
$macro = id(new PhabricatorMacroQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->withNames(array($macro_name))
|
|
|
|
->executeOne();
|
2013-01-20 03:43:35 +01:00
|
|
|
if (!$macro) {
|
2013-06-08 00:08:34 +02:00
|
|
|
return false;
|
2013-01-20 03:43:35 +01:00
|
|
|
}
|
2013-03-22 21:07:20 +01:00
|
|
|
$file = $macro->getFile();
|
2013-01-20 03:43:35 +01:00
|
|
|
|
|
|
|
$upper_text = strtoupper($upper_text);
|
|
|
|
$lower_text = strtoupper($lower_text);
|
2013-01-25 02:26:57 +01:00
|
|
|
$mixed_text = md5($upper_text).":".md5($lower_text);
|
2013-01-20 03:43:35 +01:00
|
|
|
$hash = "meme".hash("sha256", $mixed_text);
|
|
|
|
$xform = id(new PhabricatorTransformedFile())
|
|
|
|
->loadOneWhere('originalphid=%s and transform=%s',
|
|
|
|
$file->getPHID(), $hash);
|
|
|
|
|
|
|
|
if ($xform) {
|
|
|
|
$memefile = id(new PhabricatorFile())->loadOneWhere(
|
|
|
|
'phid = %s', $xform->getTransformedPHID());
|
2013-06-08 00:08:34 +02:00
|
|
|
return $memefile->getBestURI();
|
2013-01-20 03:43:35 +01:00
|
|
|
}
|
|
|
|
$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();
|
2013-06-08 00:08:34 +02:00
|
|
|
|
|
|
|
return $newfile->getBestURI();
|
2013-01-20 03:43:35 +01:00
|
|
|
}
|
|
|
|
}
|