/** * Execute the console command. * * @return mixed */ public function handle() { try { $data = $this->argument('data'); if (filter_var($data, FILTER_VALIDATE_URL)) { // need to download file $data = FileUtilities::importUrlFileToTemp($data); } if (is_file($data)) { $data = file_get_contents($data); } $format = $this->option('format'); $format = DataFormats::toNumeric($format); $this->comment($format); $service = $this->option('service'); $resource = $this->option('resource'); $result = ServiceHandler::handleRequest(Verbs::POST, $service, $resource, [], $data, $format); $this->info('Import complete!'); } catch (\Exception $e) { $this->error($e->getMessage()); } }
/** * @param array $config * * @throws InvalidArgumentException * @throws DfException */ public function __construct($config) { $storageType = strtolower(ArrayUtils::get($config, 'storage_type')); $credentials = $config; $this->container = ArrayUtils::get($config, 'container'); Session::replaceLookups($credentials, true); switch ($storageType) { case 'rackspace cloudfiles': $authUrl = ArrayUtils::get($credentials, 'url', 'https://identity.api.rackspacecloud.com/'); $region = ArrayUtils::get($credentials, 'region', 'DFW'); break; default: $authUrl = ArrayUtils::get($credentials, 'url'); $region = ArrayUtils::get($credentials, 'region'); break; } $username = ArrayUtils::get($credentials, 'username'); $password = ArrayUtils::get($credentials, 'password'); $apiKey = ArrayUtils::get($credentials, 'api_key'); $tenantName = ArrayUtils::get($credentials, 'tenant_name'); if (empty($authUrl)) { throw new InvalidArgumentException('Object Store authentication URL can not be empty.'); } if (empty($username)) { throw new InvalidArgumentException('Object Store username can not be empty.'); } $secret = ['username' => $username]; if (empty($apiKey)) { if (empty($password)) { throw new InvalidArgumentException('Object Store credentials must contain an API key or a password.'); } $secret['password'] = $password; } else { $secret['apiKey'] = $apiKey; } if (!empty($tenantName)) { $secret['tenantName'] = $tenantName; } if (empty($region)) { throw new InvalidArgumentException('Object Store region can not be empty.'); } try { switch ($storageType) { case 'rackspace cloudfiles': $pos = stripos($authUrl, '/v'); if (false !== $pos) { $authUrl = substr($authUrl, 0, $pos); } $authUrl = FileUtilities::fixFolderPath($authUrl) . 'v2.0'; $os = new Rackspace($authUrl, $secret); break; default: $os = new OpenStack($authUrl, $secret); break; } $this->blobConn = $os->ObjectStore('cloudFiles', $region); if (!$this->containerExists($this->container)) { $this->createContainer(['name' => $this->container]); } } catch (\Exception $ex) { throw new DfException('Failed to launch OpenStack service: ' . $ex->getMessage()); } }
/** * List folders and files * * @param string $root root path name * @param string $prefix Optional. search only for folders and files by specified prefix. * @param string $delimiter Optional. Delimiter, i.e. '/', for specifying folder hierarchy * * @return array * @throws \Exception */ public static function listTree($root, $prefix = '', $delimiter = '') { $dir = $root . (!empty($prefix) ? $prefix : ''); $out = []; if (is_dir($dir)) { $files = array_diff(scandir($dir), ['.', '..']); foreach ($files as $file) { $key = $dir . $file; $local = (!empty($prefix) ? $prefix : '') . $file; // get file meta if (is_dir($key)) { $stat = stat($key); $out[] = ['path' => str_replace(DIRECTORY_SEPARATOR, '/', $local) . '/', 'last_modified' => gmdate('D, d M Y H:i:s \\G\\M\\T', ArrayUtils::get($stat, 'mtime', 0))]; if (empty($delimiter)) { $out = array_merge($out, static::listTree($root, $local . DIRECTORY_SEPARATOR)); } } elseif (is_file($key)) { $stat = stat($key); $ext = FileUtilities::getFileExtension($key); $out[] = ['path' => str_replace(DIRECTORY_SEPARATOR, '/', $local), 'content_type' => FileUtilities::determineContentType($ext, '', $key), 'last_modified' => gmdate('D, d M Y H:i:s \\G\\M\\T', ArrayUtils::get($stat, 'mtime', 0)), 'content_length' => ArrayUtils::get($stat, 'size', 0)]; } else { error_log($key); } } } else { throw new NotFoundException("Folder '{$prefix}' does not exist in storage."); } return $out; }
/** * @param array $data Array of sub-folder and file paths that are relative to the root folder * @param string $root root folder from which to delete * @param bool $force * * @return array * @throws \DreamFactory\Core\Exceptions\BadRequestException */ protected function deleteFolderContent($data, $root = '', $force = false) { $root = FileUtilities::fixFolderPath($root); $out = []; if (!empty($data)) { foreach ($data as $key => $resource) { $path = ArrayUtils::get($resource, 'path'); $name = ArrayUtils::get($resource, 'name'); if (!empty($path)) { $fullPath = $path; } else { if (!empty($name)) { $fullPath = $root . '/' . $name; } else { throw new BadRequestException('No path or name provided for resource.'); } } switch (ArrayUtils::get($resource, 'type')) { case 'file': $out[$key] = ['name' => $name, 'path' => $path, 'type' => 'file']; try { $this->driver->deleteFile($this->container, $fullPath); } catch (\Exception $ex) { $out[$key]['error'] = ['message' => $ex->getMessage()]; } break; case 'folder': $out[$key] = ['name' => $name, 'path' => $path, 'type' => 'folder']; try { $this->driver->deleteFolder($this->container, $fullPath, $force); } catch (\Exception $ex) { $out[$key]['error'] = ['message' => $ex->getMessage()]; } break; } } } return $out; }
/** * @param bool|true $includeFiles * @param bool|false $includeData * * @return null * @throws \DreamFactory\Core\Exceptions\NotFoundException * @throws \Exception */ public function exportAppAsPackage($includeFiles = true, $includeData = false) { /** @type App $app */ $app = App::find($this->exportAppId); if (empty($app)) { throw new NotFoundException('App not found in database with app id - ' . $this->exportAppId); } $appName = $app->name; try { $this->initExportZipFile($appName); $this->packageAppDescription($app); $this->packageServices(); $this->packageSchemas(); if ($includeData) { $this->packageData(); } if ($app->type === AppTypes::STORAGE_SERVICE && $includeFiles) { $this->packageAppFiles($app); } $this->zip->close(); FileUtilities::sendFile($this->zipFilePath, true); return null; } catch (\Exception $e) { //Do necessary things here. throw $e; } }
public static function sendFile($file, $download = false) { if (is_file($file)) { $ext = FileUtilities::getFileExtension($file); $disposition = $download ? 'attachment' : 'inline'; header('Last-Modified: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', filemtime($file))); header('Content-Type: ' . FileUtilities::determineContentType($ext, '', $file)); header('Content-Length:' . filesize($file)); header('Content-Disposition: ' . $disposition . '; filename="' . basename($file) . '";'); header('Cache-Control: private'); // use this to open files directly header('Expires: 0'); header('Pragma: public'); readfile($file); } else { Log::debug('FileUtilities::downloadFile is_file call fail: ' . $file); $statusHeader = 'HTTP/1.1 404 The specified file ' . $file . ' does not exist.'; header($statusHeader); header('Content-Type: text/html'); } }
/** * Configures the .env file with app key and database configuration. */ protected function runConfig() { $this->info('**********************************************************************************************************************'); $this->info('* Configuring DreamFactory... '); $this->info('**********************************************************************************************************************'); if (!file_exists('.env')) { copy('.env-dist', '.env'); $this->info('Created .env file with default configuration.'); $this->call('key:generate'); } if (!file_exists('phpunit.xml')) { copy('phpunit.xml-dist', 'phpunit.xml'); $this->info('Created phpunit.xml with default configuration.'); } $db = $this->choice('Which database would you like to use for system tables?', ['sqlite', 'mysql', 'pgsql', 'sqlsrv'], 0); if ('sqlite' === $db) { $this->createSqliteDbFile(); } else { $driver = $db; $host = $this->ask('Enter your ' . $db . ' Host'); $database = $this->ask('Enter your database name'); $username = $this->ask('Enter your database username'); $password = ''; $passwordMatch = false; while (!$passwordMatch) { $password = $this->secret('Enter your database password'); $password2 = $this->secret('Re-enter your database password'); if ($password === $password2) { $passwordMatch = true; } else { $this->error('Passwords did not match. Please try again.'); } } $port = $this->ask('Enter your Database Port', '3306'); $config = ['DB_DRIVER' => $driver, 'DB_HOST' => $host, 'DB_DATABASE' => $database, 'DB_USERNAME' => $username, 'DB_PASSWORD' => $password, 'DB_PORT' => $port]; FileUtilities::updateEnvSetting($config); $this->info('Configured ' . $db . ' Database'); } $this->info('Configuration complete!'); $this->configComplete(); }
/** * @param $container * @param $path * * @return string */ private static function removeContainerFromPath($container, $path) { if (empty($container)) { return $path; } $container = FileUtilities::fixFolderPath($container); return substr($path, strlen($container)); }