public function setup()
 {
     clearstatcache();
     $fs = new Adapter\Local(__DIR__ . '/');
     $fs->deleteDir('files');
     $fs->createDir('files');
 }
 protected function _createFileOnDir(Local $adapter, $outFile)
 {
     $filesystem = new Filesystem($adapter);
     $dir = pathinfo($adapter->getPathPrefix() . $outFile, PATHINFO_DIRNAME) . '/';
     if (!is_dir($dir)) {
         $this->logger->info('Creo directory mancante: ' . $dir);
         mkdir($dir, 0700, true);
     }
     if ($filesystem->has($outFile)) {
         $filesystem->put($outFile, (string) $this->currentFile);
     } else {
         $filesystem->write($outFile, (string) $this->currentFile);
     }
     return $outFile;
     //$io->text('Outfile: '.$outFile);
 }
Exemple #3
0
 /**
  * {@inheritdoc}
  */
 public function deleteDir($dirname)
 {
     $location = $this->applyPathPrefix($dirname);
     if (!is_dir($location) || !is_writable($location)) {
         return false;
     }
     return parent::deleteDir($dirname);
 }
Exemple #4
0
 /**
  * @param SplFileInfo $file
  *
  * @return array
  */
 protected function mapFileInfo(SplFileInfo $file)
 {
     $normalized = parent::mapFileInfo($file);
     if ($normalized['type'] === 'link') {
         $normalized['type'] = 'dir';
     }
     return $normalized;
 }
 public function delete($path)
 {
     $result = parent::delete($path);
     if ($this->shouldRemoveEmptyDir()) {
         $location = $this->applyPathPrefix($path);
         $directory = dirname($location);
         $this->removeDirectoryIfEmpty($directory, true);
     }
     return $result;
 }
 public function __construct($root = null, $writeFlags = LOCK_EX, $linkHandling = self::DISALLOW_LINKS)
 {
     // Get root path
     $root = $this->findRoot($root);
     // Override permissions with config
     $permissions = \Config::inst()->get(get_class($this), 'file_permissions');
     parent::__construct($root, $writeFlags, $linkHandling, $permissions);
     // Configure server
     $this->configureServer();
 }
Exemple #7
0
 /**
  * @param string $rootDir
  * @param int $writeFlags
  * @param int $linkHandling
  * @param array $permissions
  * @throws InstantiationException
  */
 public function __construct($rootDir = null, $writeFlags = LOCK_EX, $linkHandling = parent::DISALLOW_LINKS, $permissions = [])
 {
     static::$permissions = ['file' => ['public' => 0744, 'private' => 0700], 'dir' => ['public' => 0755, 'private' => 0700]];
     try {
         parent::__construct($rootDir, $writeFlags, $linkHandling, $permissions);
     } catch (Error $ex) {
         throw new InstantiationException("AdapterLocal could not be initalized.", $ex);
     } catch (Exception $ex) {
         throw new InstantiationException("AdapterLocal could not be initalized.", $ex);
     }
 }
 /**
  * Get the extensions
  *
  * @param   string $group The extension group
  *
  * @return  ExtensionInterface[]
  */
 public function getExtensions($group = '')
 {
     if (key_exists($group, $this->extensions)) {
         return $this->extensions[$group];
     }
     $this->extensions[$group] = [];
     $fs = $this->rootFolder;
     if (is_string($this->rootFolder)) {
         // It is only the path
         $fs = new Local($this->rootFolder);
     }
     if (!$fs instanceof AbstractAdapter) {
         return [];
     }
     foreach ($fs->listContents($group, true) as $file) {
         if (strpos($file['path'], 'extension.yml') === false) {
             continue;
         }
         $path = $fs->applyPathPrefix($file['path']);
         if (key_exists($path, $this->loadedFiles)) {
             // We have loaded it already
             $this->extensions[$group][] = $this->loadedFiles[$path];
             continue;
         }
         $extension = new Extension();
         $this->loadedFiles[$path] = $extension;
         $this->extensions[$group][] = $extension;
         $config = Yaml::parse($fs->read($file['path'])['contents'], Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
         if (key_exists('listeners', $config)) {
             $this->createListeners($extension, $config['listeners']);
         }
         if (key_exists('queryhandlers', $config)) {
             $this->createQueryHandlers($extension, $config['queryhandlers']);
         }
     }
     return $this->extensions[$group];
 }
 public function __construct($root = null, $writeFlags = LOCK_EX, $linkHandling = self::DISALLOW_LINKS)
 {
     // Get root path
     if (!$root) {
         // Empty root will set the path to assets
         $root = ASSETS_PATH;
     } elseif (strpos($root, './') === 0) {
         // Substitute leading ./ with BASE_PATH
         $root = BASE_PATH . substr($root, 1);
     } elseif (strpos($root, '../') === 0) {
         // Substitute leading ./ with parent of BASE_PATH, in case storage is outside of the webroot.
         $root = dirname(BASE_PATH) . substr($root, 2);
     }
     // Override permissions with config
     $permissions = \Config::inst()->get(get_class($this), 'file_permissions');
     parent::__construct($root, $writeFlags, $linkHandling, $permissions);
 }
 /**
  * Creates a temporary directory
  *
  * @param string $prefix
  * @param null   $dir
  */
 public function __construct($prefix = '', $dir = null)
 {
     $maxTries = 1024;
     if (empty($dir)) {
         $dir = sys_get_temp_dir();
     } else {
         $dir = rtrim($dir, $this->pathSeparator);
     }
     do {
         $path = $dir . $this->pathSeparator . uniqid($prefix, true);
         if (!file_exists($path) && mkdir($path, 0700)) {
             break;
         }
         $maxTries--;
     } while ($maxTries > 0);
     if ($maxTries == 0) {
         throw new \RuntimeException("Couldn't create temporary directory, giving up");
     }
     parent::__construct($path);
 }
Exemple #11
0
 /**
  * Write a file using stream
  *
  * @param string   $path
  * @param resource $resource
  * @param FSConfig $config
  *
  * @return array|bool
  */
 public function writeStream($path, $resource, FSConfig $config)
 {
     $location = $this->applyPathPrefix($path);
     $this->ensureDirectory(dirname($location));
     $result = parent::writeStream($path, $resource, $config);
     $chmodFiles = $this->backendConfig['chmodFiles'];
     $oldUmask = umask(0);
     chmod($location, $chmodFiles);
     umask($oldUmask);
     return $result;
 }
Exemple #12
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]);
     }
 }
 public function __construct($root = null, $writeFlags = LOCK_EX, $linkHandling = self::DISALLOW_LINKS)
 {
     // Override permissions with config
     $permissions = \Config::inst()->get(get_class($this), 'file_permissions');
     parent::__construct($root ?: ASSETS_PATH, $writeFlags, $linkHandling, $permissions);
 }
