private static function buildFromFileData($data, array $params = array())
 {
     if (isset($params['storageEngines'])) {
         $engines = $params['storageEngines'];
     } else {
         $size = strlen($data);
         $engines = PhabricatorFileStorageEngine::loadStorageEngines($size);
         if (!$engines) {
             throw new Exception(pht('No configured storage engine can store this file. See ' . '"Configuring File Storage" in the documentation for ' . 'information on configuring storage engines.'));
         }
     }
     assert_instances_of($engines, 'PhabricatorFileStorageEngine');
     if (!$engines) {
         throw new Exception(pht('No valid storage engines are available!'));
     }
     $file = self::initializeNewFile();
     $aes_type = PhabricatorFileAES256StorageFormat::FORMATKEY;
     $has_aes = PhabricatorKeyring::getDefaultKeyName($aes_type);
     if ($has_aes !== null) {
         $default_key = PhabricatorFileAES256StorageFormat::FORMATKEY;
     } else {
         $default_key = PhabricatorFileRawStorageFormat::FORMATKEY;
     }
     $key = idx($params, 'format', $default_key);
     // Callers can pass in an object explicitly instead of a key. This is
     // primarily useful for unit tests.
     if ($key instanceof PhabricatorFileStorageFormat) {
         $format = clone $key;
     } else {
         $format = clone PhabricatorFileStorageFormat::requireFormat($key);
     }
     $format->setFile($file);
     $properties = $format->newStorageProperties();
     $file->setStorageFormat($format->getStorageFormatKey());
     $file->setStorageProperties($properties);
     $data_handle = null;
     $engine_identifier = null;
     $exceptions = array();
     foreach ($engines as $engine) {
         $engine_class = get_class($engine);
         try {
             list($engine_identifier, $data_handle) = $file->writeToEngine($engine, $data, $params);
             // We stored the file somewhere so stop trying to write it to other
             // places.
             break;
         } catch (PhabricatorFileStorageConfigurationException $ex) {
             // If an engine is outright misconfigured (or misimplemented), raise
             // that immediately since it probably needs attention.
             throw $ex;
         } catch (Exception $ex) {
             phlog($ex);
             // If an engine doesn't work, keep trying all the other valid engines
             // in case something else works.
             $exceptions[$engine_class] = $ex;
         }
     }
     if (!$data_handle) {
         throw new PhutilAggregateException(pht('All storage engines failed to write file:'), $exceptions);
     }
     $file->setByteSize(strlen($data));
     $file->setContentHash(self::hashFileContent($data));
     $file->setStorageEngine($engine_identifier);
     $file->setStorageHandle($data_handle);
     $file->readPropertiesFromParameters($params);
     if (!$file->getMimeType()) {
         $tmp = new TempFile();
         Filesystem::writeFile($tmp, $data);
         $file->setMimeType(Filesystem::getMimeType($tmp));
     }
     try {
         $file->updateDimensions(false);
     } catch (Exception $ex) {
         // Do nothing
     }
     $file->save();
     return $file;
 }
 private function getMasterKeyName()
 {
     if ($this->keyName !== null) {
         return $this->keyName;
     }
     $default = PhabricatorKeyring::getDefaultKeyName(self::FORMATKEY);
     if ($default !== null) {
         return $default;
     }
     throw new Exception(pht('No AES256 key is specified in the keyring as a default encryption ' . 'key, and no encryption key has been explicitly selected.'));
 }