Ejemplo n.º 1
0
 /**
  * Test file_validate_size().
  */
 function testFileValidateSize()
 {
     // Create a file with a size of 1000 bytes, and quotas of only 1 byte.
     $file = entity_create('file', array('filesize' => 1000));
     $errors = file_validate_size($file, 0, 0);
     $this->assertEqual(count($errors), 0, 'No limits means no errors.', 'File');
     $errors = file_validate_size($file, 1, 0);
     $this->assertEqual(count($errors), 1, 'Error for the file being over the limit.', 'File');
     $errors = file_validate_size($file, 0, 1);
     $this->assertEqual(count($errors), 1, 'Error for the user being over their limit.', 'File');
     $errors = file_validate_size($file, 1, 1);
     $this->assertEqual(count($errors), 2, 'Errors for both the file and their limit.', 'File');
 }
Ejemplo n.º 2
0
 /**
  * Resizes a list of imce items and returns succeeded ones.
  */
 public function resizeItems(ImceFM $fm, array $items, $width, $height, $copy = FALSE)
 {
     $factory = \Drupal::service('image.factory');
     $fs = \Drupal::service('file_system');
     $success = array();
     foreach ($items as $item) {
         $uri = $item->getUri();
         $image = $factory->get($uri);
         // Check vallidity
         if (!$image->isValid()) {
             continue;
         }
         // Check if resizing is needed.
         $resize = $image->getWidth() != $width || $image->getHeight() != $height;
         if (!$resize && !$copy) {
             continue;
         }
         if ($resize && !$image->resize($width, $height)) {
             continue;
         }
         // Save
         $destination = $copy ? file_create_filename($fs->basename($uri), $fs->dirname($uri)) : $uri;
         if (!$image->save($destination)) {
             continue;
         }
         // Create a new file record.
         if ($copy) {
             $filename = $fs->basename($destination);
             $values = array('uid' => $fm->user->id(), 'status' => 1, 'filename' => $filename, 'uri' => $destination, 'filesize' => $image->getFileSize(), 'filemime' => $image->getMimeType());
             $file = \Drupal::entityTypeManager()->getStorage('file')->create($values);
             // Check quota
             if ($errors = file_validate_size($file, 0, $fm->getConf('quota'))) {
                 file_unmanaged_delete($destination);
                 $fm->setMessage($errors[0]);
             } else {
                 $file->save();
                 // Add imce item
                 $item->parent->addFile($filename)->addToJs();
             }
         } else {
             if ($file = Imce::getFileEntity($uri)) {
                 $file->setSize($image->getFileSize());
                 $file->save();
             }
             // Add to js
             $item->addToJs();
         }
         $success[] = $item;
     }
     return $success;
 }
Ejemplo n.º 3
0
 /**
  * Test file_validate_size().
  */
 function testFileValidateSize()
 {
     // Run these tests as a regular user.
     $user = entity_create('user', array('uid' => 2, 'name' => $this->randomName()));
     $user->enforceIsNew();
     $user->save();
     \Drupal::currentUser()->setAccount($user);
     // Create a file with a size of 1000 bytes, and quotas of only 1 byte.
     $file = entity_create('file', array('filesize' => 1000));
     $errors = file_validate_size($file, 0, 0);
     $this->assertEqual(count($errors), 0, 'No limits means no errors.', 'File');
     $errors = file_validate_size($file, 1, 0);
     $this->assertEqual(count($errors), 1, 'Error for the file being over the limit.', 'File');
     $errors = file_validate_size($file, 0, 1);
     $this->assertEqual(count($errors), 1, 'Error for the user being over their limit.', 'File');
     $errors = file_validate_size($file, 1, 1);
     $this->assertEqual(count($errors), 2, 'Errors for both the file and their limit.', 'File');
 }