/** * Checks if subject is transferable * * @param mixed $check Path to file in local FS, URL or file-upload array * @return boolean */ function resource($check) { if (TransferValidation::fileUpload($check) || TransferValidation::uploadedFile($check) || MediaValidation::file($check) || TransferValidation::url($check)) { return !TransferValidation::blank($check); } return false; }
public function testBlank() { $upload = array(); $result = TransferValidation::blank($upload); $this->assertTrue($result); $upload = array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0); $result = TransferValidation::blank($upload); $this->assertTrue($result); $upload = ''; $result = TransferValidation::blank($upload); $this->assertTrue($result); $upload = array('name' => 'file.name', 'type' => 'mime/type', 'tmp_name' => 'efhf42983q7ghdsui', 'error' => '', 'size' => 1234); $result = TransferValidation::blank($upload); $this->assertFalse($result); $upload = 'Lorem ipsum'; $result = TransferValidation::blank($upload); $this->assertFalse($result); }
/** * Triggers `prepare()` and performs transfer * * If transfer is unsuccessful save operation will abort. * * @param Model $Model * @return boolean */ function beforeSave(&$Model) { if (!isset($Model->data[$Model->alias]['file'])) { return true; } $file = $Model->data[$Model->alias]['file']; if (TransferValidation::blank($file)) { unset($Model->data[$Model->alias]['file']); return true; } if (!($file = $this->transfer($Model, $file))) { return false; } $Model->data[$Model->alias]['file'] = $file; return $Model->data[$Model->alias]; }
/** * Triggered by `beforeValidate`, `beforeSave` or upon user request * * Prepares runtime for being used by `perform()` * * @param Model $Model * @param string $file Optionally provide a valid transfer resource to be used as source * @return mixed true if transfer is ready to be performed, false on error, null if no data was found */ function prepare(&$Model, $file = null) { if (isset($Model->data[$Model->alias]['file'])) { $file = $Model->data[$Model->alias]['file']; } if (empty($file)) { return null; } if ($this->runtime[$Model->alias]['hasPerformed']) { $this->reset($Model); } if ($this->runtime[$Model->alias]['isReady']) { return true; } /* Extraction must happen after reset */ extract($this->settings[$Model->alias], EXTR_SKIP); extract($this->runtime[$Model->alias], EXTR_SKIP); if (TransferValidation::blank($file)) { /* Set explicitly null enabling allowEmpty in rules act upon emptiness */ return $Model->data[$Model->alias]['file'] = null; } if ($source = $this->_source($Model, $file)) { $this->runtime[$Model->alias]['source'] = $source; } else { return false; } /* Temporary is optional and can fail */ if ($source['type'] !== 'file-local') { $temporary = $this->runtime[$Model->alias]['temporary'] = $this->_temporary($Model, $file); } $this->_addMarker($Model, 'DS', DS); $this->_addMarker($Model, 'uuid', String::uuid()); $this->_addMarker($Model, 'unixTimestamp', time()); $this->_addMarker($Model, 'year', date('Y')); $this->_addMarker($Model, 'month', date('m')); $this->_addMarker($Model, 'day', date('d')); $filename = $this->_addMarker($Model, 'Source.filename', $source['filename'], true); $extension = $this->_addMarker($Model, 'Source.extension', $source['extension'], true); $this->_addMarker($Model, 'Source.basename', empty($extension) ? $filename : $filename . '.' . $extension); $this->_addMarker($Model, 'Source.mimeType', $source['mimeType'], true); $this->_addMarker($Model, 'Source.type', $source['type']); $this->_addMarker($Model, 'Medium.name', strtolower(Medium::name($source['file'], $source['mimeType']))); $this->_addMarker($Model, 'Medium.short', Medium::short($source['file'], $source['mimeType'])); /* Needed for tableless Models */ if (isset($Model->data[$Model->alias])) { $this->_addMarker($Model, $Model->alias . '.', $Model->data[$Model->alias], true); $this->_addMarker($Model, 'Model.', $Model->data[$Model->alias], true); } $this->_addMarker($Model, 'Model.name', $Model->name); $this->_addMarker($Model, 'Model.alias', $Model->alias); if (!($destinationFile = $this->_replaceMarker($Model, $destinationFile))) { return false; } if ($destination = $this->_destination($Model, $baseDirectory . $destinationFile)) { $this->runtime[$Model->alias]['destination'] = $destination; } else { return false; } if ($source == $destination || $temporary == $destination) { return false; } $Folder = new Folder($destination['dirname'], $createDirectory); if (!$Folder->pwd()) { $message = "TransferBehavior::prepare - Directory `{$destination['dirname']}` could "; $message .= "not be created or is not writable. Please check the permissions."; trigger_error($message, E_USER_WARNING); return false; } return $this->runtime[$Model->alias]['isReady'] = true; }