Exemple #14
0
 /**
  * @inheritdoc
  */
 public function applyPathPrefix($path)
 {
     $prefixedPath = parent::applyPathPrefix($path);
     return rtrim($prefixedPath, DIRECTORY_SEPARATOR);
 }
Exemple #15
0
 protected function createDistributedPathDirs()
 {
     $dirPath = '';
     $this->distributedPathCollection->each(function ($prefix) use(&$dirPath) {
         $dirPath .= $this->pathSeparator . $prefix;
         $fullPath = parent::getPathPrefix() . $dirPath;
         if (!is_dir($fullPath)) {
             @mkdir(parent::getPathPrefix() . $dirPath);
         }
     });
     return $this;
 }
Exemple #16
0
 /**
  * Constructor.
  *
  * @param string $root
  * @param int    $writeFlags
  * @param int    $linkHandling
  * @param array  $permissions
  */
 public function __construct($root, $writeFlags = 0, $linkHandling = self::DISALLOW_LINKS, array $permissions = [])
 {
     parent::__construct($root, $writeFlags, $linkHandling, $permissions);
 }
 /**
  * @param string $root
  * @param string $publicUrlBase
  * @param int $writeFlags
  * @param array|int $linkHandling
  * @param array $permissions
  */
 public function __construct($root, $publicUrlBase = null, $writeFlags = LOCK_EX, $linkHandling = self::DISALLOW_LINKS, array $permissions = [])
 {
     $this->publicUrlBase = $publicUrlBase;
     parent::__construct($root, $writeFlags, $linkHandling, $permissions);
 }
 /**
  * Map file info to normalized key names
  *
  * @param   \SplFileInfo  $file  The original file info class
  * @return  array
  */
 protected function mapFileInfo(SplFileInfo $file)
 {
     $default = parent::mapFileInfo($file);
     $default['owner'] = $file->getOwner();
     return $default;
 }
 public function __construct($root = null, $writeFlags = LOCK_EX, $linkHandling = self::DISALLOW_LINKS)
 {
     parent::__construct($root ?: ASSETS_PATH, $writeFlags, $linkHandling);
 }
 /**
  * {@inheritdoc}
  */
 public function getPathPrefix()
 {
     return $this->wrapPath(parent::getPathPrefix());
 }
Exemple #21
0
 /**
  * @inheritdoc
  */
 protected function ensureDirectory($root)
 {
     return parent::ensureDirectory(Yii::getAlias($root));
 }
Exemple #22
0
 /**
  * Get the normalized path from a SplFileInfo object.
  *
  * @param \SplFileInfo $file
  *
  * @return string
  */
 protected function getFilePath(\SplFileInfo $file)
 {
     $path = parent::getFilePath($file);
     if ($this->pathSeparator === '\\') {
         return str_replace($this->pathSeparator, '/', $path);
     } else {
         return $path;
     }
 }
 /**
  * @expectedException League\Flysystem\NotSupportedException
  */
 public function testLinkCausedUnsupportedException()
 {
     $root = __DIR__ . '/files/';
     $original = $root . 'original.txt';
     $link = $root . 'link.txt';
     file_put_contents($original, 'something');
     symlink($original, $link);
     $adapter = new Local($root);
     $adapter->listContents();
 }