Пример #1
0
 /**
  * Find the given folder or create it as a database record
  *
  * @param string $folderPath Directory path relative to assets root
  * @return Folder|null
  */
 public static function find_or_make($folderPath)
 {
     // replace leading and trailing slashes
     $folderPath = preg_replace('/^\\/?(.*)\\/?$/', '$1', trim($folderPath));
     $parts = explode("/", $folderPath);
     $parentID = 0;
     $item = null;
     $filter = FileNameFilter::create();
     foreach ($parts as $part) {
         if (!$part) {
             continue;
             // happens for paths with a trailing slash
         }
         // Ensure search includes folders with illegal characters removed, but
         // err in favour of matching existing folders if $folderPath
         // includes illegal characters itself.
         $partSafe = $filter->filter($part);
         $item = Folder::get()->filter(array('ParentID' => $parentID, 'Name' => array($partSafe, $part)))->first();
         if (!$item) {
             $item = new Folder();
             $item->ParentID = $parentID;
             $item->Name = $partSafe;
             $item->Title = $part;
             $item->write();
         }
         $parentID = $item->ID;
     }
     return $item;
 }
 public function testDuplicateDashesRemoved()
 {
     $name = 'test--document.txt';
     $filter = new FileNameFilter();
     $this->assertEquals('test-document.txt', $filter->filter($name));
 }
 /**
  * Check if file exists, both checking filtered filename and exact filename
  *
  * @param string $originalFile Filename
  * @return bool
  */
 protected function checkFileExists($originalFile)
 {
     // Check both original and safely filtered filename
     $nameFilter = FileNameFilter::create();
     $filteredFile = $nameFilter->filter($originalFile);
     // Resolve expected folder name
     $folderName = $this->getFolderName();
     $folder = Folder::find_or_make($folderName);
     $parentPath = $folder ? $folder->getFilename() : '';
     // check if either file exists
     return File::find($parentPath . $originalFile) || File::find($parentPath . $filteredFile);
 }
Пример #4
0
 /**
  * Given a temporary file and upload path, validate the file and determine the
  * value of the 'Filename' tuple that should be used to store this asset.
  *
  * @param array $tmpFile
  * @param string $folderPath
  * @return string|false Value of filename tuple, or false if invalid
  */
 protected function getValidFilename($tmpFile, $folderPath = null)
 {
     if (!is_array($tmpFile)) {
         throw new InvalidArgumentException("Upload::load() Not passed an array.  Most likely, the form hasn't got the right enctype");
     }
     // Validate
     $this->clearErrors();
     $valid = $this->validate($tmpFile);
     if (!$valid) {
         return false;
     }
     // Clean filename
     if (!$folderPath) {
         $folderPath = $this->config()->uploads_folder;
     }
     $nameFilter = FileNameFilter::create();
     $file = $nameFilter->filter($tmpFile['name']);
     $filename = basename($file);
     if ($folderPath) {
         $filename = File::join_paths($folderPath, $filename);
     }
     return $filename;
 }