/** * Override the standard UploadField constructor so we can set a few things. * @param string $name * @param string $title * @param SS_List $items */ public function __construct($name, $title = null, SS_List $items = null) { parent::__construct($name, $title, $items); // Remove the parent's JS hook. $this->removeExtraClass('ss-upload'); // Add our own JS hook. $this->addExtraClass('s3-upload'); // Set a bigger initial limit for our uploads. Otherwise this will // default to what's the Max File Size limit in our php.ini $this->getValidator()->setAllowedMaxFileSize(File::ini2bytes('2G')); }
/** * Construct a new ChunkedUploadField instance * * @param string $name The internal field name, passed to forms. * @param string $title The field label. * @param SS_List $items If no items are defined, the field will try to auto-detect an existing relation on * @link $record}, with the same name as the field name. * @param Form $form Reference to the container form */ public function __construct($name, $title = null, SS_List $items = null) { parent::__construct($name, $title); if ($items) { $this->setItems($items); } // set max chunk size $maxUpload = File::ini2bytes(ini_get('upload_max_filesize')); $maxPost = File::ini2bytes(ini_get('post_max_size')); $this->setConfig('maxChunkSize', round(min($maxUpload, $maxPost) * 0.9)); // ~90%, allow some overhead }
/** * @param string $name The internal field name, passed to forms. * @param string $title The field label. * @param SS_List $items If no items are defined, the field will try to auto-detect an existing relation on {@link $record}, * with the same name as the field name. * @param Form $form Reference to the container form */ public function __construct($name, $title = null, SS_List $items = null) { // TODO thats the first thing that came to my head, feel free to change it $this->addExtraClass('ss-upload'); // class, used by js $this->addExtraClass('ss-uploadfield'); // class, used by css for uploadfield only parent::__construct($name, $title); if ($items) { $this->setItems($items); } $this->getValidator()->setAllowedExtensions(array_filter(File::$allowed_extensions)); // filter out '' since this would be a regex problem on JS end $this->getValidator()->setAllowedMaxFileSize(min(File::ini2bytes(ini_get('upload_max_filesize')), File::ini2bytes(ini_get('post_max_size')))); // get the lower max size }
/** * Construct a new UploadField instance * * @param string $name The internal field name, passed to forms. * @param string $title The field label. * @param Form $form Reference to the container form */ public function __construct($name, $title = null) { $this->addExtraClass('ss-upload'); // class, used by js $this->addExtraClass('ss-uploadfield'); // class, used by css for uploadfield only $this->ufConfig = array_merge($this->ufConfig, self::config()->defaultConfig); parent::__construct($name, $title); // AssetField always uses rename replacement method $this->getUpload()->setReplaceFile(false); // filter out '' since this would be a regex problem on JS end $this->getValidator()->setAllowedExtensions(array_filter(Config::inst()->get('File', 'allowed_extensions'))); // get the lower max size $maxUpload = File::ini2bytes(ini_get('upload_max_filesize')); $maxPost = File::ini2bytes(ini_get('post_max_size')); $this->getValidator()->setAllowedMaxFileSize(min($maxUpload, $maxPost)); }
/** * Construct the upload form. * * @param SS_HTTPRequest $request * @return Form */ public function getUploadSnapshotForm(SS_HTTPRequest $request) { // Performs canView permission check by limiting visible projects $project = $this->getCurrentProject(); if (!$project) { return $this->project404Response(); } if (!$project->canUploadArchive()) { return new SS_HTTPResponse("Not allowed to upload", 401); } // Framing an environment as a "group of people with download access" // makes more sense to the user here, while still allowing us to enforce // environment specific restrictions on downloading the file later on. $envs = $project->DNEnvironmentList()->filterByCallback(function ($item) { return $item->canUploadArchive(); }); $envsMap = array(); foreach ($envs as $env) { $envsMap[$env->ID] = $env->Name; } $maxSize = min(File::ini2bytes(ini_get('upload_max_filesize')), File::ini2bytes(ini_get('post_max_size'))); $fileField = DataArchiveFileField::create('ArchiveFile', 'File'); $fileField->getValidator()->setAllowedExtensions(array('sspak')); $fileField->getValidator()->setAllowedMaxFileSize(array('*' => $maxSize)); $form = Form::create($this, 'UploadSnapshotForm', FieldList::create($fileField, DropdownField::create('Mode', 'What does this file contain?', DNDataArchive::get_mode_map()), DropdownField::create('EnvironmentID', 'Initial ownership of the file', $envsMap)->setEmptyString('Select an environment')), FieldList::create(FormAction::create('doUploadSnapshot', 'Upload File')->addExtraClass('btn')), RequiredFields::create('ArchiveFile')); $form->disableSecurityToken(); $form->addExtraClass('fields-wide'); // Tweak the action so it plays well with our fake URL structure. $form->setFormAction($project->Link() . '/UploadSnapshotForm'); return $form; }
/** * Construct a new UploadField instance * * @param string $name The internal field name, passed to forms. * @param string $title The field label. * @param SS_List $items If no items are defined, the field will try to auto-detect an existing relation on * @link $record}, with the same name as the field name. * @param Form $form Reference to the container form */ public function __construct($name, $title = null, SS_List $items = null) { // TODO thats the first thing that came to my head, feel free to change it $this->addExtraClass('ss-upload'); // class, used by js $this->addExtraClass('ss-uploadfield'); // class, used by css for uploadfield only $this->ufConfig = array_merge($this->ufConfig, self::config()->defaultConfig); parent::__construct($name, $title); if ($items) { $this->setItems($items); } // filter out '' since this would be a regex problem on JS end $this->getValidator()->setAllowedExtensions(array_filter(Config::inst()->get('File', 'allowed_extensions'))); // get the lower max size $maxUpload = File::ini2bytes(ini_get('upload_max_filesize')); $maxPost = File::ini2bytes(ini_get('post_max_size')); $this->getValidator()->setAllowedMaxFileSize(min($maxUpload, $maxPost)); }
/** * @param SS_HTTPRequest $req * @return HTMLText */ public function download(SS_HTTPRequest $req) { // find the download link $hash = $req->param('Hash'); if (empty($hash)) { $this->httpError(400); } // bad request $link = DownloadLink::get_by_hash($hash); if (!$link || !$link->exists()) { $this->httpError(403); } // access denied // check that the order exists and is valid $order = $link->Order(); if (!$order || !$order->exists() || !$order->DownloadsAvailable()) { $this->httpError(403); } // check the the file still exists $file = $link->File(); if (!$file || !$file->exists()) { $this->httpError(404); } // if the file is under the "small file" tipping point, just pass it through $smallSize = File::ini2bytes(Config::inst()->get('Downloadable', 'small_file_size')); $fileSize = $file->getAbsoluteSize(); if ($fileSize < $smallSize) { $this->addToLog($order->ID, $file); $this->sendFile($file); } else { return $this->initiateOfflineProcessing(array($file), $order->ID); } }
/** * Looks at the php.ini and takes the lower of two values, translates it into * an int representing the number of bytes allowed per upload * * @return int */ public static function get_filesize_from_ini() { $bytes = min(array(File::ini2bytes(ini_get('post_max_size') ?: '8M'), File::ini2bytes(ini_get('upload_max_filesize') ?: '2M'))); return floor($bytes / (1000 * 1000)); }
/** * Set filesize maximums (in bytes or INI format). * Automatically converts extensions to lowercase * for easier matching. * * Example: * <code> * array('*' => 200, 'jpg' => 1000, '[doc]' => '5m') * </code> * * @param array|int $rules */ public function setAllowedMaxFileSize($rules) { if (is_array($rules) && count($rules)) { // make sure all extensions are lowercase $rules = array_change_key_case($rules, CASE_LOWER); $finalRules = array(); foreach ($rules as $rule => $value) { if (is_numeric($value)) { $tmpSize = $value; } else { $tmpSize = File::ini2bytes($value); } $finalRules[$rule] = (int) $tmpSize; } $this->allowedMaxFileSize = $finalRules; } elseif (is_string($rules)) { $this->allowedMaxFileSize['*'] = File::ini2bytes($rules); } elseif ((int) $rules > 0) { $this->allowedMaxFileSize['*'] = (int) $rules; } }