Completed
Push — master ( 522461...c89cc2 )
by Fabien
02:24
created

DuplicateFilesFinderTool::getDataService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Fab\Media\Tool;
3
4
/*
5
 * This file is part of the Fab/Media project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10
11
use Fab\Vidi\Service\DataService;
12
use TYPO3\CMS\Core\Resource\ResourceFactory;
13
use TYPO3\CMS\Core\Resource\StorageRepository;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use Fab\Vidi\Tool\AbstractTool;
16
17
/**
18
 * Search for duplicate files having the same "sha1" and process them.
19
 */
20
class DuplicateFilesFinderTool extends AbstractTool
21
{
22
23
    /**
24
     * Display the title of the tool on the welcome screen.
25
     *
26
     * @return string
27
     */
28
    public function getTitle()
29
    {
30
        return 'Find duplicate Files';
31
    }
32
33
    /**
34
     * Display the description of the tool in the welcome screen.
35
     *
36
     * @return string
37
     */
38
    public function getDescription()
39
    {
40
        $templateNameAndPath = 'EXT:media/Resources/Private/Standalone/Tool/DuplicateFilesFinder/Launcher.html';
41
        $view = $this->initializeStandaloneView($templateNameAndPath);
42
        $view->assign('isAdmin', $this->getBackendUser()->isAdmin());
43
        $view->assign('sitePath', PATH_site);
44
        return $view->render();
45
    }
46
47
    /**
48
     * Do the job: analyse Index.
49
     *
50
     * @param array $arguments
51
     * @return string
52
     */
53
    public function work(array $arguments = [])
54
    {
55
56
        // Possible clean up of missing files if the User has clicked so.
57
        if (!empty($arguments['deleteDuplicateFiles'])) {
58
            $this->deleteMissingFilesAction($arguments['files']);
59
        }
60
61
        $templateNameAndPath = 'EXT:media/Resources/Private/Standalone/Tool/DuplicateFilesFinder/WorkResult.html';
62
        $view = $this->initializeStandaloneView($templateNameAndPath);
63
64
        $duplicateFilesReports = [];
65
66
        if ($this->getBackendUser()->isAdmin()) {
67 View Code Duplication
            foreach ($this->getStorageRepository()->findAll() as $storage) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
                if ($storage->isOnline()) {
69
                    $duplicateFiles = $this->getIndexAnalyser()->searchForDuplicateSha1($storage);
70
                    $duplicateFilesReports[] = array(
71
                        'storage' => $storage,
72
                        'duplicateFiles' => $duplicateFiles,
73
                        'numberOfDuplicateFiles' => count($duplicateFiles),
74
                    );
75
                }
76
            }
77
        } else {
78
79
            $fileMounts = $this->getBackendUser()->getFileMountRecords();
80
81
            $allowedStorages = [];
82
            foreach ($fileMounts as $fileMount) {
83
                if ((bool)$fileMount['read_only']) {
84
                    continue;
85
                }
86
87
                if (!isset($allowedStorages[$fileMount['base']])) {
88
                    $allowedStorages[$fileMount['base']] = [];
89
                }
90
                if (!in_array($fileMount['base'], $allowedStorages)) {
91
                    $allowedStorages[$fileMount['base']][] = $fileMount['path'];
92
                }
93
            }
94
95
            foreach ($allowedStorages as $storageIdentifier => $allowedMountPoints) {
96
                $storage = ResourceFactory::getInstance()->getStorageObject($storageIdentifier);
97
98
                if ($storage->isOnline()) {
99
100
                    $duplicateFiles = $this->getIndexAnalyser()->searchForDuplicateSha1($storage);
101
102
                    // Filter duplicates files
103
                    foreach ($duplicateFiles as $key => $files) {
104
105
                        $filteredFiles = [];
106
                        foreach ($files as $file) {
107
108
                            foreach ($allowedMountPoints as $allowedMountPoint) {
109
110
                                $pattern = '%^' . $allowedMountPoint . '%isU';
111
                                if (preg_match($pattern, $file['identifier'])) {
112
                                    $filteredFiles[] = $file;
113
                                    break; // no need to further loop around, stop the loop.
114
                                }
115
                            }
116
                        }
117
118
                        // We need more than 1 files to be shown as duplicate.
119
                        if (count($filteredFiles) > 1) {
120
                            $duplicateFiles[$key] = $filteredFiles;
121
                        } else {
122
                            unset($duplicateFiles[$key]);
123
                        }
124
                    }
125
                    $duplicateFilesReports[] = array(
126
                        'storage' => $storage,
127
                        'duplicateFiles' => $duplicateFiles,
128
                        'numberOfDuplicateFiles' => count($duplicateFiles),
129
                    );
130
131
                }
132
            }
133
        }
134
135
        $view->assign('duplicateFilesReports', $duplicateFilesReports);
136
137
        return $view->render();
138
    }
139
140
    /**
141
     * Delete files given as parameter.
142
     * This is a special case as we have a missing file in the file system
143
     * As a result, we can't use $fileObject->delete(); which will
144
     * raise exception "Error while fetching permissions".
145
     *
146
     * @param array $files
147
     * @return void
148
     */
149
    protected function deleteMissingFilesAction(array $files = [])
150
    {
151
152
        foreach ($files as $fileUid) {
153
154
            /** @var \TYPO3\CMS\Core\Resource\File $file */
155
            try {
156
                $file = ResourceFactory::getInstance()->getFileObject($fileUid);
157
                if ($file->exists()) {
158
159
                    $numberOfReferences = $this->getFileReferenceService()->countTotalReferences($file);
160
                    if ($numberOfReferences === 0) {
161
                        $file->delete();
162
                    }
163
                } else {
164
                    $this->getDataService()->delete('sys_file', ['uid' => $file->getUid()]);
165
                }
166
            } catch (\Exception $e) {
167
                continue;
168
            }
169
        }
170
    }
171
172
    /**
173
     * Return a pointer to the database.
174
     *
175
     * @return \Fab\Media\Index\IndexAnalyser|object
176
     */
177
    protected function getIndexAnalyser()
178
    {
179
        return GeneralUtility::makeInstance(\Fab\Media\Index\IndexAnalyser::class);
180
    }
181
182
    /**
183
     * @return \Fab\Media\Thumbnail\ThumbnailGenerator|object
184
     */
185
    protected function getThumbnailGenerator()
186
    {
187
        return GeneralUtility::makeInstance(\Fab\Media\Thumbnail\ThumbnailGenerator::class);
188
    }
189
190
    /**
191
     * @return StorageRepository|object
192
     */
193
    protected function getStorageRepository()
194
    {
195
        return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class);
196
    }
197
198
    /**
199
     * Tell whether the tools should be displayed according to the context.
200
     *
201
     * @return bool
202
     */
203
    public function isShown()
204
    {
205
        return true;
206
    }
207
208
    /**
209
     * @return \Fab\Media\Resource\FileReferenceService|object
210
     */
211
    protected function getFileReferenceService()
212
    {
213
        return GeneralUtility::makeInstance(\Fab\Media\Resource\FileReferenceService::class);
214
    }
215
216
    /**
217
     * @return object|DataService
218
     */
219
    protected function getDataService(): DataService
220
    {
221
        return GeneralUtility::makeInstance(DataService::class);
222
    }
223
224
}
225
226