Completed
Pull Request — master (#93)
by Janis
04:19
created

FileService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 8
cts 8
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 7
nc 1
nop 6
crap 1
1
<?php
2
3
/**
4
 * Nextcloud - OCR
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later.
7
 * See the COPYING file.
8
 * 
9
 * @author Janis Koehr <[email protected]>
10
 * @copyright Janis Koehr 2017
11
 */
12
namespace OCA\Ocr\Service;
13
14
use OCA\Ocr\Db\FileMapper;
15
use OCA\Ocr\Db\File;
16
use OCP\ILogger;
17
use OCA\Ocr\Db\ShareMapper;
18
use OCP\IL10N;
19
use OCA\Ocr\Constants\OcrConstants;
20
use OCA\Ocr\Util\FileUtil;
21
22
23
/**
24
 * Class FileService
25
 * 
26
 * @package OCA\Ocr\Service
27
 */
28
class FileService {
29
30
    /**
31
     *
32
     * @var ILogger
33
     */
34
    private $logger;
35
36
    /**
37
     *
38
     * @var FileMapper
39
     */
40
    private $fileMapper;
41
42
    /**
43
     *
44
     * @var ShareMapper
45
     */
46
    private $shareMapper;
47
48
    /**
49
     *
50
     * @var string
51
     */
52
    private $userId;
53
54
    /**
55
     *
56
     * @var IL10N
57
     */
58
    private $l10n;
59
    
60
    /**
61
     * 
62
     * @var FileUtil
63
     */
64
    private $fileUtil;
65
66 19
    public function __construct(IL10N $l10n, ILogger $logger, $userId, FileMapper $fileMapper, ShareMapper $shareMapper, FileUtil $fileUtil) {
67 19
        $this->l10n = $l10n;
68 19
        $this->logger = $logger;
69 19
        $this->userId = $userId;
70 19
        $this->fileMapper = $fileMapper;
71 19
        $this->shareMapper = $shareMapper;
72 19
        $this->fileUtil = $fileUtil;
73 19
    }
74
75
    /**
76
     * Checks if shared with the process initiator
77
     * 
78
     * @param File $fileInfo            
79
     * @return boolean
80
     */
81 2
    public function checkSharedWithInitiator($fileInfo) {
82 2
        $owner = str_replace('home::', '', $fileInfo->getStoragename());
83 2
        if ($this->userId === $owner) {
84
            // user is owner (no shared file)
85 1
            return false;
86
        } else {
87
            // user is not owner (shared file)
88 1
            return true;
89
        }
90
    }
91
92
    /**
93
     * Builds the target name.
94
     * 
95
     * @param File $fileInfo            
96
     * @param boolean $shared            
97
     * @return string
98
     */
99 4
    public function buildTarget($fileInfo, $shared) {
100 4
        if ($shared) {
101 2
            $target = $this->buildTargetForShared($fileInfo);
102
        } else {
103 2
            $target = $this->buildTargetNotForShared($fileInfo);
104
        }
105 4
        return $target;
106
    }
107
108
    /**
109
     * Builds the source name.
110
     * 
111
     * @param File $fileInfo            
112
     * @param boolean $shared            
113
     * @return string
114
     */
115 2
    public function buildSource($fileInfo, $shared) {
116 2
        $source = $fileInfo->getPath();
117 2
        if ($shared) {
118 1
            $source = str_replace('home::', '', $fileInfo->getStoragename()) . '/' . $source;
119
        } else {
120 1
            $source = $this->userId . '/' . $source;
121
        }
122 2
        return $source;
123
    }
124
125
    /**
126
     * Returns the fileInfo for each file in files and checks
127
     * if it has a allowed MIME type and some other conditions.
128
     * 
129
     * @param array $files            
130
     * @return File[]
131
     * @throws NotFoundException
132
     */
133 4
    public function buildFileInfo($files) {
134 4
        $fileArray = array();
135 4
        foreach ($files as $file) {
136
            // Check if anything is missing and file type is correct
137 4
            if (!empty($file['id'])) {
138 3
                $fileInfo = $this->fileMapper->find($file['id']);
139 3
                $this->checkMimeType($fileInfo);
140 2
                array_push($fileArray, $fileInfo);
141
            } else {
142 3
                throw new NotFoundException($this->l10n->t('Wrong parameter.'));
143
            }
144
        }
145 1
        return $fileArray;
146
    }
147
148
    /**
149
     * Determines the correct type for the ocr process worker.
150
     * 
151
     * @param File $fileInfo            
152
     * @return integer
153
     */
154 2
    public function getCorrectType($fileInfo) {
155 2
        if ($fileInfo->getMimetype() === OcrConstants::MIME_TYPE_PDF) {
156 1
            return OcrConstants::OCRmyPDF;
157
        } else {
158 1
            return OcrConstants::TESSERACT;
159
        }
160
    }
161
162
    /**
163
     * Returns a not existing file name for pdf or image processing.
164
     * 
165
     * @param File $fileInfo            
166
     * @return string
167
     */
168 2
    private function buildTargetForShared(File $fileInfo) {
169 2
        $share = $this->shareMapper->find($fileInfo->getFileid(), $this->userId, 
170 2
                str_replace('home::', '', $fileInfo->getStoragename()));
171
        // get rid of the .png or .pdf and so on
172
        // '/thedom.png' => '/thedom' || '/Test/thedom.png' => '/Test/thedom'
1 ignored issue
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
173 2
        $fileName = substr($share->getFileTarget(), 0, (strrpos($share->getFileTarget(), '.')));
174
        // remove everything in front of and including of the first appearance of a slash from behind
175
        // '/thedom' => 'thedom' || '/Test/thedom' => 'thedom'
1 ignored issue
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
176 2
        $fileName = substr(strrchr($fileName, "/"), 1);
177
        // eliminate the file name from the path
178
        // '/thedom.png' => '/' || '/Test/thedom.png' => '/Test'
1 ignored issue
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
179 2
        $filePath = dirname($share->getFileTarget());
180
        // replace the first slash
181 2
        $pos = strpos($filePath, '/');
182 2
        if ($pos !== false) {
183
            // '/' => '' || '/Test/' => 'Test'
1 ignored issue
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
184 2
            $filePath = substr_replace($filePath, '', $pos, strlen('/'));
185
        }
186 2 View Code Duplication
        if ($fileInfo->getMimetype() === OcrConstants::MIME_TYPE_PDF) {
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...
187
            // PDFs:
188 1
            return $this->fileUtil->buildNotExistingFilename($filePath, $fileName . '_OCR.pdf');
189
        } else {
190
            // IMAGES:
191 1
            return $this->fileUtil->buildNotExistingFilename($filePath, $fileName . '_OCR.txt');
192
        }
193
    }
194
195
    /**
196
     * Returns a not existing file name for PDF or image processing.
197
     * 
198
     * @param File $fileInfo            
199
     * @return string
200
     */
201 2
    private function buildTargetNotForShared(File $fileInfo) {
202
        // get rid of the .png or .pdf and so on
203
        // 'thedom.png' => 'thedom'
1 ignored issue
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
204 2
        $fileName = substr($fileInfo->getName(), 0, (strrpos($fileInfo->getName(), '.'))); 
0 ignored issues
show
Bug introduced by
Consider using $fileInfo->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
205
        // eliminate the file name from the path
206
        // 'files/Test/thedom.png' => 'files/Test/' || 'files/thedom.png' => 'files/'
1 ignored issue
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
207 2
        $filePath = str_replace($fileInfo->getName(), '', $fileInfo->getPath()); 
0 ignored issues
show
Bug introduced by
Consider using $fileInfo->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
208
        // and get the path on top of the files/ dir
209
        // 'files/Test/' => '/Test/' || 'files/' => '/'
1 ignored issue
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
210 2
        $filePath = str_replace('files', '', $filePath);
211
        // remove the last slash
212
        // '/Test/' => '/Test' || '/' => ''
1 ignored issue
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
213 2
        $filePath = substr_replace($filePath, '', strrpos($filePath, '/'), strlen('/'));
214
        // replace the first slash
215 2
        $pos = strpos($filePath, '/');
216 2
        if ($pos !== false) {
217
            // '/Test' => '// 'Test' || '/' => ''
218 2
            $filePath = substr_replace($filePath, '', $pos, strlen('/'));
219
        }
220 2 View Code Duplication
        if ($fileInfo->getMimetype() === OcrConstants::MIME_TYPE_PDF) {
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...
221
            // PDFs:
222 1
            return $this->fileUtil->buildNotExistingFilename($filePath, $fileName . '_OCR.pdf');
223
        } else {
224
            // IMAGES:
225 1
            return $this->fileUtil->buildNotExistingFilename($filePath, $fileName . '_OCR.txt');
226
        }
227
    }
228
229
    /**
230
     * Checks a MIME type for a specifically given FileInfo.
231
     * 
232
     * @param File $fileInfo            
233
     */
234 3
    private function checkMimeType(File $fileInfo) {
235 3
        if (!$fileInfo || !in_array($fileInfo->getMimetype(), OcrConstants::ALLOWED_MIME_TYPES)) {
236 1
            $this->logger->debug('Getting FileInfo did not work or not included in the ALLOWED_MIMETYPES array.');
237 1
            throw new NotFoundException($this->l10n->t('Wrong MIME type.'));
238
        }
239
    }
240
}