[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/files/controller/ -> PhabricatorFileUploadController.php (source)

   1  <?php
   2  
   3  final class PhabricatorFileUploadController extends PhabricatorFileController {
   4  
   5    public function processRequest() {
   6      $request = $this->getRequest();
   7      $viewer = $request->getUser();
   8  
   9      $file = PhabricatorFile::initializeNewFile();
  10  
  11      $e_file = true;
  12      $errors = array();
  13      if ($request->isFormPost()) {
  14        $view_policy = $request->getStr('viewPolicy');
  15  
  16        if (!$request->getFileExists('file')) {
  17          $e_file = pht('Required');
  18          $errors[] = pht('You must select a file to upload.');
  19        } else {
  20          $file = PhabricatorFile::newFromPHPUpload(
  21            idx($_FILES, 'file'),
  22            array(
  23              'name'        => $request->getStr('name'),
  24              'authorPHID'  => $viewer->getPHID(),
  25              'viewPolicy'  => $view_policy,
  26              'isExplicitUpload' => true,
  27            ));
  28        }
  29  
  30        if (!$errors) {
  31          return id(new AphrontRedirectResponse())->setURI($file->getInfoURI());
  32        }
  33  
  34        $file->setViewPolicy($view_policy);
  35      }
  36  
  37      $support_id = celerity_generate_unique_node_id();
  38      $instructions = id(new AphrontFormMarkupControl())
  39        ->setControlID($support_id)
  40        ->setControlStyle('display: none')
  41        ->setValue(hsprintf(
  42          '<br /><br /><strong>%s</strong> %s<br /><br />',
  43          pht('Drag and Drop:'),
  44          pht(
  45            'You can also upload files by dragging and dropping them from your '.
  46            'desktop onto this page or the Phabricator home page.')));
  47  
  48      $policies = id(new PhabricatorPolicyQuery())
  49        ->setViewer($viewer)
  50        ->setObject($file)
  51        ->execute();
  52  
  53      $form = id(new AphrontFormView())
  54        ->setUser($viewer)
  55        ->setEncType('multipart/form-data')
  56        ->appendChild(
  57          id(new AphrontFormFileControl())
  58            ->setLabel(pht('File'))
  59            ->setName('file')
  60            ->setError($e_file)
  61            ->setCaption($this->renderUploadLimit()))
  62        ->appendChild(
  63          id(new AphrontFormTextControl())
  64            ->setLabel(pht('Name'))
  65            ->setName('name')
  66            ->setValue($request->getStr('name')))
  67        ->appendChild(
  68          id(new AphrontFormPolicyControl())
  69            ->setUser($viewer)
  70            ->setCapability(PhabricatorPolicyCapability::CAN_VIEW)
  71            ->setPolicyObject($file)
  72            ->setPolicies($policies)
  73            ->setName('viewPolicy'))
  74        ->appendChild(
  75          id(new AphrontFormSubmitControl())
  76            ->setValue(pht('Upload'))
  77            ->addCancelButton('/file/'))
  78        ->appendChild($instructions);
  79  
  80      $crumbs = $this->buildApplicationCrumbs();
  81      $crumbs->addTextCrumb(pht('Upload'), $request->getRequestURI());
  82  
  83      $title = pht('Upload File');
  84  
  85      $global_upload = id(new PhabricatorGlobalUploadTargetView())
  86        ->setUser($viewer)
  87        ->setShowIfSupportedID($support_id);
  88  
  89      $form_box = id(new PHUIObjectBoxView())
  90        ->setHeaderText($title)
  91        ->setFormErrors($errors)
  92        ->setForm($form);
  93  
  94      return $this->buildApplicationPage(
  95        array(
  96          $crumbs,
  97          $form_box,
  98          $global_upload,
  99        ),
 100        array(
 101          'title' => $title,
 102        ));
 103    }
 104  
 105    private function renderUploadLimit() {
 106      $limit = PhabricatorEnv::getEnvConfig('storage.upload-size-limit');
 107      $limit = phutil_parse_bytes($limit);
 108      if ($limit) {
 109        $formatted = phutil_format_bytes($limit);
 110        return 'Maximum file size: '.$formatted;
 111      }
 112  
 113      $doc_href = PhabricatorEnv::getDocLink(
 114        'Configuring File Upload Limits');
 115      $doc_link = phutil_tag(
 116        'a',
 117        array(
 118          'href'    => $doc_href,
 119          'target'  => '_blank',
 120        ),
 121        'Configuring File Upload Limits');
 122  
 123      return hsprintf('Upload limit is not configured, see %s.', $doc_link);
 124    }
 125  
 126  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1