/** * 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()); } }
/** * Handles POST actions. * * @return \DreamFactory\Core\Utility\ServiceResponse * @throws BadRequestException * @throws InternalServerErrorException * @throws \Exception */ protected function handlePOST() { if (empty($this->filePath)) { // create folders and files // possible file handling parameters $extract = $this->request->getParameterAsBool('extract', false); $clean = $this->request->getParameterAsBool('clean', false); $checkExist = $this->request->getParameterAsBool('check_exist', false); $fileNameHeader = $this->request->getHeader('X-File-Name'); $folderNameHeader = $this->request->getHeader('X-Folder-Name'); $fileUrl = filter_var($this->request->getParameter('url', ''), FILTER_SANITIZE_URL); if (!empty($fileNameHeader)) { // html5 single posting for file create $result = $this->handleFileContent($this->folderPath, $fileNameHeader, $this->request->getContent(), $this->request->getContentType(), $extract, $clean, $checkExist); } elseif (!empty($folderNameHeader)) { // html5 single posting for folder create $fullPathName = $this->folderPath . $folderNameHeader; $content = $this->request->getPayloadData(); $this->driver->createFolder($this->container, $fullPathName, $content); $result = ['name' => $folderNameHeader, 'path' => $fullPathName]; } elseif (!empty($fileUrl)) { // upload a file from a url, could be expandable zip $tmpName = null; try { $tmpName = FileUtilities::importUrlFileToTemp($fileUrl); $result = $this->handleFile($this->folderPath, '', $tmpName, '', $extract, $clean, $checkExist); @unlink($tmpName); } catch (\Exception $ex) { if (!empty($tmpName)) { @unlink($tmpName); } throw $ex; } } elseif (null !== ($uploadedFiles = $this->request->getFile('files'))) { // older html multi-part/form-data post, single or multiple files $files = FileUtilities::rearrangePostedFiles($uploadedFiles); $result = $this->handleFolderContentFromFiles($files, $extract, $clean, $checkExist); $result = ResourcesWrapper::cleanResources($result); } else { // possibly xml or json post either of files or folders to create, copy or move if (!empty($data = ResourcesWrapper::unwrapResources($this->getPayloadData()))) { $result = $this->handleFolderContentFromData($data, $extract, $clean, $checkExist); $result = ResourcesWrapper::cleanResources($result); } else { // create folder from resource path $this->driver->createFolder($this->container, $this->folderPath); $result = ['name' => basename($this->folderPath), 'path' => $this->folderPath]; } } } else { // create the file // possible file handling parameters $extract = $this->request->getParameterAsBool('extract', false); $clean = $this->request->getParameterAsBool('clean', false); $checkExist = $this->request->getParameterAsBool('check_exist', false); $name = basename($this->filePath); $path = dirname($this->filePath); $files = $this->request->getFile('files'); if (empty($files)) { // direct load from posted data as content // or possibly xml or json post of file properties create, copy or move $result = $this->handleFileContent($path, $name, $this->request->getContent(), $this->request->getContentType(), $extract, $clean, $checkExist); } else { // older html multipart/form-data post, should be single file $files = FileUtilities::rearrangePostedFiles($files); if (1 < count($files)) { throw new BadRequestException("Multiple files uploaded to a single REST resource '{$name}'."); } $file = ArrayUtils::get($files, 0); if (empty($file)) { throw new BadRequestException("No file uploaded to REST resource '{$name}'."); } $error = $file['error']; if (UPLOAD_ERR_OK == $error) { $result = $this->handleFile($path, $name, $file["tmp_name"], $file['type'], $extract, $clean, $checkExist); } else { throw new InternalServerErrorException("Failed to upload file {$name}.\n{$error}"); } } } return ResponseFactory::create($result, $this->nativeFormat, ServiceResponseInterface::HTTP_CREATED); }
/** * Verifies file import from url. * * @param $url * * @throws \DreamFactory\Core\Exceptions\BadRequestException * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException */ protected function verifyImportFromUrl($url) { $extension = strtolower(pathinfo($url, PATHINFO_EXTENSION)); if (static::FILE_EXTENSION != $extension) { throw new BadRequestException("Only package files ending with '" . static::FILE_EXTENSION . "' are allowed for import."); } try { // need to download and extract zip file and move contents to storage $file = FileUtilities::importUrlFileToTemp($url); } catch (\Exception $ex) { throw new InternalServerErrorException("Failed to import package {$url}.\n{$ex->getMessage()}"); } $this->setZipFile($file); }