public function testDeleteDir()
 {
     $this->adapter->write('nested/dir/path.txt', 'contents', new Config());
     $this->assertTrue(is_dir(__DIR__ . '/files/nested/dir'));
     $this->adapter->deleteDir('nested');
     $this->assertFalse($this->adapter->has('nested/dir/path.txt'));
     $this->assertFalse(is_dir(__DIR__ . '/files/nested/dir'));
 }
Example #2
0
 public function upload()
 {
     $this->logger->info("======================");
     $this->logger->info("upload() Method called");
     $file = new \Upload\File($this->getFileName(), $this->source);
     $file->addValidations($this->getValidations());
     $this->logger->info("Discovered file :" . $file->getNameWithExtension() . " tmpName:" . $file->getRealPath());
     try {
         $tmpName = $file->getRealPath();
         $originalName = $file->getNameWithExtension();
         $hash = md5_file($tmpName);
         $this->logger->info("Created hash for file:" . $hash);
         $this->logger->info("Checking if file with hash = {$hash} exists in db.");
         $fileExistsInDb = $this->fileExistsInDb($hash);
         if ($fileExistsInDb !== false) {
             $this->logger->info("Found " . json_encode($fileExistsInDb));
             $this->logger->info("File already exists");
             return $this->setResult(["success" => false, "message" => "Same File", "id" => abs($fileExistsInDb["id"]), "error" => 4]);
         }
         $this->logger->info("File is unique, file will be validated before saving to db.");
         if (!$file->validate()) {
             $this->logger->info("File is invalid. " . json_encode($file->getErrors()));
             return $this->setResult(["success" => false, "id" => -1, "message" => "Invalid file type.\nMimeType: " . $file->getMimeType(), "additionalInfo" => implode($file->getErrors()), "error" => 3]);
         }
         $this->logger->info("File passed validation. Saving to db.");
         $stmnt = $this->db->prepare("Insert into upload (hash) values (:hash) returning id");
         $stmnt->bindParam(':hash', $hash);
         $stmnt->execute();
         $saveResult = $stmnt->fetch()['id'];
         $this->logger->info("Successfully saved to db with id {$saveResult}");
         if (!$saveResult) {
             $this->logger->info("Failure to save file to db.");
             return $this->setResult(["success" => false, "id" => -1, "message" => "Error in data.", "error" => 7]);
         }
         $uploadBaseDir = $this->container["uploadConfig"]["path"];
         $adapter = new Local($uploadBaseDir, 0);
         $fileParentDir = (string) round($saveResult, -4);
         if (!$adapter->has($fileParentDir)) {
             $config = new Config(['visibility' => 'public']);
             $adapter->createDir($fileParentDir, $config);
         }
         $file = new \Upload\File($this->getFileName(), new FileSystem($uploadBaseDir . $fileParentDir));
         $file->addValidations($this->getValidations());
         $file->setName($saveResult);
         $newFullPath = $uploadBaseDir . $fileParentDir . DS . $file->getNameWithExtension();
         $extension = $file->getExtension();
         $this->logger->info("File will be uploaded to {$newFullPath}");
         $result = $file->upload();
         if (!$result) {
             return $this->setResult(["success" => false, "id" => -1, "message" => "Error uploading file.", "error" => 8]);
         }
         $this->logger->info("File upload successful.");
         $cvlizerConfig = $this->container["cvlizer"];
         $this->logger->info("Reading data of \n {$newFullPath}");
         $fileContents = file_get_contents($newFullPath);
         $soapClient = new \SoapClient($cvlizerConfig["uri"]);
         $this->logger->info("Parsing data with arguments : extractToXML(" . $cvlizerConfig["username"] . "," . $cvlizerConfig["password"] . ", EN, " . $cvlizerConfig["model"] . ", ..., " . $extension . ")");
         $parsedData = $soapClient->extractToXML($cvlizerConfig["username"], $cvlizerConfig["password"], "EN", $cvlizerConfig["model"], $fileContents, $extension);
         if (is_soap_fault($parsedData)) {
             $message = "Error parsing file.\n" . $parsedData->faultcode . "\n" . $parsedData->faultstring;
             $this->logger->info($message);
             return $this->setResult(["success" => false, "id" => -1, "message" => $message, "error" => 9]);
         }
         $this->logger->info("Parse successful.");
         $xmlFile = fopen($cvlizerConfig["xmlSavePath"] . abs($saveResult) . ".xml", "w") or die("Unable to open file!");
         $this->logger->info("Writing to file. {$xmlFile}");
         fwrite($xmlFile, $parsedData);
         fclose($xmlFile);
         $this->logger->info("Write successful.");
         $updateStatement = $this->db->update(["name" => $originalName, "ftype" => $extension, "created" => date("Y-m-d H:i:s")])->table("upload")->where("id", "=", $saveResult);
         $affectedRows = $updateStatement->execute();
         $this->logger->info("Executing sql:\n" . $updateStatement->__toString());
         if ($affectedRows) {
             $this->logger->info("Successfully executed");
             $cv = new CV($parsedData);
             $cv->setId($saveResult);
             $saved = $cv->saveToDb();
             $this->logger->info(json_encode($saved));
             if ($saved) {
                 $this->setResult(["success" => true, "id" => $saveResult, "message" => "New", "error" => 0]);
             }
         } else {
             $message = "Error saving data.";
             $this->logger->info($message);
             return $this->setResult(["success" => false, "id" => 10, "message" => $message]);
         }
     } catch (\Exception $e) {
         $this->logger->info($e->getMessage());
         $this->logger->info($e->__toString());
         return $this->setResult(["success" => false, "id" => -1, "message" => "Error processing file. " . $e->getMessage(), "error" => 10]);
     }
 }