getUploadDir()->getFile($this->getUserDirectoryHash() . $targetLocation); $returnValue['uploaded'] = $file->put(fopen($tmp_name, 'rb')); $this->getServiceManager()->get(EventManager::CONFIG_ID)->trigger(new FileUploadedEvent($file)); tao_helpers_File::remove($tmp_name); $data['type'] = $file->getMimetype(); $data['uploaded_file'] = $file->getBasename(); $data['name'] = $name; $data['size'] = array_key_exists('size', $postedFile) ? $postedFile['size'] : $file->getSize(); $returnValue['name'] = $name; $returnValue['uploaded_file'] = $data['uploaded_file']; $returnValue['data'] = json_encode($data); return $returnValue; } /** * Returns the file system identifier. * * @return string */ public function getUploadFSid() { return self::$tmpFilesystemId; } /** * Returns the current file system directory instance. * * @return Directory */ public function getUploadDir() { return $this->getServiceLocator()->get(FileSystemService::SERVICE_ID)->getDirectory($this->getUploadFSid()); } /** * * @return \oat\generis\model\fileReference\UrlFileSerializer */ public function getSerializer() { if (!$this->uriSerializer) { $this->uriSerializer = new UrlFileSerializer(); $this->uriSerializer->setServiceLocator($this->getServiceLocator()); } return $this->uriSerializer; } /** * Detects * @param $file * @return File|string * @deprecated * @throws \common_Exception */ public function universalizeUpload($file) { if ((is_string($file) && is_file($file)) || $file instanceof File) { return $file; } return $this->getUploadedFlyFile($file); } /** * Either create local copy or use original location for tile * Returns absolute path to the file, to be compatible with legacy methods * @deprecated * @param string|File $serial * @return string * @throws \common_Exception */ public function getUploadedFile($serial) { $file = $this->universalizeUpload($serial); if ($file instanceof File) { $file = $this->getLocalCopy($file); } return $file; } /** * @param string $serial * * @throws \common_exception_NotAcceptable When the uploaded file url contains a wrong system id. * * @return File */ public function getUploadedFlyFile($serial) { $path = $this->getUserDirectoryHash() . '/'; if (filter_var($serial, FILTER_VALIDATE_URL)) { // Getting the File instance of the file url. $fakeFile = $this->getSerializer()->unserializeFile($serial); // Filesystem hack check. if ($fakeFile->getFileSystemId() !== $this->getUploadFSid()) { throw new \common_exception_NotAcceptable( 'The uploaded file url contains a wrong filesystem id!' . '(Expected: `' . $this->getUploadFSid() . '` Actual: `' . $fakeFile->getFileSystemId() . '`)' ); } $path .= $fakeFile->getBasename(); } else { $path .= $serial; } $realFile = $this->getUploadDir()->getFile($path); if ($realFile->exists()) { return $realFile; } return null; } /** * Deprecated, for compatibility with legacy code * @param $file * @return string * @throws \common_Exception */ private function getLocalCopy(File $file) { $tmpName = \tao_helpers_File::concat([\tao_helpers_File::createTempDir(), $file->getBasename()]); if (($resource = fopen($tmpName, 'wb')) !== false) { stream_copy_to_stream($file->readStream(), $resource); fclose($resource); $this->getServiceLocator()->get(EventManager::CONFIG_ID)->trigger(new UploadLocalCopyCreatedEvent( $file, $tmpName )); return $tmpName; } throw new \common_Exception('Impossible to make local file copy at ' . $tmpName); } /** * @param FileUploadedEvent $event */ public function listenUploadEvent(FileUploadedEvent $event) { $storage = new TempFlyStorageAssociation($this->getServiceLocator()); $storage->setUpload($event->getFile()); } /** * @param UploadLocalCopyCreatedEvent $event */ public function listenLocalCopyEvent(UploadLocalCopyCreatedEvent $event) { $storage = new TempFlyStorageAssociation($this->getServiceLocator()); $storage->addLocalCopies($event->getFile(), $event->getTmpPath()); } public function remove($file) { $storage = new TempFlyStorageAssociation($this->getServiceLocator()); if ($file instanceof File) { $storedLocalTmps = $storage->getLocalCopies($file); foreach ((array)$storedLocalTmps as $tmp) { tao_helpers_File::remove($tmp); } $storage->removeFiles($file); $file->delete(); } } /** * Returns the username directory hash. * * @return string */ public function getUserDirectoryHash() { return hash( 'crc32b', \common_session_SessionManager::getSession()->getUser()->getIdentifier() ); } /** * Is the uploaded filename looks as an uploaded file. * * @param $filePath * * @return bool */ public function isUploadedFile($filePath) { // If it's a serialized one. if (filter_var($filePath, FILTER_VALIDATE_URL)) { return true; } // If it's a normal filename. $file = $this->getUploadDir()->getFile($this->getUserDirectoryHash() . '/' . $filePath); if ($file->exists()) { return true; } return false; } }