Path: blob/master/src/aphront/multipartparser/AphrontMultipartPart.php
12241 views
<?php12final class AphrontMultipartPart extends Phobject {34private $headers = array();5private $value = '';67private $name;8private $filename;9private $tempFile;10private $byteSize = 0;1112public function appendRawHeader($bytes) {13$parser = id(new AphrontHTTPHeaderParser())14->parseRawHeader($bytes);1516$header_name = $parser->getHeaderName();1718$this->headers[] = array(19$header_name,20$parser->getHeaderContent(),21);2223if (strtolower($header_name) === 'content-disposition') {24$pairs = $parser->getHeaderContentAsPairs();25foreach ($pairs as $pair) {26list($key, $value) = $pair;27switch ($key) {28case 'filename':29$this->filename = $value;30break;31case 'name':32$this->name = $value;33break;34}35}36}3738return $this;39}4041public function appendData($bytes) {42$this->byteSize += strlen($bytes);4344if ($this->isVariable()) {45$this->value .= $bytes;46} else {47if (!$this->tempFile) {48$this->tempFile = new TempFile(getmypid().'.upload');49}50Filesystem::appendFile($this->tempFile, $bytes);51}5253return $this;54}5556public function isVariable() {57return ($this->filename === null);58}5960public function getName() {61return $this->name;62}6364public function getVariableValue() {65if (!$this->isVariable()) {66throw new Exception(pht('This part is not a variable!'));67}6869return $this->value;70}7172public function getPHPFileDictionary() {73if (!$this->tempFile) {74$this->appendData('');75}7677$mime_type = 'application/octet-stream';78foreach ($this->headers as $header) {79list($name, $value) = $header;80if (strtolower($name) == 'content-type') {81$mime_type = $value;82break;83}84}8586return array(87'name' => $this->filename,88'type' => $mime_type,89'tmp_name' => (string)$this->tempFile,90'error' => 0,91'size' => $this->byteSize,92);93}9495}969798