/** * @param $targetPath * @return File * @throws PartialUploadException */ public function moveLocal($targetPath) { // source stream $sourceFile = $this->getFile(); $sourceFileReadStream = $sourceFile->getStream('r'); // target stream $targetFile = new File($targetPath, $sourceFile->getOriginalBasename(), $sourceFile->getSize(), $sourceFile->getType()); $targetFileWriteStream = $targetFile->getStream('w+'); // move stream content $size = stream_copy_to_stream($sourceFileReadStream, $targetFileWriteStream); // close resources fclose($sourceFileReadStream); fclose($targetFileWriteStream); // check copied file size if ($size !== $this->getFileSize()) { throw new PartialUploadException('Partial upload. Expected ' . $this->getFileSize() . ', found ' . $size); } return $targetFile; }
/** * @return File */ public function getFile() { if ($this->file) { return $this->file; } // validate $this->validate(); // test if format supported if ($this->supportedFormats && !in_array($this->file->getExtension(), $this->supportedFormats)) { throw new WrongFormatException('File type not allowed'); } // check checksum if ($this->isChecksumValidationAllowed) { if ($this->getExpectedChecksum() !== $this->file->getChecksum()) { throw new WrongChecksumException('Checksum missmatch'); } } // build file $this->file = $this->buildFile(); return $this->file; }
private function buildTargetBasename(File $sourceFile, $targetFilename = null) { if ($targetFilename) { $extension = $sourceFile->getExtension(); if ($extension) { $targetBasename = $targetFilename . '.' . $extension; } else { $targetBasename = $targetFilename; } } else { $targetBasename = $sourceFile->getOriginalBasename(); } return $targetBasename; }