Exemplo n.º 1
0
 /**
  * Generate location slug
  *
  * @param $location
  */
 private function generateSlug($location)
 {
     $slug = \CB\slug($location['name']);
     $exists = isset($this->existingSlugs[$slug]);
     if ($exists) {
         $tmp = $slug;
         $i = 2;
         while ($exists) {
             $tmp = $slug . '-' . $i;
             $exists = isset($this->existingSlugs[$tmp]);
             $i++;
         }
         $slug = $tmp;
     }
     $this->existingSlugs[$slug] = true;
     $this->locationSlugs[$location['id']] = $slug;
     $this->execute(sprintf('UPDATE location SET slug = \'%s\' WHERE id = %d;', $slug, $location['id']));
 }
Exemplo n.º 2
0
Arquivo: File.php Projeto: nexcra/cb5
 /**
  * @param $User
  * @param $Location
  * @param $name
  * @param $file
  * @return \CB\Entity\File
  * @throws Exception
  * @throws \Exception
  */
 public function upload($User, $Location, $name, $file)
 {
     // set created date
     $this->_slug = $Location->getSlug();
     $this->_created = $Location->getCreated();
     // check file size
     $fileSize = filesize($file);
     if ($fileSize === 0) {
         throw new \CB\Service\Exception('File "' . $name . '" is empty!');
     }
     if ($fileSize > $this->_stringToBytes(\CB\Config::get('upload.maxfilesize'))) {
         throw new \CB\Service\Exception('File "' . $name . '" is too big (' . $this->_bytesToString($fileSize) . '). Maximum allowed size is ' . \CB\Config::get('upload.maxfilesize') . '"!');
     }
     // get mime type
     if (false === ($mimeType = $this->_getMimeType($file))) {
         throw new \CB\Service\Exception('Unknown file "' . $name . '"!');
     }
     // check mime type
     if (!isset(\CB\Config::get('upload.mimetypes')[$mimeType])) {
         throw new \CB\Service\Exception('Invalid file type "' . $mimeType . '" for file "' . $name . '"!');
     }
     // prepare file variables
     $fileName = \CB\slug(substr($name, 0, strrpos($name, '.')));
     $extension = \CB\Config::get('upload.mimetypes')[$mimeType];
     $name = $fileName . '.' . $extension;
     $dir = $this->_getFileDir();
     $target = $this->_getFilePath($name);
     // make sure not to overwrite existing files ... append _number to fileName
     if (file_exists($target)) {
         $originalName = $fileName;
         $j = 2;
         do {
             $fileName = $originalName . '-' . $j;
             $name = $fileName . '.' . $extension;
             $target = $this->_getFilePath($name);
             $j++;
         } while (file_exists($target));
     }
     // create destination folder
     //print_r(['dir', $dir]);
     if (!is_dir($dir) && !mkdir($dir, 0777, true)) {
         throw new \CB\Service\Exception('Unable create upload path!');
     }
     //print_r([$file, $target]);die;
     // move file
     if (!copy($file, $target)) {
         throw new \CB\Service\Exception('Unable move uploaded file "' . $name . '"!');
     }
     // resize image and create thumbnails
     $Image = new \CB\Image();
     try {
         $Image->load($target);
         foreach (\CB\Config::get('image.sizes') as $suffix => $size) {
             $Image->{$size}[0]($size[1], $size[2]);
             $Image->save($this->_getFileSizePath($fileName, $suffix, $extension));
         }
         unset($Image);
     } catch (\Exception $e) {
         // cleanup
         unset($Image);
         $this->_deleteFiles($fileName, $extension);
         throw new \CB\Service\Exception($e->getMessage());
     }
     // set file entity
     $File = new \CB\Entity\File();
     $File->setValues(['name' => $name, 'fileName' => $fileName, 'extension' => $extension, 'mimeType' => $mimeType, 'created' => $this->_created]);
     $File->setUser($User);
     $File->setLocation($Location);
     $Location->getFiles()->add($File);
     // save file entity
     try {
         $this->getEntityManager()->persist($File);
     } catch (\Exception $e) {
         // cleanup
         $this->_deleteFiles($fileName, $extension);
         throw $e;
     }
     return $File;
 }
Exemplo n.º 3
0
 /**
  * Get slug from location name
  *
  * @param $name
  * @return mixed|string
  */
 public function getSlug($name)
 {
     $slug = \CB\slug($name);
     $exists = $this->slugExists($slug);
     if ($exists) {
         $original = $slug;
         $num = 2;
         while ($exists) {
             $slug = $original . '-' . $num;
             $exists = $this->slugExists($slug);
             $num++;
         }
     }
     return $slug;
 }