Page MenuHomestyx hydra

No OneTemporary

diff --git a/src/applications/macro/controller/PhabricatorMacroMemeController.php b/src/applications/macro/controller/PhabricatorMacroMemeController.php
index 03b13f6205..794e24e36d 100644
--- a/src/applications/macro/controller/PhabricatorMacroMemeController.php
+++ b/src/applications/macro/controller/PhabricatorMacroMemeController.php
@@ -1,68 +1,116 @@
<?php
final class PhabricatorMacroMemeController
extends PhabricatorMacroController {
public function shouldAllowPublic() {
return true;
}
public function handleRequest(AphrontRequest $request) {
$macro_name = $request->getStr('macro');
$upper_text = $request->getStr('uppertext');
$lower_text = $request->getStr('lowertext');
$viewer = $request->getViewer();
- $uri = self::generateMacro($viewer, $macro_name,
- $upper_text, $lower_text);
- if ($uri === false) {
- return new Aphront404Response();
- }
- return id(new AphrontRedirectResponse())
- ->setIsExternal(true)
- ->setURI($uri);
+ $uri = self::generateMacro(
+ $viewer,
+ $macro_name,
+ $upper_text,
+ $lower_text);
+
+ $content = array(
+ 'imageURI' => $uri,
+ );
+
+ return id(new AphrontAjaxResponse())->setContent($content);
}
- public static function generateMacro($viewer, $macro_name, $upper_text,
- $lower_text) {
+ public static function generateMacro(
+ PhabricatorUser $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();
+ $upper_text = phutil_utf8_strtoupper($upper_text);
+ $lower_text = phutil_utf8_strtoupper($lower_text);
+
+ $hash = PhabricatorHash::digestForIndex(
+ phutil_json_encode(
+ array(
+ 'kind' => 'meme',
+ 'upper' => $upper_text,
+ 'lower' => $lower_text,
+ )));
+
+ $xfile = self::loadTransformedFile($viewer, $file->getPHID(), $hash);
+ if ($xfile) {
+ return $xfile->getViewURI();
+ }
+
+ $transformer = new PhabricatorImageTransformer();
+
+ $new_file = $transformer->executeMemeTransform(
+ $file,
+ $upper_text,
+ $lower_text);
+
+ $xfile = id(new PhabricatorTransformedFile())
+ ->setOriginalPHID($file->getPHID())
+ ->setTransformedPHID($new_file->getPHID())
+ ->setTransform($hash);
+
+ try {
+ $caught = null;
+
+ $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
+ try {
+ $xfile->save();
+ } catch (Exception $ex) {
+ $caught = $ex;
+ }
+ unset($unguarded);
+
+ if ($caught) {
+ throw $caught;
}
+
+ return $new_file->getViewURI();
+ } catch (AphrontDuplicateKeyQueryException $ex) {
+ $xfile = self::loadTransformedFile($viewer, $file->getPHID(), $hash);
+ if (!$xfile) {
+ throw $ex;
+ }
+ return $xfile->getViewURI();
}
+ }
- $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();
+ private static function loadTransformedFile(
+ PhabricatorUser $viewer,
+ $file_phid,
+ $hash) {
+
+ $xform = id(new PhabricatorTransformedFile())->loadOneWhere(
+ 'originalPHID = %s AND transform = %s',
+ $file_phid,
+ $hash);
+ if (!$xform) {
+ return null;
+ }
+
+ return id(new PhabricatorFileQuery())
+ ->setViewer($viewer)
+ ->withPHIDs(array($xform->getTransformedPHID()))
+ ->executeOne();
}
}
diff --git a/src/applications/macro/markup/PhabricatorMemeRemarkupRule.php b/src/applications/macro/markup/PhabricatorMemeRemarkupRule.php
index 004e415450..278bd403cb 100644
--- a/src/applications/macro/markup/PhabricatorMemeRemarkupRule.php
+++ b/src/applications/macro/markup/PhabricatorMemeRemarkupRule.php
@@ -1,65 +1,62 @@
<?php
final class PhabricatorMemeRemarkupRule extends PhutilRemarkupRule {
private $images;
public function getPriority() {
return 200.0;
}
public function apply($text) {
return preg_replace_callback(
'@{meme,((?:[^}\\\\]+|\\\\.)+)}@m',
array($this, 'markupMeme'),
$text);
}
public function markupMeme(array $matches) {
if (!$this->isFlatText($matches[0])) {
return $matches[0];
}
$options = array(
'src' => null,
'above' => null,
'below' => null,
);
$parser = new PhutilSimpleOptions();
$options = $parser->parse($matches[1]) + $options;
$uri = id(new PhutilURI('/macro/meme/'))
->alter('macro', $options['src'])
->alter('uppertext', $options['above'])
->alter('lowertext', $options['below']);
if ($this->getEngine()->isHTMLMailMode()) {
$uri = PhabricatorEnv::getProductionURI($uri);
}
if ($this->getEngine()->isTextMode()) {
$img =
($options['above'] != '' ? "\"{$options['above']}\"\n" : '').
$options['src'].' <'.PhabricatorEnv::getProductionURI($uri).'>'.
($options['below'] != '' ? "\n\"{$options['below']}\"" : '');
} else {
$alt_text = pht(
'Macro %s: %s %s',
$options['src'],
$options['above'],
$options['below']);
- $img = $this->newTag(
- 'img',
- array(
- 'src' => $uri,
- 'alt' => $alt_text,
- 'class' => 'phabricator-remarkup-macro',
- ));
+ $img = id(new PHUIRemarkupImageView())
+ ->setURI($uri)
+ ->addClass('phabricator-remarkup-macro')
+ ->setAlt($alt_text);
}
return $this->getEngine()->storeText($img);
}
}
diff --git a/src/infrastructure/markup/view/PHUIRemarkupImageView.php b/src/infrastructure/markup/view/PHUIRemarkupImageView.php
index 0a73efb83b..0402f000a4 100644
--- a/src/infrastructure/markup/view/PHUIRemarkupImageView.php
+++ b/src/infrastructure/markup/view/PHUIRemarkupImageView.php
@@ -1,67 +1,79 @@
<?php
final class PHUIRemarkupImageView
extends AphrontView {
private $uri;
private $width;
private $height;
private $alt;
+ private $classes = array();
public function setURI($uri) {
$this->uri = $uri;
return $this;
}
public function getURI() {
return $this->uri;
}
public function setWidth($width) {
$this->width = $width;
return $this;
}
public function getWidth() {
return $this->width;
}
public function setHeight($height) {
$this->height = $height;
return $this;
}
public function getHeight() {
return $this->height;
}
public function setAlt($alt) {
$this->alt = $alt;
return $this;
}
public function getAlt() {
return $this->alt;
}
+ public function addClass($class) {
+ $this->classes[] = $class;
+ return $this;
+ }
+
public function render() {
$id = celerity_generate_unique_node_id();
Javelin::initBehavior(
'remarkup-load-image',
array(
'uri' => (string)$this->uri,
'imageID' => $id,
));
+ $classes = null;
+ if ($this->classes) {
+ $classes = implode(' ', $this->classes);
+ }
+
return phutil_tag(
'img',
array(
'id' => $id,
'width' => $this->getWidth(),
'height' => $this->getHeight(),
'alt' => $this->getAlt(),
+ 'class' => $classes,
));
}
}

File Metadata

Mime Type
text/x-diff
Expires
Wed, Jul 2, 5:44 PM (1 d, 1 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
165064
Default Alt Text
(8 KB)

Event Timeline