Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/controller/PhabricatorFileDataController.php
| Show First 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | if (!$is_download && $file->isPDF()) { | ||||
| ->setFragment(null) | ->setFragment(null) | ||||
| ->removeAllQueryParams(); | ->removeAllQueryParams(); | ||||
| $response->addContentSecurityPolicyURI( | $response->addContentSecurityPolicyURI( | ||||
| 'object-src', | 'object-src', | ||||
| (string)$request_uri); | (string)$request_uri); | ||||
| } | } | ||||
| if ($this->shouldCompressFileDataResponse($file)) { | |||||
| $response->setCompressResponse(true); | |||||
| } | |||||
| return $response; | return $response; | ||||
| } | } | ||||
| private function loadFile() { | private function loadFile() { | ||||
| // Access to files is provided by knowledge of a per-file secret key in | // Access to files is provided by knowledge of a per-file secret key in | ||||
| // the URI. Knowledge of this secret is sufficient to retrieve the file. | // the URI. Knowledge of this secret is sufficient to retrieve the file. | ||||
| // For some requests, we also have a valid viewer. However, for many | // For some requests, we also have a valid viewer. However, for many | ||||
| ▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | final class PhabricatorFileDataController extends PhabricatorFileController { | ||||
| private function getFile() { | private function getFile() { | ||||
| if (!$this->file) { | if (!$this->file) { | ||||
| throw new PhutilInvalidStateException('loadFile'); | throw new PhutilInvalidStateException('loadFile'); | ||||
| } | } | ||||
| return $this->file; | return $this->file; | ||||
| } | } | ||||
| private function shouldCompressFileDataResponse(PhabricatorFile $file) { | |||||
| // If the client sends "Accept-Encoding: gzip", we have the option of | |||||
| // compressing the response. | |||||
| // We generally expect this to be a good idea if the file compresses well, | |||||
| // but maybe not such a great idea if the file is already compressed (like | |||||
| // an image or video) or compresses poorly: the CPU cost of compressing and | |||||
| // decompressing the stream may exceed the bandwidth savings during | |||||
| // transfer. | |||||
| // Ideally, we'd probably make this decision by compressing files when | |||||
| // they are uploaded, storing the compressed size, and then doing a test | |||||
| // here using the compression savings and estimated transfer speed. | |||||
| // For now, just guess that we shouldn't compress images or videos or | |||||
| // files that look like they are already compressed, and should compress | |||||
| // everything else. | |||||
| if ($file->isViewableImage()) { | |||||
| return false; | |||||
| } | |||||
| if ($file->isAudio()) { | |||||
| return false; | |||||
| } | |||||
| if ($file->isVideo()) { | |||||
| return false; | |||||
| } | |||||
| $compressed_types = array( | |||||
| 'application/x-gzip', | |||||
| 'application/x-compress', | |||||
| 'application/x-compressed', | |||||
| 'application/x-zip-compressed', | |||||
| 'application/zip', | |||||
| ); | |||||
| $compressed_types = array_fuse($compressed_types); | |||||
| $mime_type = $file->getMimeType(); | |||||
| if (isset($compressed_types[$mime_type])) { | |||||
| return false; | |||||
| } | |||||
| return true; | |||||
| } | |||||
| } | } | ||||