@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Simplify build file from data-or-hash code

Summary:
We have a bit more copy-paste than we need, consolidate a bit.

(Also switch Mercurial to download git diffs, which it handles well; we use them in "arc patch".)

Test Plan:
- Downloaded a raw diff from Differential.
- Downloaded a raw change from Diffusion.
- Downloaded a raw file from Diffusion.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Differential Revision: https://secure.phabricator.com/D2942

+68 -68
+19 -32
src/applications/differential/controller/DifferentialRevisionViewController.php
··· 940 940 $vcs = $repository ? $repository->getVersionControlSystem() : null; 941 941 switch ($vcs) { 942 942 case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT: 943 + case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL: 943 944 $raw_diff = $bundle->toGitPatch(); 944 945 break; 945 946 case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN: 946 - case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL: 947 947 default: 948 948 $raw_diff = $bundle->toUnifiedDiff(); 949 949 break; 950 950 } 951 951 952 - $hash = PhabricatorHash::digest($raw_diff); 952 + $request_uri = $this->getRequest()->getRequestURI(); 953 953 954 - $file = id(new PhabricatorFile())->loadOneWhere( 955 - 'contentHash = %s LIMIT 1', 956 - $hash); 957 - 958 - if (!$file) { 959 - $request_uri = $this->getRequest()->getRequestURI(); 960 - 961 - // this ends up being something like 962 - // D123.diff 963 - // or the verbose 964 - // D123.vs123.id123.whitespaceignore-all.diff 965 - // lame but nice to include these options 966 - $file_name = ltrim($request_uri->getPath(), '/') . '.'; 967 - foreach ($request_uri->getQueryParams() as $key => $value) { 968 - if ($key == 'download') { 969 - continue; 970 - } 971 - $file_name .= $key . $value . '.'; 954 + // this ends up being something like 955 + // D123.diff 956 + // or the verbose 957 + // D123.vs123.id123.whitespaceignore-all.diff 958 + // lame but nice to include these options 959 + $file_name = ltrim($request_uri->getPath(), '/').'.'; 960 + foreach ($request_uri->getQueryParams() as $key => $value) { 961 + if ($key == 'download') { 962 + continue; 972 963 } 973 - $file_name .= 'diff'; 964 + $file_name .= $key.$value.'.'; 965 + } 966 + $file_name .= 'diff'; 974 967 975 - // We're just caching the data; this is always safe. 976 - $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); 977 - 978 - $file = PhabricatorFile::newFromFileData( 979 - $raw_diff, 980 - array( 981 - 'name' => $file_name, 982 - )); 983 - 984 - unset($unguarded); 985 - } 968 + $file = PhabricatorFile::buildFromFileDataOrHash( 969 + $raw_diff, 970 + array( 971 + 'name' => $file_name, 972 + )); 986 973 987 974 return id(new AphrontRedirectResponse())->setURI($file->getBestURI()); 988 975
+5 -19
src/applications/diffusion/controller/DiffusionBrowseFileController.php
··· 639 639 } 640 640 641 641 private function loadFileForData($path, $data) { 642 - $hash = PhabricatorHash::digest($data); 643 - 644 - $file = id(new PhabricatorFile())->loadOneWhere( 645 - 'contentHash = %s LIMIT 1', 646 - $hash); 647 - if (!$file) { 648 - // We're just caching the data; this is always safe. 649 - $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); 650 - 651 - $file = PhabricatorFile::newFromFileData( 652 - $data, 653 - array( 654 - 'name' => basename($path), 655 - )); 656 - 657 - unset($unguarded); 658 - } 659 - 660 - return $file; 642 + return PhabricatorFile::buildFromFileDataOrHash( 643 + $data, 644 + array( 645 + 'name' => basename($path), 646 + )); 661 647 } 662 648 663 649 private function buildRawResponse($path, $data) {
+5 -17
src/applications/diffusion/controller/DiffusionCommitController.php
··· 872 872 $raw_query = DiffusionRawDiffQuery::newFromDiffusionRequest($drequest); 873 873 $raw_diff = $raw_query->loadRawDiff(); 874 874 875 - $hash = PhabricatorHash::digest($raw_diff); 876 - 877 - $file = id(new PhabricatorFile())->loadOneWhere( 878 - 'contentHash = %s LIMIT 1', 879 - $hash); 880 - if (!$file) { 881 - // We're just caching the data; this is always safe. 882 - $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); 883 - 884 - $file = PhabricatorFile::newFromFileData( 885 - $raw_diff, 886 - array( 887 - 'name' => $drequest->getCommit().'.diff', 888 - )); 889 - 890 - unset($unguarded); 891 - } 875 + $file = PhabricatorFile::buildFromFileDataOrHash( 876 + $raw_diff, 877 + array( 878 + 'name' => $drequest->getCommit().'.diff', 879 + )); 892 880 893 881 return id(new AphrontRedirectResponse())->setURI($file->getBestURI()); 894 882 }
+39
src/applications/files/storage/PhabricatorFile.php
··· 101 101 } 102 102 } 103 103 104 + 105 + /** 106 + * Given a block of data, try to load an existing file with the same content 107 + * if one exists. If it does not, build a new file. 108 + * 109 + * This method is generally used when we have some piece of semi-trusted data 110 + * like a diff or a file from a repository that we want to show to the user. 111 + * We can't just dump it out because it may be dangerous for any number of 112 + * reasons; instead, we need to serve it through the File abstraction so it 113 + * ends up on the CDN domain if one is configured and so on. However, if we 114 + * simply wrote a new file every time we'd potentially end up with a lot 115 + * of redundant data in file storage. 116 + * 117 + * To solve these problems, we use file storage as a cache and reuse the 118 + * same file again if we've previously written it. 119 + * 120 + * NOTE: This method unguards writes. 121 + * 122 + * @param string Raw file data. 123 + * @param dict Dictionary of file information. 124 + */ 125 + public static function buildFromFileDataOrHash( 126 + $data, 127 + array $params = array()) { 128 + 129 + $file = id(new PhabricatorFile())->loadOneWhere( 130 + 'contentHash = %s LIMIT 1', 131 + PhabricatorHash::digest($data)); 132 + 133 + if (!$file) { 134 + $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); 135 + $file = PhabricatorFile::newFromFileData($data, $params); 136 + unset($unguarded); 137 + } 138 + 139 + return $file; 140 + } 141 + 142 + 104 143 public static function newFromFileData($data, array $params = array()) { 105 144 $selector = PhabricatorEnv::newObjectFromConfig('storage.engine-selector'); 106 145