Exemplo n.º 1
0
 /**
  * Calculate the DiffMatchPatch patch between two Phabricator files.
  *
  * @phutil-external-symbol class diff_match_patch
  */
 public static function calculatePatch(PhabricatorFile $old = null, PhabricatorFile $new = null)
 {
     $root = dirname(phutil_get_library_root('phabricator'));
     require_once $root . '/externals/diff_match_patch/diff_match_patch.php';
     $old_hash = self::EMPTY_HASH;
     $new_hash = self::EMPTY_HASH;
     if ($old !== null) {
         $old_hash = $old->getContentHash();
     }
     if ($new !== null) {
         $new_hash = $new->getContentHash();
     }
     $old_content = '';
     $new_content = '';
     if ($old_hash === $new_hash) {
         return null;
     }
     if ($old_hash !== self::EMPTY_HASH) {
         $old_content = $old->loadFileData();
     } else {
         $old_content = '';
     }
     if ($new_hash !== self::EMPTY_HASH) {
         $new_content = $new->loadFileData();
     } else {
         $new_content = '';
     }
     $dmp = new diff_match_patch();
     $dmp_patches = $dmp->patch_make($old_content, $new_content);
     return $dmp->patch_toText($dmp_patches);
 }
 /**
  * Very crudely scale an image up or down to an exact size.
  */
 private function crudelyScaleTo(PhabricatorFile $file, $dx, $dy)
 {
     $data = $file->loadFileData();
     $src = imagecreatefromstring($data);
     $dst = $this->applyScaleTo($src, $dx, $dy);
     return $this->saveImageDataInAnyFormat($dst, $file->getMimeType());
 }
 private function applyMemeToFile(PhabricatorFile $file, $upper_text, $lower_text)
 {
     $data = $file->loadFileData();
     $img_type = $file->getMimeType();
     $imagemagick = PhabricatorEnv::getEnvConfig('files.enable-imagemagick');
     if ($img_type != 'image/gif' || $imagemagick == false) {
         return $this->applyMemeTo($data, $upper_text, $lower_text, $img_type);
     }
     $data = $file->loadFileData();
     $input = new TempFile();
     Filesystem::writeFile($input, $data);
     list($out) = execx('convert %s info:', $input);
     $split = phutil_split_lines($out);
     if (count($split) > 1) {
         return $this->applyMemeWithImagemagick($input, $upper_text, $lower_text, count($split), $img_type);
     } else {
         return $this->applyMemeTo($data, $upper_text, $lower_text, $img_type);
     }
 }
 private function buildSourceCodeView(PhabricatorPaste $paste, PhabricatorFile $file)
 {
     $language = $paste->getLanguage();
     $source = $file->loadFileData();
     if (empty($language)) {
         $source = PhabricatorSyntaxHighlighter::highlightWithFilename($paste->getTitle(), $source);
     } else {
         $source = PhabricatorSyntaxHighlighter::highlightWithLanguage($language, $source);
     }
     $lines = explode("\n", $source);
     return id(new PhabricatorSourceCodeView())->setLines($lines);
 }
 private function generatePreview(PhabricatorFile $file, $size)
 {
     $data = $file->loadFileData();
     $src = imagecreatefromstring($data);
     $x = imagesx($src);
     $y = imagesy($src);
     $scale = min($size / $x, $size / $y, 1);
     $dx = max($size / 4, $scale * $x);
     $dy = max($size / 4, $scale * $y);
     $dst = imagecreatetruecolor($dx, $dy);
     imagesavealpha($dst, true);
     imagefill($dst, 0, 0, imagecolorallocatealpha($dst, 255, 255, 255, 127));
     $sdx = $scale * $x;
     $sdy = $scale * $y;
     imagecopyresampled($dst, $src, ($dx - $sdx) / 2, ($dy - $sdy) / 2, 0, 0, $sdx, $sdy, $x, $y);
     return $this->saveImageDataInAnyFormat($dst, $file->getMimeType());
 }
 public function getFileDataIterator(PhabricatorFile $file, $begin, $end)
 {
     // The default implementation is trivial and just loads the entire file
     // upfront.
     $data = $file->loadFileData();
     if ($begin !== null && $end !== null) {
         $data = substr($data, $begin, $end - $begin);
     } else {
         if ($begin !== null) {
             $data = substr($data, $begin);
         } else {
             if ($end !== null) {
                 $data = substr($data, 0, $end);
             }
         }
     }
     return array($data);
 }
 private function applyScaleWithImagemagick(PhabricatorFile $file, $dx, $dy)
 {
     $img_type = $file->getMimeType();
     $imagemagick = PhabricatorEnv::getEnvConfig('files.enable-imagemagick');
     if ($img_type != 'image/gif' || $imagemagick == false) {
         return null;
     }
     $data = $file->loadFileData();
     $src = imagecreatefromstring($data);
     $x = imagesx($src);
     $y = imagesy($src);
     if (self::isEnormousGIF($x, $y)) {
         return null;
     }
     $scale = min($dx / $x, $dy / $y, 1);
     $sdx = $scale * $x;
     $sdy = $scale * $y;
     $input = new TempFile();
     Filesystem::writeFile($input, $data);
     $resized = new TempFile();
     $future = new ExecFuture('convert %s -coalesce -resize %sX%s%s %s', $input, $sdx, $sdy, '!', $resized);
     // Don't spend more than 10 seconds resizing; just fail if it takes longer
     // than that.
     $future->setTimeout(10)->resolvex();
     return Filesystem::readFile($resized);
 }
 public function executeProfileTransform(PhabricatorFile $file, $x, $min_y, $max_y)
 {
     $data = $file->loadFileData();
     $image = $this->crudelyCropTo($data, $x, $min_y, $max_y);
     return PhabricatorFile::newFromFileData($image, array('name' => 'profile-' . $file->getName()));
 }