2828
2929use OC \BackgroundJob \TimedJob ;
3030use OC \Files \AppData \Factory ;
31+ use OC \Preview \Storage \Root ;
3132use OCP \DB \QueryBuilder \IQueryBuilder ;
33+ use OCP \Files \IMimeTypeLoader ;
3234use OCP \Files \NotFoundException ;
3335use OCP \Files \NotPermittedException ;
36+ use OCP \Files \SimpleFS \ISimpleFolder ;
3437use OCP \IDBConnection ;
3538
3639class BackgroundCleanupJob extends TimedJob {
3740
3841 /** @var IDBConnection */
3942 private $ connection ;
4043
41- /** @var Factory */
42- private $ appDataFactory ;
44+ /** @var Root */
45+ private $ previewFolder ;
4346
4447 /** @var bool */
4548 private $ isCLI ;
4649
50+ /** @var IMimeTypeLoader */
51+ private $ mimeTypeLoader ;
52+
4753 public function __construct (IDBConnection $ connection ,
48- Factory $ appDataFactory ,
54+ Root $ previewFolder ,
55+ IMimeTypeLoader $ mimeTypeLoader ,
4956 bool $ isCLI ) {
5057 // Run at most once an hour
5158 $ this ->setInterval (3600 );
5259
5360 $ this ->connection = $ connection ;
54- $ this ->appDataFactory = $ appDataFactory ;
61+ $ this ->previewFolder = $ previewFolder ;
5562 $ this ->isCLI = $ isCLI ;
63+ $ this ->mimeTypeLoader = $ mimeTypeLoader ;
5664 }
5765
5866 public function run ($ argument ) {
59- $ previews = $ this ->appDataFactory ->get ('preview ' );
67+ foreach ($ this ->getDeletedFiles () as $ fileId ) {
68+ try {
69+ $ preview = $ this ->previewFolder ->getFolder ((string )$ fileId );
70+ $ preview ->delete ();
71+ } catch (NotFoundException $ e ) {
72+ // continue
73+ } catch (NotPermittedException $ e ) {
74+ // continue
75+ }
76+ }
77+ }
6078
61- $ previewFodlerId = $ previews ->getId ();
79+ private function getDeletedFiles (): \Iterator {
80+ yield from $ this ->getOldPreviewLocations ();
81+ yield from $ this ->getNewPreviewLocations ();
82+ }
6283
84+ private function getOldPreviewLocations (): \Iterator {
6385 $ qb = $ this ->connection ->getQueryBuilder ();
6486 $ qb ->select ('a.name ' )
6587 ->from ('filecache ' , 'a ' )
@@ -69,7 +91,9 @@ public function run($argument) {
6991 ->where (
7092 $ qb ->expr ()->isNull ('b.fileid ' )
7193 )->andWhere (
72- $ qb ->expr ()->eq ('a.parent ' , $ qb ->createNamedParameter ($ previewFodlerId ))
94+ $ qb ->expr ()->eq ('a.parent ' , $ qb ->createNamedParameter ($ this ->previewFolder ->getId ()))
95+ )->andWhere (
96+ $ qb ->expr ()->like ('a.name ' , $ qb ->createNamedParameter ('__% ' ))
7397 );
7498
7599 if (!$ this ->isCLI ) {
@@ -79,14 +103,49 @@ public function run($argument) {
79103 $ cursor = $ qb ->execute ();
80104
81105 while ($ row = $ cursor ->fetch ()) {
82- try {
83- $ preview = $ previews ->getFolder ($ row ['name ' ]);
84- $ preview ->delete ();
85- } catch (NotFoundException $ e ) {
86- // continue
87- } catch (NotPermittedException $ e ) {
88- // continue
89- }
106+ yield $ row ['name ' ];
107+ }
108+
109+ $ cursor ->closeCursor ();
110+ }
111+
112+ private function getNewPreviewLocations (): \Iterator {
113+ $ qb = $ this ->connection ->getQueryBuilder ();
114+ $ qb ->select ('path ' , 'mimetype ' )
115+ ->from ('filecache ' )
116+ ->where ($ qb ->expr ()->eq ('fileid ' , $ qb ->createNamedParameter ($ this ->previewFolder ->getId ())));
117+ $ cursor = $ qb ->execute ();
118+ $ data = $ cursor ->fetch ();
119+ $ cursor ->closeCursor ();
120+
121+ if ($ data === null ) {
122+ return [];
123+ }
124+
125+ $ like = $ this ->connection ->escapeLikeParameter ($ data ['path ' ]) . '/_/_/_/_/_/_/_/% ' ;
126+
127+ $ qb = $ this ->connection ->getQueryBuilder ();
128+ $ qb ->select ('a.name ' )
129+ ->from ('filecache ' , 'a ' )
130+ ->leftJoin ('a ' , 'filecache ' , 'b ' , $ qb ->expr ()->eq (
131+ $ qb ->expr ()->castColumn ('a.name ' , IQueryBuilder::PARAM_INT ), 'b.fileid '
132+ ))
133+ ->where (
134+ $ qb ->expr ()->andX (
135+ $ qb ->expr ()->isNull ('b.fileid ' ),
136+ $ qb ->expr ()->like ('a.path ' , $ qb ->createNamedParameter ($ like )),
137+ $ qb ->expr ()->eq ('a.mimetype ' , $ qb ->createNamedParameter ($ this ->mimeTypeLoader ->getId ('httpd/unix-directory ' )))
138+ )
139+ );
140+
141+ if (!$ this ->isCLI ) {
142+ $ qb ->setMaxResults (10 );
143+ }
144+
145+ $ cursor = $ qb ->execute ();
146+
147+ while ($ row = $ cursor ->fetch ()) {
148+ yield $ row ['name ' ];
90149 }
91150
92151 $ cursor ->closeCursor ();
0 commit comments