Completed
Push — master ( fcfab9...3a1193 )
by Jan
03:55
created

AttachmentHelper::placeholderToRealPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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\PartAttachment;
37
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
38
use Symfony\Component\Filesystem\Filesystem;
39
use Symfony\Component\HttpFoundation\File\File;
40
use Symfony\Component\HttpFoundation\File\UploadedFile;
41
use Symfony\Component\HttpKernel\KernelInterface;
42
43
class AttachmentHelper
44
{
45
    /**
46
     * @var string The folder where the attachments are saved. By default this is data/media in the project root string
47
     */
48
    protected $base_path;
49
50
    public function __construct(ParameterBagInterface $params, KernelInterface $kernel)
51
    {
52
        $tmp_base_path = $params->get('media_directory');
53
54
        $fs = new Filesystem();
55
56
        //Determine if it is an absolute path, or if we need to create a real absolute one out of it
57
        if ($fs->isAbsolutePath($tmp_base_path)) {
58
            $this->base_path = $tmp_base_path;
59
        } else {
60
            $this->base_path = realpath($kernel->getProjectDir() . DIRECTORY_SEPARATOR . $tmp_base_path);
61
        }
62
    }
63
64
    /**
65
     * Converts an relative placeholder filepath (with %MEDIA% or older %BASE%) to an absolute filepath on disk.
66
     * @param string $placeholder_path The filepath with placeholder for which the real path should be determined.
67
     * @return string The absolute real path of the file
68
     */
69
    protected function placeholderToRealPath(string $placeholder_path) : string
70
    {
71
        //The new attachments use %MEDIA% as placeholders, which is the directory set in media_directory
72
        $placeholder_path = str_replace("%MEDIA%", $this->base_path, $placeholder_path);
73
74
        //Older path entries are given via %BASE% which was the project root
75
        $placeholder_path = str_replace("%BASE%/data/media", $this->base_path, $placeholder_path);
76
77
        return $placeholder_path;
78
    }
79
80
    /**
81
     * Converts an real absolute filepath to a placeholder version.
82
     * @param string $real_path The absolute path, for which the placeholder version should be generated.
83
     * @param bool $old_version By default the %MEDIA% placeholder is used, which is directly replaced with the
84
     * media directory. If set to true, the old version with %BASE% will be used, which is the project directory.
85
     * @return string The placeholder version of the filepath
86
     */
87
    protected function realPathToPlaceholder(string $real_path, bool $old_version = false) : string
88
    {
89
        if ($old_version) {
90
            return str_replace($this->base_path, "%BASE%/data/media", $real_path);
91
        }
92
93
        return str_replace($this->base_path, "%MEDIA%", $real_path);
94
    }
95
96
    /**
97
     * Returns the absolute filepath of the attachment. Null is returned, if the attachment is externally saved.
98
     * @param Attachment $attachment The attachment for which the filepath should be determined
99
     * @return string|null
100
     */
101
    public function toAbsoluteFilePath(Attachment $attachment): ?string
102
    {
103
        if ($attachment->isExternal()) {
104
            return null;
105
        }
106
107
        $path = $attachment->getPath();
108
        $path = $this->placeholderToRealPath($path);
109
        return realpath($path);
110
    }
111
112
    /**
113
     * Checks if the file in this attachement is existing. This works for files on the HDD, and for URLs
114
     * (it's not checked if the ressource behind the URL is really existing, so for every external attachment true is returned).
115
     *
116
     * @param Attachment $attachment The attachment for which the existence should be checked
117
     *
118
     * @return bool True if the file is existing.
119
     */
120
    public function isFileExisting(Attachment $attachment): bool
121
    {
122
        return file_exists($this->toAbsoluteFilePath($attachment)) || $attachment->isExternal();
123
    }
124
125
    /**
126
     * Returns the filesize of the attachments in bytes.
127
     * For external attachments, null is returned.
128
     *
129
     * @param Attachment $attachment The filesize for which the filesize should be calculated.
130
     * @return int|null
131
     */
132
    public function getFileSize(Attachment $attachment): ?int
133
    {
134
        if ($attachment->isExternal()) {
135
            return null;
136
        }
137
138
        return filesize($this->toAbsoluteFilePath($attachment));
139
    }
140
141
    /**
142
     * Returns a human readable version of the attachment file size.
143
     * For external attachments, null is returned.
144
     *
145
     * @param Attachment $attachment
146
     * @param int $decimals The number of decimals numbers that should be printed
147
     * @return string|null A string like 1.3M
148
     */
149
    public function getHumanFileSize(Attachment $attachment, $decimals = 2): ?string
150
    {
151
        $bytes = $this->getFileSize($attachment);
152
153
        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...
154
            return null;
155
        }
156
157
        //Format filesize for human reading
158
        //Taken from: https://www.php.net/manual/de/function.filesize.php#106569 and slightly modified
159
160
        $sz = 'BKMGTP';
161
        $factor = (int) floor((strlen($bytes) - 1) / 3);
162
        return sprintf("%.{$decimals}f", $bytes / 1024 ** $factor) . @$sz[$factor];
163
    }
164
165
    /**
166
     * Generate a path to a folder, where this attachment can save its file.
167
     * @param Attachment $attachment The attachment for which the folder should be generated
168
     * @return string The path to the folder (without trailing slash)
169
     */
170
    public function generateFolderForAttachment(Attachment $attachment) : string
171
    {
172
        $mapping = [PartAttachment::class => 'part'];
173
174
        $path = $this->base_path . DIRECTORY_SEPARATOR . $mapping[get_class($attachment)] . DIRECTORY_SEPARATOR . $attachment->getElement()->getID();
175
        return $path;
176
    }
177
178
    /**
179
     * Moves the given uploaded file to a permanent place and saves it into the attachment
180
     * @param Attachment $attachment The attachment in which the file should be saved
181
     * @param UploadedFile|null $file The file which was uploaded
182
     * @return Attachment The attachment with the new filepath
183
     */
184
    public function upload(Attachment $attachment, ?UploadedFile $file) : Attachment
185
    {
186
        //If file is null, do nothing (helpful, so we dont have to check if the file was reuploaded in controller)
187
        if (!$file) {
188
            return $attachment;
189
        }
190
191
        $folder = $this->generateFolderForAttachment($attachment);
192
193
        //Sanatize filename
194
        $originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
195
        $safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
196
        $newFilename = $safeFilename . '.' . $file->getClientOriginalExtension();
197
198
        //If a file with this name is already existing add a number to the filename
199
        if (file_exists($folder . DIRECTORY_SEPARATOR . $newFilename)) {
200
            $bak = $newFilename;
0 ignored issues
show
Unused Code introduced by
The assignment to $bak is dead and can be removed.
Loading history...
201
202
            $number = 1;
203
            $newFilename = $folder . DIRECTORY_SEPARATOR . $safeFilename . '-' . $number . '.' . $file->getClientOriginalExtension();
204
            while (file_exists($newFilename)) {
205
                $number++;
206
                $newFilename = $folder . DIRECTORY_SEPARATOR . $safeFilename . '-' . $number . '.' . $file->getClientOriginalExtension();
207
            }
208
        }
209
210
        //Move our temporay attachment to its final location
211
        $file_path = $file->move($folder, $newFilename)->getRealPath();
212
213
        //Make our file path relative to %BASE%
214
        $file_path = $this->realPathToPlaceholder($file_path);
215
216
        //Save the path to the attachment
217
        $attachment->setPath($file_path);
218
219
        return $attachment;
220
    }
221
222
}