Path: blob/master/src/applications/files/controller/PhabricatorFileDropUploadController.php
12242 views
<?php12final class PhabricatorFileDropUploadController3extends PhabricatorFileController {45public function shouldAllowRestrictedParameter($parameter_name) {6// Prevent false positives from file content when it is submitted via7// drag-and-drop upload.8return true;9}1011/**12* @phutil-external-symbol class PhabricatorStartup13*/14public function handleRequest(AphrontRequest $request) {15$viewer = $request->getViewer();1617// NOTE: Throws if valid CSRF token is not present in the request.18$request->validateCSRF();1920$name = $request->getStr('name');21$file_phid = $request->getStr('phid');22// If there's no explicit view policy, make it very restrictive by default.23// This is the correct policy for files dropped onto objects during24// creation, comment and edit flows.25$view_policy = $request->getStr('viewPolicy');26if (!$view_policy) {27$view_policy = $viewer->getPHID();28}2930$is_chunks = $request->getBool('querychunks');31if ($is_chunks) {32$params = array(33'filePHID' => $file_phid,34);3536$result = id(new ConduitCall('file.querychunks', $params))37->setUser($viewer)38->execute();3940return id(new AphrontAjaxResponse())->setContent($result);41}4243$is_allocate = $request->getBool('allocate');44if ($is_allocate) {45$params = array(46'name' => $name,47'contentLength' => $request->getInt('length'),48'viewPolicy' => $view_policy,49);5051$result = id(new ConduitCall('file.allocate', $params))52->setUser($viewer)53->execute();5455$file_phid = $result['filePHID'];56if ($file_phid) {57$file = $this->loadFile($file_phid);58$result += $file->getDragAndDropDictionary();59}6061return id(new AphrontAjaxResponse())->setContent($result);62}6364// Read the raw request data. We're either doing a chunk upload or a65// vanilla upload, so we need it.66$data = PhabricatorStartup::getRawInput();6768$is_chunk_upload = $request->getBool('uploadchunk');69if ($is_chunk_upload) {70$params = array(71'filePHID' => $file_phid,72'byteStart' => $request->getInt('byteStart'),73'data' => $data,74);7576$result = id(new ConduitCall('file.uploadchunk', $params))77->setUser($viewer)78->execute();7980$file = $this->loadFile($file_phid);81if ($file->getIsPartial()) {82$result = array();83} else {84$result = array(85'complete' => true,86) + $file->getDragAndDropDictionary();87}8889return id(new AphrontAjaxResponse())->setContent($result);90}9192$file = PhabricatorFile::newFromXHRUpload(93$data,94array(95'name' => $request->getStr('name'),96'authorPHID' => $viewer->getPHID(),97'viewPolicy' => $view_policy,98'isExplicitUpload' => true,99));100101$result = $file->getDragAndDropDictionary();102return id(new AphrontAjaxResponse())->setContent($result);103}104105private function loadFile($file_phid) {106$viewer = $this->getViewer();107108$file = id(new PhabricatorFileQuery())109->setViewer($viewer)110->withPHIDs(array($file_phid))111->executeOne();112if (!$file) {113throw new Exception(pht('Failed to load file.'));114}115116return $file;117}118119}120121122