@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.

Consolidate handling of special properties for newly uploaded files

Summary:
Fixes T5849. When a new file is created, we might have to actually write the data to a storage engine, or we might be able to just point at data which is already there.

Currently, these two paths handle `$params` with different code and mild behavioral differences. Instead, have them call the same code so they get the same behavior.

Test Plan:
- Uploaded the same file multiple times to home page.
- Uploaded the same file multiple times as profile picture.
- Generated files via Diffusion.
- All the files got the expected properties, whether they were reusing data or not.

Reviewers: btrahan, 20after4

Reviewed By: 20after4

Subscribers: epriestley

Maniphest Tasks: T5849

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

+67 -37
+67 -37
src/applications/files/storage/PhabricatorFile.php
··· 1 1 <?php 2 2 3 + /** 4 + * Parameters 5 + * ========== 6 + * 7 + * When creating a new file using a method like @{method:newFromFileData}, these 8 + * parameters are supported: 9 + * 10 + * | name | Human readable filename. 11 + * | authorPHID | User PHID of uploader. 12 + * | ttl | Temporary file lifetime, in seconds. 13 + * | viewPolicy | File visibility policy. 14 + * | isExplicitUpload | Used to show users files they explicitly uploaded. 15 + * | canCDN | Allows the file to be cached and delivered over a CDN. 16 + * | mime-type | Optional, explicit file MIME type. 17 + * 18 + */ 3 19 final class PhabricatorFile extends PhabricatorFileDAO 4 20 implements 5 21 PhabricatorTokenReceiverInterface, ··· 159 175 return $file; 160 176 } 161 177 162 - public static function newFileFromContentHash($hash, $params) { 178 + public static function newFileFromContentHash($hash, array $params) { 163 179 // Check to see if a file with same contentHash exist 164 180 $file = id(new PhabricatorFile())->loadOneWhere( 165 - 'contentHash = %s LIMIT 1', $hash); 181 + 'contentHash = %s LIMIT 1', 182 + $hash); 166 183 167 184 if ($file) { 168 185 // copy storageEngine, storageHandle, storageFormat ··· 171 188 $copy_of_storage_format = $file->getStorageFormat(); 172 189 $copy_of_byteSize = $file->getByteSize(); 173 190 $copy_of_mimeType = $file->getMimeType(); 174 - 175 - $file_name = idx($params, 'name'); 176 - $file_name = self::normalizeFileName($file_name); 177 - $file_ttl = idx($params, 'ttl'); 178 - $authorPHID = idx($params, 'authorPHID'); 179 191 180 192 $new_file = new PhabricatorFile(); 181 193 182 - $new_file->setName($file_name); 183 194 $new_file->setByteSize($copy_of_byteSize); 184 - $new_file->setAuthorPHID($authorPHID); 185 - $new_file->setTtl($file_ttl); 186 - 187 - if (idx($params, 'viewPolicy')) { 188 - $new_file->setViewPolicy($params['viewPolicy']); 189 - } 190 195 191 196 $new_file->setContentHash($hash); 192 197 $new_file->setStorageEngine($copy_of_storage_engine); ··· 194 199 $new_file->setStorageFormat($copy_of_storage_format); 195 200 $new_file->setMimeType($copy_of_mimeType); 196 201 $new_file->copyDimensions($file); 202 + 203 + $new_file->readPropertiesFromParameters($params); 197 204 198 205 $new_file->save(); 199 206 ··· 253 260 $exceptions); 254 261 } 255 262 256 - $file_name = idx($params, 'name'); 257 - $file_name = self::normalizeFileName($file_name); 258 - $file_ttl = idx($params, 'ttl'); 259 - 260 - // If for whatever reason, authorPHID isn't passed as a param 261 - // (always the case with newFromFileDownload()), store a '' 262 - $authorPHID = idx($params, 'authorPHID'); 263 - 264 - $file->setName($file_name); 265 263 $file->setByteSize(strlen($data)); 266 - $file->setAuthorPHID($authorPHID); 267 - $file->setTtl($file_ttl); 268 264 $file->setContentHash(self::hashFileContent($data)); 269 265 270 - if (idx($params, 'viewPolicy')) { 271 - $file->setViewPolicy($params['viewPolicy']); 272 - } 273 - 274 - if (idx($params, 'canCDN')) { 275 - $file->setCanCDN(true); 276 - } 277 - 278 266 $file->setStorageEngine($engine_identifier); 279 267 $file->setStorageHandle($data_handle); 280 268 281 269 // TODO: This is probably YAGNI, but allows for us to do encryption or 282 270 // compression later if we want. 283 271 $file->setStorageFormat(self::STORAGE_FORMAT_RAW); 284 - $file->setIsExplicitUpload(idx($params, 'isExplicitUpload') ? 1 : 0); 272 + 273 + $file->readPropertiesFromParameters($params); 285 274 286 - if (isset($params['mime-type'])) { 287 - $file->setMimeType($params['mime-type']); 288 - } else { 275 + if (!$file->getMimeType()) { 289 276 $tmp = new TempFile(); 290 277 Filesystem::writeFile($tmp, $data); 291 278 $file->setMimeType(Filesystem::getMimeType($tmp)); ··· 931 918 id(new PhabricatorEdgeEditor()) 932 919 ->addEdge($phid, $edge_type, $this->getPHID()) 933 920 ->save(); 921 + 922 + return $this; 923 + } 924 + 925 + 926 + /** 927 + * Configure a newly created file object according to specified parameters. 928 + * 929 + * This method is called both when creating a file from fresh data, and 930 + * when creating a new file which reuses existing storage. 931 + * 932 + * @param map<string, wild> Bag of parameters, see @{class:PhabricatorFile} 933 + * for documentation. 934 + * @return this 935 + */ 936 + private function readPropertiesFromParameters(array $params) { 937 + $file_name = idx($params, 'name'); 938 + $file_name = self::normalizeFileName($file_name); 939 + $this->setName($file_name); 940 + 941 + $author_phid = idx($params, 'authorPHID'); 942 + $this->setAuthorPHID($author_phid); 943 + 944 + $file_ttl = idx($params, 'ttl'); 945 + $this->setTtl($file_ttl); 946 + 947 + $view_policy = idx($params, 'viewPolicy'); 948 + if ($view_policy) { 949 + $this->setViewPolicy($params['viewPolicy']); 950 + } 951 + 952 + $is_explicit = (idx($params, 'isExplicitUpload') ? 1 : 0); 953 + $this->setIsExplicitUpload($is_explicit); 954 + 955 + $can_cdn = idx($params, 'canCDN'); 956 + if ($can_cdn) { 957 + $this->setCanCDN(true); 958 + } 959 + 960 + $mime_type = idx($params, 'mime-type'); 961 + if ($mime_type) { 962 + $this->setMimeType($mime_type); 963 + } 934 964 935 965 return $this; 936 966 }