Changeset View
Standalone View
src/aphront/response/AphrontFileResponse.php
<?php | <?php | ||||
final class AphrontFileResponse extends AphrontResponse { | final class AphrontFileResponse extends AphrontResponse { | ||||
private $content; | private $content; | ||||
private $contentIterator; | private $contentIterator; | ||||
private $contentLength; | private $contentLength; | ||||
private $compressResponse; | private $compressResponse; | ||||
private $mimeType; | private $mimeType; | ||||
/** | |||||
* Download filename | |||||
* | |||||
* This is NULL as default or a string. | |||||
* | |||||
* @var string|null | |||||
*/ | |||||
private $download; | private $download; | ||||
private $rangeMin; | private $rangeMin; | ||||
private $rangeMax; | private $rangeMax; | ||||
private $allowOrigins = array(); | private $allowOrigins = array(); | ||||
public function addAllowOrigin($origin) { | public function addAllowOrigin($origin) { | ||||
$this->allowOrigins[] = $origin; | $this->allowOrigins[] = $origin; | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | |||||
* Set a download filename | |||||
avivey: From the code, this doesn't "activate" the download, only provides a name. | |||||
Done Inline ActionsThanks. Replaced with "Set a download filename". valerio.bozzolan: Thanks. Replaced with "Set a download filename". | |||||
* | |||||
* @param $download string | |||||
* @return self | |||||
*/ | |||||
public function setDownload($download) { | public function setDownload($download) { | ||||
// Make sure we have a populated string | |||||
if (!phutil_nonempty_string($download)) { | if (!phutil_nonempty_string($download)) { | ||||
Done Inline ActionsIn short, this was too rigid here ↑ I don't know why Evan wanted to explode when receiving a boolean, but that's the current potential situation, and that's too risky, since I've found some setDownload(true) around. valerio.bozzolan: In short, this was too rigid here ↑
I don't know why Evan wanted to explode when receiving a… | |||||
Done Inline ActionsNote that this now will raise useful warnings when setting to false or true, since both values have really no sense here. The only accepted value now are:
valerio.bozzolan: Note that this now will raise useful warnings when setting to false or true, since both values… | |||||
$download = 'untitled'; | $download = 'untitled'; | ||||
Done Inline Actions2 spaces here. avivey: 2 spaces here.
Not sure why `arc lint` doesn't catch this one. | |||||
} | } | ||||
$this->download = $download; | $this->download = $download; | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | |||||
Done Inline ActionsIn short, this ↑ was the previous situation, and now it's somehow documented and supported again valerio.bozzolan: In short, this ↑ was the previous situation, and now it's somehow documented and supported again | |||||
Done Inline ActionsFor clarifications see the original version: rP5952b0a31b6a: Stop mangling filenames when downloading them valerio.bozzolan: For clarifications see the original version:
{rP5952b0a31b6aac0718bc23aefe43560b9bfe8cc5} | |||||
* Get the download filename | |||||
* | |||||
* If this was never set, NULL is given. | |||||
* | |||||
* @return string|null | |||||
*/ | |||||
public function getDownload() { | public function getDownload() { | ||||
Done Inline ActionsIs there any case where this method is called and the result is not assumed to be a string? avivey: Is there any case where this method is called and the result is not assumed to be a string? | |||||
Done Inline ActionsProbably fixed: now the getter is as-is, but it's safe to assume that it always return NULL, or a string. valerio.bozzolan: Probably fixed: now the getter is as-is, but it's safe to assume that it always return NULL, or… | |||||
return $this->download; | return $this->download; | ||||
} | } | ||||
public function setMimeType($mime_type) { | public function setMimeType($mime_type) { | ||||
$this->mimeType = $mime_type; | $this->mimeType = $mime_type; | ||||
return $this; | return $this; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | public function getHeaders() { | ||||
} else { | } else { | ||||
$content_len = $this->getContentLength(); | $content_len = $this->getContentLength(); | ||||
} | } | ||||
if (!$this->shouldCompressResponse()) { | if (!$this->shouldCompressResponse()) { | ||||
$headers[] = array('Content-Length', $content_len); | $headers[] = array('Content-Length', $content_len); | ||||
} | } | ||||
if (strlen($this->getDownload())) { | if (phutil_nonempty_string($this->getDownload())) { | ||||
Done Inline ActionsThis was probably problematic in PHP 8.1 in the default NULL case valerio.bozzolan: This was probably problematic in PHP 8.1 in the default NULL case | |||||
Done Inline Actions↑ In short, if getDownload() is falsy, that is ignored. If getDownload() is truly, it must be a string, and should be non-empty, so let's require a string and report alien values. valerio.bozzolan: ↑ In short, if `getDownload()` is falsy, that is ignored.
If `getDownload()` is truly, it must… | |||||
Done Inline ActionsThe method phutil_nonempty_string() is still useful since it literally checks whenever that string is non-empty and non-NULL, and reporting alien values. This is much more readable than before with strlen() and effectively fixes PHP 8.1 issues with the default value (NULL). valerio.bozzolan: The method `phutil_nonempty_string()` is still useful since it literally checks whenever that… | |||||
$headers[] = array('X-Download-Options', 'noopen'); | $headers[] = array('X-Download-Options', 'noopen'); | ||||
$filename = $this->getDownload(); | $filename = $this->getDownload(); | ||||
$filename = addcslashes($filename, '"\\'); | $filename = addcslashes($filename, '"\\'); | ||||
$headers[] = array( | $headers[] = array( | ||||
'Content-Disposition', | 'Content-Disposition', | ||||
'attachment; filename="'.$filename.'"', | 'attachment; filename="'.$filename.'"', | ||||
); | ); | ||||
▲ Show 20 Lines • Show All 43 Lines • Show Last 20 Lines |
From the code, this doesn't "activate" the download, only provides a name.