4242use OCP \Files \SimpleFS \ISimpleFile ;
4343use OCP \ICache ;
4444use OCP \ICacheFactory ;
45+ use OCP \IConfig ;
4546use OCP \IRequest ;
4647use OCP \Lock \LockedException ;
4748use OCP \PreConditionNotMetException ;
@@ -70,8 +71,9 @@ class DocumentService {
7071 private IAppData $ appData ;
7172 private ILockManager $ lockManager ;
7273 private IUserMountCache $ userMountCache ;
74+ private IConfig $ config ;
7375
74- public function __construct (DocumentMapper $ documentMapper , StepMapper $ stepMapper , SessionMapper $ sessionMapper , IAppData $ appData , ?string $ userId , IRootFolder $ rootFolder , ICacheFactory $ cacheFactory , LoggerInterface $ logger , ShareManager $ shareManager , IRequest $ request , IManager $ directManager , ILockManager $ lockManager , IUserMountCache $ userMountCache ) {
76+ public function __construct (DocumentMapper $ documentMapper , StepMapper $ stepMapper , SessionMapper $ sessionMapper , IAppData $ appData , ?string $ userId , IRootFolder $ rootFolder , ICacheFactory $ cacheFactory , LoggerInterface $ logger , ShareManager $ shareManager , IRequest $ request , IManager $ directManager , ILockManager $ lockManager , IUserMountCache $ userMountCache, IConfig $ config ) {
7577 $ this ->documentMapper = $ documentMapper ;
7678 $ this ->stepMapper = $ stepMapper ;
7779 $ this ->sessionMapper = $ sessionMapper ;
@@ -83,6 +85,7 @@ public function __construct(DocumentMapper $documentMapper, StepMapper $stepMapp
8385 $ this ->shareManager = $ shareManager ;
8486 $ this ->lockManager = $ lockManager ;
8587 $ this ->userMountCache = $ userMountCache ;
88+ $ this ->config = $ config ;
8689 $ token = $ request ->getParam ('token ' );
8790 if ($ this ->userId === null && $ token !== null ) {
8891 try {
@@ -428,7 +431,7 @@ public function resetDocument(int $documentId, bool $force = false): void {
428431 }
429432 }
430433
431- public function getAll (): array {
434+ public function getAll (): \ Generator {
432435 return $ this ->documentMapper ->findAll ();
433436 }
434437
@@ -642,4 +645,40 @@ public function unlock(int $fileId): void {
642645 } catch (NoLockProviderException | PreConditionNotMetException | NotFoundException $ e ) {
643646 }
644647 }
648+
649+ public function countAll (): int {
650+ return $ this ->documentMapper ->countAll ();
651+ }
652+
653+ private function getFullAppFolder (): Folder {
654+ $ appFolder = $ this ->rootFolder ->get ('appdata_ ' . $ this ->config ->getSystemValueString ('instanceid ' , '' ) . '/text ' );
655+ if (!$ appFolder instanceof Folder) {
656+ throw new NotFoundException ('Folder not found ' );
657+ }
658+ return $ appFolder ;
659+ }
660+
661+ public function clearAll (): void {
662+ $ this ->stepMapper ->clearAll ();
663+ $ this ->sessionMapper ->clearAll ();
664+ $ this ->documentMapper ->clearAll ();
665+ try {
666+ $ appFolder = $ this ->getFullAppFolder ();
667+ $ appFolder ->get ('documents ' )->move ($ appFolder ->getPath () . '/documents_old_ ' . time ());
668+ } catch (NotFoundException ) {
669+ }
670+ $ this ->ensureDocumentsFolder ();
671+ }
672+
673+ public function cleanupOldDocumentsFolders (): void {
674+ try {
675+ $ appFolder = $ this ->getFullAppFolder ();
676+ foreach ($ appFolder ->getDirectoryListing () as $ node ) {
677+ if (str_starts_with ($ node ->getName (), 'documents_old_ ' )) {
678+ $ node ->delete ();
679+ }
680+ }
681+ } catch (NotFoundException ) {
682+ }
683+ }
645684}
0 commit comments