Example #1
0
 /**
  * Process single submitted file
  * @param  string  $tmp_name
  * @param  string  $name
  * @param  integer $size
  * @param  string  $type
  * @param  integer $error
  * @param  integer $index
  * @param  array   $contentRange
  * @return File
  */
 protected function process($tmp_name, $name, $size, $type, $error, $index = 0, $contentRange = null)
 {
     $file = new File();
     $file->name = $this->getFilename($name, $type, $index, $contentRange, $tmp_name);
     $file->size = $this->fixIntegerOverflow(intval($size));
     $file->setTypeFromPath($tmp_name);
     if ($this->validate($tmp_name, $file, $error, $index)) {
         // Now that we passed the validation, we can work with the file
         $upload_path = $this->pathresolver->getUploadPath();
         $file_path = $this->pathresolver->getUploadPath($file->name);
         $append_file = $contentRange && $this->filesystem->isFile($file_path) && $file->size > $this->getFilesize($file_path);
         if ($tmp_name && $this->filesystem->isUploadedFile($tmp_name)) {
             // This is a normal upload from temporary file
             if ($append_file) {
                 // Adding to existing file (chunked uploads)
                 $this->filesystem->writeToFile($file_path, $this->filesystem->getFileStream($tmp_name), true);
             } else {
                 // Upload full file
                 $this->filesystem->moveUploadedFile($tmp_name, $file_path);
             }
         } else {
             // This is a PUT-type upload
             $this->filesystem->writeToFile($file_path, $this->filesystem->getInputStream(), $append_file);
         }
         $file_size = $this->getFilesize($file_path, $append_file);
         if ($this->logger) {
             $this->logger->debug('Processing ' . $file->name, ['File path' => $file_path, 'File object' => $file, 'Append to file?' => $append_file, 'File exists?' => $this->filesystem->isFile($file_path), 'File size' => $file_size]);
         }
         if ($file->size == $file_size) {
             // Yay, upload is complete!
             $file->path = $file_path;
             $file->completed = true;
             $this->processCallbacksFor('completed', $file);
         } else {
             $file->size = $file_size;
             if (!$contentRange) {
                 // The file is incomplete and it's not a chunked upload, abort
                 $this->filesystem->unlink($file_path);
                 $file->error = 'abort';
             }
         }
     }
     return $file;
 }
Example #2
0
 public function testImageNegativeDetermination()
 {
     $file = new File();
     $file->setTypeFromPath(__DIR__ . '/../fixtures/fake-image.jpg');
     $this->assertFalse($file->isImage(), 'Detect fake image');
 }