2013-01-19 18:43:35 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
final class PhabricatorMacroMemeController
|
|
|
|
|
extends PhabricatorMacroController {
|
|
|
|
|
|
2014-09-04 12:50:51 -07:00
|
|
|
public function shouldAllowPublic() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-27 09:04:08 -07:00
|
|
|
public function handleRequest(AphrontRequest $request) {
|
2013-01-19 18:43:35 -08:00
|
|
|
$macro_name = $request->getStr('macro');
|
|
|
|
|
$upper_text = $request->getStr('uppertext');
|
|
|
|
|
$lower_text = $request->getStr('lowertext');
|
2015-07-27 09:04:08 -07:00
|
|
|
$viewer = $request->getViewer();
|
2013-06-07 15:08:34 -07:00
|
|
|
|
2015-07-27 09:04:08 -07:00
|
|
|
$uri = self::generateMacro($viewer, $macro_name,
|
2013-06-07 15:08:34 -07:00
|
|
|
$upper_text, $lower_text);
|
|
|
|
|
if ($uri === false) {
|
|
|
|
|
return new Aphront404Response();
|
|
|
|
|
}
|
2014-08-20 06:59:08 -07:00
|
|
|
return id(new AphrontRedirectResponse())
|
|
|
|
|
->setIsExternal(true)
|
|
|
|
|
->setURI($uri);
|
2013-06-07 15:08:34 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-27 09:04:08 -07:00
|
|
|
public static function generateMacro($viewer, $macro_name, $upper_text,
|
2013-06-07 15:08:34 -07:00
|
|
|
$lower_text) {
|
2013-03-22 13:07:20 -07:00
|
|
|
$macro = id(new PhabricatorMacroQuery())
|
2015-07-27 09:04:08 -07:00
|
|
|
->setViewer($viewer)
|
2013-03-22 13:07:20 -07:00
|
|
|
->withNames(array($macro_name))
|
Reduce the cost of loading large numbers of macros
Summary:
Ref T6013. I accidentally made this cost explosviely huge when fixing macros for logged out users in D10411.
Specifically, we'd load all the macros, which would load all the files, which would load all the macros (to do policy checks), which would fill out of cache I think (but maybe only some of the time?). Anyway, bad news.
Instead, only load the files if we need them.
Test Plan: Viewed macro main page, macro detail, used a macro, used a meme, edited a macro, edited audio.
Reviewers: btrahan, csilvers
Reviewed By: csilvers
Subscribers: epriestley, spicyj
Maniphest Tasks: T6013
Differential Revision: https://secure.phabricator.com/D10428
2014-09-05 17:30:26 -07:00
|
|
|
->needFiles(true)
|
2013-03-22 13:07:20 -07:00
|
|
|
->executeOne();
|
2013-01-19 18:43:35 -08:00
|
|
|
if (!$macro) {
|
2013-06-07 15:08:34 -07:00
|
|
|
return false;
|
2013-01-19 18:43:35 -08:00
|
|
|
}
|
2013-03-22 13:07:20 -07:00
|
|
|
$file = $macro->getFile();
|
2013-01-19 18:43:35 -08:00
|
|
|
|
|
|
|
|
$upper_text = strtoupper($upper_text);
|
|
|
|
|
$lower_text = strtoupper($lower_text);
|
2014-06-09 11:36:49 -07:00
|
|
|
$mixed_text = md5($upper_text).':'.md5($lower_text);
|
|
|
|
|
$hash = 'meme'.hash('sha256', $mixed_text);
|
2013-01-19 18:43:35 -08:00
|
|
|
$xform = id(new PhabricatorTransformedFile())
|
|
|
|
|
->loadOneWhere('originalphid=%s and transform=%s',
|
|
|
|
|
$file->getPHID(), $hash);
|
|
|
|
|
|
|
|
|
|
if ($xform) {
|
2013-09-30 09:38:13 -07:00
|
|
|
$memefile = id(new PhabricatorFileQuery())
|
2015-07-27 09:04:08 -07:00
|
|
|
->setViewer($viewer)
|
2013-09-30 09:38:13 -07:00
|
|
|
->withPHIDs(array($xform->getTransformedPHID()))
|
|
|
|
|
->executeOne();
|
|
|
|
|
if ($memefile) {
|
|
|
|
|
return $memefile->getBestURI();
|
|
|
|
|
}
|
2013-01-19 18:43:35 -08:00
|
|
|
}
|
2013-09-30 09:38:13 -07:00
|
|
|
|
2013-01-19 18:43:35 -08: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-07 15:08:34 -07:00
|
|
|
|
|
|
|
|
return $newfile->getBestURI();
|
2013-01-19 18:43:35 -08:00
|
|
|
}
|
|
|
|
|
}
|