Path: blob/master/src/aphront/response/AphrontFileResponse.php
12241 views
<?php12final class AphrontFileResponse extends AphrontResponse {34private $content;5private $contentIterator;6private $contentLength;7private $compressResponse;89private $mimeType;10private $download;11private $rangeMin;12private $rangeMax;13private $allowOrigins = array();1415public function addAllowOrigin($origin) {16$this->allowOrigins[] = $origin;17return $this;18}1920public function setDownload($download) {21if ($download === null || !strlen($download)) {22$download = 'untitled';23}24$this->download = $download;25return $this;26}2728public function getDownload() {29return $this->download;30}3132public function setMimeType($mime_type) {33$this->mimeType = $mime_type;34return $this;35}3637public function getMimeType() {38return $this->mimeType;39}4041public function setContent($content) {42$this->setContentLength(strlen($content));43$this->content = $content;44return $this;45}4647public function setContentIterator($iterator) {48$this->contentIterator = $iterator;49return $this;50}5152public function buildResponseString() {53return $this->content;54}5556public function getContentIterator() {57if ($this->contentIterator) {58return $this->contentIterator;59}60return parent::getContentIterator();61}6263public function setContentLength($length) {64$this->contentLength = $length;65return $this;66}6768public function getContentLength() {69return $this->contentLength;70}7172public function setCompressResponse($compress_response) {73$this->compressResponse = $compress_response;74return $this;75}7677public function getCompressResponse() {78return $this->compressResponse;79}8081public function setRange($min, $max) {82$this->rangeMin = $min;83$this->rangeMax = $max;84return $this;85}8687public function getHeaders() {88$headers = array(89array('Content-Type', $this->getMimeType()),90// This tells clients that we can support requests with a "Range" header,91// which allows downloads to be resumed, in some browsers, some of the92// time, if the stars align.93array('Accept-Ranges', 'bytes'),94);9596if ($this->rangeMin !== null || $this->rangeMax !== null) {97$len = $this->getContentLength();98$min = $this->rangeMin;99100$max = $this->rangeMax;101if ($max === null) {102$max = ($len - 1);103}104105$headers[] = array('Content-Range', "bytes {$min}-{$max}/{$len}");106$content_len = ($max - $min) + 1;107} else {108$content_len = $this->getContentLength();109}110111if (!$this->shouldCompressResponse()) {112$headers[] = array('Content-Length', $content_len);113}114115$download = $this->getDownload();116if ($download !== null && strlen($download)) {117$headers[] = array('X-Download-Options', 'noopen');118119$filename = $this->getDownload();120$filename = addcslashes($filename, '"\\');121$headers[] = array(122'Content-Disposition',123'attachment; filename="'.$filename.'"',124);125}126127if ($this->allowOrigins) {128$headers[] = array(129'Access-Control-Allow-Origin',130implode(',', $this->allowOrigins),131);132}133134$headers = array_merge(parent::getHeaders(), $headers);135return $headers;136}137138protected function shouldCompressResponse() {139return $this->getCompressResponse();140}141142public function parseHTTPRange($range) {143$begin = null;144$end = null;145146$matches = null;147if (preg_match('/^bytes=(\d+)-(\d*)$/', $range, $matches)) {148// Note that the "Range" header specifies bytes differently than149// we do internally: the range 0-1 has 2 bytes (byte 0 and byte 1).150$begin = (int)$matches[1];151152// The "Range" may be "200-299" or "200-", meaning "until end of file".153if ($matches[2] !== null && strlen($matches[2])) {154$range_end = (int)$matches[2];155$end = $range_end + 1;156} else {157$range_end = null;158}159160$this->setHTTPResponseCode(206);161$this->setRange($begin, $range_end);162}163164return array($begin, $end);165}166167}168169170