From 79c205becaa03a7ca3a8f73f5edbcaba5d0af75a Mon Sep 17 00:00:00 2001 From: Antipkin-A Date: Mon, 19 Oct 2020 12:22:48 +0300 Subject: [PATCH] save version author --- controller/callbackcontroller.php | 4 +++ lib/fileversions.php | 48 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/controller/callbackcontroller.php b/controller/callbackcontroller.php index 3117100..a2d89d5 100644 --- a/controller/callbackcontroller.php +++ b/controller/callbackcontroller.php @@ -547,6 +547,10 @@ class CallbackController extends Controller { FileVersions::saveHistory($file->getFileInfo(), $history, $changes, $prevVersion); } + if (!empty($user)) { + FileVersions::saveAuthor($file->getFileInfo(), $user); + } + $result = 0; } catch (\Exception $e) { $this->logger->logException($e, ["message" => "Track: $fileId status $status error", "app" => $this->appName]); diff --git a/lib/fileversions.php b/lib/fileversions.php index 2de1308..cc354f9 100644 --- a/lib/fileversions.php +++ b/lib/fileversions.php @@ -23,6 +23,7 @@ use OC\Files\Node\File; use OC\Files\View; use OCP\Files\FileInfo; +use OCP\IUser; /** * File versions @@ -52,6 +53,13 @@ class FileVersions { */ private static $historyExt = ".json"; + /** + * File name contain author + * + * @var string + */ + private static $authorExt = "_author.json"; + /** * Split file path and version id * @@ -327,4 +335,44 @@ class FileVersions { $logger->debug("deleteVersion $changesPath", ["app" => self::$appName]); } } + + /** + * Save file author + * + * @param FileInfo $fileInfo - file info + * @param IUser $author - version author + */ + public static function saveAuthor($fileInfo, $author) { + $logger = \OC::$server->getLogger(); + + if ($fileInfo === null || $author === null) { + return; + } + + $owner = $fileInfo->getOwner(); + if ($owner === null) { + return; + } + + $ownerId = $owner->getUID(); + $fileId = $fileInfo->getId(); + $versionId = $fileInfo->getMtime(); + + list ($view, $path) = self::getView($ownerId, $fileId, true); + + try { + $authorPath = $path . "/" . $versionId . self::$authorExt; + $view->touch($authorPath); + + $authorData = [ + "id" => $author->getUID(), + "name" => $author->getDisplayName() + ]; + $view->file_put_contents($authorPath, json_encode($authorData)); + + $logger->debug("saveAuthor: $fileId for $ownerId stored author $authorPath", ["app" => self::$appName]); + } catch (\Exception $e) { + $logger->logException($e, ["message" => "saveAuthor: save $fileId author error", "app" => self::$appName]); + } + } } \ No newline at end of file