Example #1
0
 /**
  * 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);
 }