1414use OCP \Files \Lock \ILock ;
1515use OCP \IL10N ;
1616use OCP \IUser ;
17+ use Override ;
18+ use Psr \Log \LoggerInterface ;
1719
1820class FileContext implements IContext {
1921
2022 public function __construct (
2123 private readonly FileService $ fileService ,
2224 private readonly IL10N $ l10n ,
2325 private readonly LockService $ lockService ,
26+ private readonly LoggerInterface $ logger ,
2427 private readonly File $ file ,
2528 private readonly ?string $ baseVersionEtag ,
2629 private readonly ?string $ token = null ,
2730 ) {
2831 }
2932
30- public function check (): ?string {
33+ #[Override]
34+ public function getId (): int {
35+ return $ this ->file ->getId ();
36+ }
37+
38+ #[Override]
39+ public function getType (): string {
40+ return 'file ' ;
41+ }
42+
43+ #[Override]
44+ public function toString (): string {
45+ return $ this ->getType () . ' ( ' . $ this ->getId () . ') ' ;
46+ }
47+
48+ #[Override]
49+ public function buildDocument (): Document |string {
3150 // Block using text for disabled download internal shares
3251 if ($ this ->fileService ->isDownloadDisabled ($ this ->file )) {
3352 return $ this ->l10n ->t ('This file cannot be displayed as download is disabled by the share ' );
3453 }
35- return null ;
54+ $ document = new Document ();
55+ $ document ->setId ($ this ->getId ());
56+ $ document ->setLastSavedVersion (0 );
57+ $ document ->setLastSavedVersionTime ($ this ->file ->getMTime ());
58+ $ document ->setLastSavedVersionEtag ($ this ->file ->getEtag ());
59+ $ document ->setChecksum ($ this ->computeChecksum ());
60+ // This is a new document - so it needs a fresh base version etag.
61+ $ document ->setBaseVersionEtag (uniqid ());
62+ return $ document ;
3663 }
3764
38- public function checkDocument (Document $ document ): ?string {
65+ #[Override]
66+ public function prepareSession (DocumentData $ documentData ): SessionInfo |string {
67+ $ document = $ documentData ->document ;
68+ $ documentState = $ documentData ->documentState ;
69+
3970 if ($ this ->baseVersionEtag !== null && $ this ->baseVersionEtag !== $ document ->getBaseVersionEtag ()) {
4071 return $ this ->l10n ->t ('Editing session has expired. Please reload the page. ' );
4172 }
42- return null ;
43- }
4473
45- public function isReadOnly (): bool {
46- return $ this ->fileService ->isReadOnly ($ this ->file , $ this ->token );
47- }
74+ $ content = null ;
75+ if ($ documentState === null ) {
76+ $ this ->logger ->debug ('Sending content for ' . $ document ->toString ());
77+ $ content = $ this ->loadContent ();
78+ }
4879
49- public function getId (): int {
50- return $ this ->file ->getId ();
80+ $ readOnly = $ this ->isReadOnly ();
81+ $ lockInfo = $ this ->getLockInfo ();
82+ if (!$ readOnly ) {
83+ $ isLocked = $ this ->lock ();
84+ if (!$ isLocked ) {
85+ $ readOnly = true ;
86+ }
87+ }
88+
89+ return new SessionInfo (
90+ content: $ content ,
91+ readOnly: $ readOnly ,
92+ lock: $ lockInfo ,
93+ hasOwner: $ this ->getOwner () !== null ,
94+ );
5195 }
5296
53- public function getType (): string {
54- return ' file ' ;
97+ private function computeCheckSum (): string {
98+ return hash ( ' crc32 ' , $ this -> file -> getContent ()) ;
5599 }
56100
57- public function toString (): string {
58- return $ this ->getType () . ' ( ' . $ this ->getId () . ' ) ' ;
101+ private function isReadOnly (): bool {
102+ return $ this ->fileService -> isReadOnly ( $ this -> file , $ this ->token ) ;
59103 }
60104
61- public function loadContent (): ?string {
105+ private function loadContent (): ?string {
62106 return $ this ->fileService ->loadContent ($ this ->file );
63107 }
64108
65- public function getLockInfo (): ?ILock {
109+ private function getLockInfo (): ?ILock {
66110 return $ this ->lockService ->getLockByOthers ($ this ->file );
67111 }
68112
69- public function getOwner (): ?IUser {
113+ private function getOwner (): ?IUser {
70114 return $ this ->file ->getOwner ();
71115 }
72116
73- public function lock (): bool {
117+ private function lock (): bool {
74118 // Disable file locking for Readme.md files, because in the
75119 // current setup, this makes it almost impossible to delete these files.
76120 if (strcasecmp ($ this ->file ->getName (), 'Readme.md ' ) !== 0 ) {
@@ -79,20 +123,4 @@ public function lock(): bool {
79123 return true ;
80124 }
81125
82- public function createDocument (): Document {
83- $ document = new Document ();
84- $ document ->setId ($ this ->getId ());
85- $ document ->setLastSavedVersion (0 );
86- $ document ->setLastSavedVersionTime ($ this ->file ->getMTime ());
87- $ document ->setLastSavedVersionEtag ($ this ->file ->getEtag ());
88- $ document ->setChecksum ($ this ->computeChecksum ());
89- // This is a new document - so it needs a fresh base version etag.
90- $ document ->setBaseVersionEtag (uniqid ());
91- return $ document ;
92- }
93-
94- public function computeCheckSum (): string {
95- return hash ('crc32 ' , $ this ->file ->getContent ());
96- }
97-
98126}
0 commit comments