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

Compress file downloads if the client sends "Accept-Encoding: gzip" and we guess the file might compress alright

Summary:
Ref T13507. We currently compress normal responses, but do not compress file data responses because most files we serve are images and already compressed.

However, there are some cases where large files may be highly compressible (e.g., huge XML files stored in LFS) and we can benefit from compressing responses.

Make a reasonable guess about whether compression is beneficial and enable compression if we guess it is.

Test Plan:
- Used `curl ...` to download an image with `Accept-Encoding: gzip`. Got raw image data in the response (as expected, because we don't expect images to be worthwhile to recompress).
- Used `curl ...` to download a text file with `Accept-Encoding: gzip`. Got a compressed response. Decompressed the response into the original file.

Maniphest Tasks: T13507

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

+51
+51
src/applications/files/controller/PhabricatorFileDataController.php
··· 142 142 (string)$request_uri); 143 143 } 144 144 145 + if ($this->shouldCompressFileDataResponse($file)) { 146 + $response->setCompressResponse(true); 147 + } 148 + 145 149 return $response; 146 150 } 147 151 ··· 222 226 throw new PhutilInvalidStateException('loadFile'); 223 227 } 224 228 return $this->file; 229 + } 230 + 231 + private function shouldCompressFileDataResponse(PhabricatorFile $file) { 232 + // If the client sends "Accept-Encoding: gzip", we have the option of 233 + // compressing the response. 234 + 235 + // We generally expect this to be a good idea if the file compresses well, 236 + // but maybe not such a great idea if the file is already compressed (like 237 + // an image or video) or compresses poorly: the CPU cost of compressing and 238 + // decompressing the stream may exceed the bandwidth savings during 239 + // transfer. 240 + 241 + // Ideally, we'd probably make this decision by compressing files when 242 + // they are uploaded, storing the compressed size, and then doing a test 243 + // here using the compression savings and estimated transfer speed. 244 + 245 + // For now, just guess that we shouldn't compress images or videos or 246 + // files that look like they are already compressed, and should compress 247 + // everything else. 248 + 249 + if ($file->isViewableImage()) { 250 + return false; 251 + } 252 + 253 + if ($file->isAudio()) { 254 + return false; 255 + } 256 + 257 + if ($file->isVideo()) { 258 + return false; 259 + } 260 + 261 + $compressed_types = array( 262 + 'application/x-gzip', 263 + 'application/x-compress', 264 + 'application/x-compressed', 265 + 'application/x-zip-compressed', 266 + 'application/zip', 267 + ); 268 + $compressed_types = array_fuse($compressed_types); 269 + 270 + $mime_type = $file->getMimeType(); 271 + if (isset($compressed_types[$mime_type])) { 272 + return false; 273 + } 274 + 275 + return true; 225 276 } 226 277 227 278 }