/** * Start a editing session or return an existing one * @param string $uid of the user starting a session * @param \OCA\Documents\File $file - file object * @return array * @throws \Exception */ public static function start($uid, $file) { // Create a directory to store genesis $genesis = new \OCA\Documents\Genesis($file); list($ownerView, $path) = $file->getOwnerViewAndPath(); $mimetype = $ownerView->getMimeType($path); if (!Filter::isSupportedMimetype($mimetype)) { throw new \Exception($path . ' is ' . $mimetype . ' and is not supported by Documents app'); } $oldSession = new Session(); $oldSession->loadBy('file_id', $file->getFileId()); //If there is no existing session we need to start a new one if (!$oldSession->hasData()) { $newSession = new Session(array($genesis->getPath(), $genesis->getHash(), $file->getOwner(), $file->getFileId())); if (!$newSession->insert()) { throw new \Exception('Failed to add session into database'); } } $sessionData = $oldSession->loadBy('file_id', $file->getFileId())->getData(); $memberColor = \OCA\Documents\Helper::getMemberColor($uid); $member = new \OCA\Documents\Db\Member(array($sessionData['es_id'], $uid, $memberColor, time(), intval($file->isPublicShare()), $file->getToken())); if (!$member->insert()) { throw new \Exception('Failed to add member into database'); } $sessionData['member_id'] = (string) $member->getLastInsertId(); // Do we have OC_Avatar in out disposal? if (\OC_Config::getValue('enable_avatars', true) !== true) { $imageUrl = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw=='; } else { $imageUrl = $uid; } $displayName = $file->isPublicShare() ? $uid . ' ' . \OCA\Documents\Db\Member::getGuestPostfix() : \OCP\User::getDisplayName($uid); $userId = $file->isPublicShare() ? $displayName : \OCP\User::getUser(); $op = new \OCA\Documents\Db\Op(); $op->addMember($sessionData['es_id'], $sessionData['member_id'], $displayName, $userId, $memberColor, $imageUrl); $sessionData['title'] = basename($path); $fileInfo = $ownerView->getFileInfo($path); $sessionData['permissions'] = $fileInfo->getPermissions(); return $sessionData; }
protected function initViews() { $this->ownerView = new View('/' . $this->owner); $this->ownerViewFiles = new View('/' . $this->owner . '/files'); $this->path = $this->ownerView->getPath($this->fileId); $this->pathFiles = $this->ownerViewFiles->getPath($this->fileId); if (!$this->path || !$this->pathFiles) { throw new \Exception($this->fileId . ' can not be resolved'); } if (!$this->ownerView->file_exists($this->path)) { throw new \Exception($this->path . ' doesn\'t exist'); } if (!$this->ownerViewFiles->file_exists($this->pathFiles)) { throw new \Exception($this->pathFiles . ' doesn\'t exist'); } if (!$this->ownerView->is_file($this->path)) { throw new \Exception('Object ' . $this->path . ' is not a file.'); } //TODO check if it is a valid odt $mimetype = $this->ownerView->getMimeType($this->path); if (!Filter::isSupportedMimetype($mimetype)) { throw new \Exception($this->path . ' is ' . $mimetype . ' and is not supported by Documents app'); } }