示例#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 $content_range
  * @return File
  */
 protected function process($tmp_name, $name, $size, $type, $error, $index = 0, $content_range = null)
 {
     $file = $this->getFileContainer();
     $file->name = $this->getFilename($name, $type, $index, $content_range, $tmp_name);
     $file->size = $this->fixIntegerOverflow(intval($size));
     if ($content_range) {
         $file->type = $type;
     } else {
         $file->setTypeFromPath($tmp_name);
     }
     if ($file->name) {
         //since the md5 filename generator would return false if it's allowDuplicate property is set to false and the file already exists.
         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 = $content_range && $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, array('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 (!$content_range) {
                     // The file is incomplete and it's not a chunked upload, abort
                     $this->filesystem->unlink($file_path);
                     $file->error = 'abort';
                 }
             }
         }
     }
     return $file;
 }
示例#2
0
 /**
  * Get unique but consistent name
  * @param  string  $name
  * @param  string  $type
  * @param  integer $index
  * @param  array   $content_range
  * @return string
  */
 protected function getUniqueFilename($name, $type, $index, $content_range)
 {
     while ($this->filesystem->isDir($this->pathresolver->getUploadPath($name))) {
         $name = $this->pathresolver->upcountName($name);
     }
     $uploaded_bytes = $this->fixIntegerOverflow(intval($content_range[1]));
     while ($this->filesystem->isFile($this->pathresolver->getUploadPath($name))) {
         if ($uploaded_bytes == $this->getFilesize($this->pathresolver->getUploadPath($name))) {
             break;
         }
         $name = $this->pathresolver->upcountName($name);
     }
     return $name;
 }