@@ -112,7 +112,7 @@ public function getPreviewsForMimeTypes(array $mimeTypes): \Generator {
112112 public function deleteAll (): void {
113113 $ lastId = 0 ;
114114 while (true ) {
115- $ previews = $ this ->previewMapper ->getPreviews ($ lastId , 1000 );
115+ $ previews = $ this ->previewMapper ->getPreviews ($ lastId , PreviewMapper:: MAX_CHUNK_SIZE );
116116 $ i = 0 ;
117117
118118 // FIXME: Should we use transaction here? Du to the I/O created when
@@ -124,7 +124,7 @@ public function deleteAll(): void {
124124 $ lastId = $ preview ->getId ();
125125 }
126126
127- if ($ i !== 1000 ) {
127+ if ($ i !== PreviewMapper:: MAX_CHUNK_SIZE ) {
128128 break ;
129129 }
130130 }
@@ -137,4 +137,33 @@ public function deleteAll(): void {
137137 public function getAvailablePreviews (array $ fileIds ): array {
138138 return $ this ->previewMapper ->getAvailablePreviews ($ fileIds );
139139 }
140+
141+ public function deleteExpiredPreviews (int $ maxAgeDays ): void {
142+ try {
143+ $ this ->connection ->beginTransaction ();
144+
145+ $ lastId = 0 ;
146+ $ startTime = time ();
147+ while (true ) {
148+ $ previews = $ this ->previewMapper ->getPreviews ($ lastId , PreviewMapper::MAX_CHUNK_SIZE , $ maxAgeDays );
149+ $ i = 0 ;
150+ foreach ($ previews as $ preview ) {
151+ $ this ->deletePreview ($ preview );
152+ $ i ++;
153+ $ lastId = $ preview ->getId ();
154+ }
155+
156+ if ($ i !== PreviewMapper::MAX_CHUNK_SIZE ) {
157+ break ;
158+ }
159+
160+ // Stop if execution time is more than one hour.
161+ if (time () - $ startTime > 3600 ) {
162+ return ;
163+ }
164+ }
165+ } finally {
166+ $ this ->connection ->commit ();
167+ }
168+ }
140169}
0 commit comments