Completed
Push — master ( 1f7c12...27a001 )
by Jan
04:25
created

AttachmentHelper::upload()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 34
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
c 3
b 0
f 0
nc 4
nop 3
dl 0
loc 34
rs 8.8333
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\Services;
33
34
35
use App\Entity\Attachments\Attachment;
36
use App\Entity\Attachments\AttachmentContainingDBElement;
37
use App\Entity\Attachments\AttachmentTypeAttachment;
38
use App\Entity\Attachments\CategoryAttachment;
39
use App\Entity\Attachments\CurrencyAttachment;
40
use App\Entity\Attachments\DeviceAttachment;
41
use App\Entity\Attachments\FootprintAttachment;
42
use App\Entity\Attachments\GroupAttachment;
43
use App\Entity\Attachments\ManufacturerAttachment;
44
use App\Entity\Attachments\MeasurementUnitAttachment;
45
use App\Entity\Attachments\PartAttachment;
46
use App\Entity\Attachments\StorelocationAttachment;
47
use App\Entity\Attachments\SupplierAttachment;
48
use App\Entity\Attachments\UserAttachment;
49
use App\Services\Attachments\AttachmentPathResolver;
50
use Symfony\Component\HttpFoundation\File\UploadedFile;
51
use Symfony\Component\OptionsResolver\OptionsResolver;
52
53
class AttachmentHelper
54
{
55
56
    protected $pathResolver;
57
58
    public function __construct(AttachmentPathResolver $pathResolver)
59
    {
60
        $this->pathResolver = $pathResolver;
61
    }
62
63
    /**
64
     * Gets an SPLFileInfo object representing the file associated with the attachment.
65
     * @param Attachment $attachment The attachment for which the file should be generated
66
     * @return \SplFileInfo|null The fileinfo for the attachment file. Null, if the attachment is external or has
67
     * invalid file.
68
     */
69
    public function attachmentToFile(Attachment $attachment) : ?\SplFileInfo
70
    {
71
        if ($attachment->isExternal() || !$this->isFileExisting($attachment)) {
72
            return null;
73
        }
74
75
        return new \SplFileInfo($this->toAbsoluteFilePath($attachment));
76
    }
77
78
    /**
79
     * Returns the absolute filepath of the attachment. Null is returned, if the attachment is externally saved,
80
     * or is not existing.
81
     * @param Attachment $attachment The attachment for which the filepath should be determined
82
     * @return string|null
83
     */
84
    public function toAbsoluteFilePath(Attachment $attachment): ?string
85
    {
86
        if (empty($attachment->getPath())) {
87
            return null;
88
        }
89
90
        if ($attachment->isExternal()) {
91
            return null;
92
        }
93
94
        $path = $this->pathResolver->placeholderToRealPath($attachment->getPath());
95
96
        //realpath does not work with null as argument
97
        if ($path === null) {
98
            return null;
99
        }
100
101
        $tmp = realpath($path);
102
        //If path is not existing realpath returns false.
103
        if ($tmp === false) {
104
            return null;
105
        }
106
        return $tmp;
107
    }
108
109
    /**
110
     * Checks if the file in this attachement is existing. This works for files on the HDD, and for URLs
111
     * (it's not checked if the ressource behind the URL is really existing, so for every external attachment true is returned).
112
     *
113
     * @param Attachment $attachment The attachment for which the existence should be checked
114
     *
115
     * @return bool True if the file is existing.
116
     */
117
    public function isFileExisting(Attachment $attachment): bool
118
    {
119
        if (empty($attachment->getPath())) {
120
            return false;
121
        }
122
123
        return file_exists($this->toAbsoluteFilePath($attachment)) || $attachment->isExternal();
124
    }
125
126
    /**
127
     * Returns the filesize of the attachments in bytes.
128
     * For external attachments or not existing attachments, null is returned.
129
     *
130
     * @param Attachment $attachment The filesize for which the filesize should be calculated.
131
     * @return int|null
132
     */
133
    public function getFileSize(Attachment $attachment): ?int
134
    {
135
        if ($attachment->isExternal()) {
136
            return null;
137
        }
138
139
        if (!$this->isFileExisting($attachment)) {
140
            return null;
141
        }
142
143
        $tmp = filesize($this->toAbsoluteFilePath($attachment));
144
        return  $tmp !== false ? $tmp : null;
145
    }
146
147
    /**
148
     * Returns a human readable version of the attachment file size.
149
     * For external attachments, null is returned.
150
     *
151
     * @param Attachment $attachment
152
     * @param int $decimals The number of decimals numbers that should be printed
153
     * @return string|null A string like 1.3M
154
     */
155
    public function getHumanFileSize(Attachment $attachment, $decimals = 2): ?string
156
    {
157
        $bytes = $this->getFileSize($attachment);
158
159
        if ($bytes == null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $bytes of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
160
            return null;
161
        }
162
163
        //Format filesize for human reading
164
        //Taken from: https://www.php.net/manual/de/function.filesize.php#106569 and slightly modified
165
166
        $sz = 'BKMGTP';
167
        $factor = (int) floor((strlen($bytes) - 1) / 3);
168
        return sprintf("%.{$decimals}f", $bytes / 1024 ** $factor) . @$sz[$factor];
169
    }
170
171
172
173
174
}