File size: 2,666 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

namespace Mautic\CoreBundle\Helper;

use Mautic\CoreBundle\Exception\FilePathException;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class FilePathResolver
{
    public function __construct(
        private Filesystem $filesystem,
        private InputHelper $inputHelper
    ) {
    }

    /**
     * @param string $uploadDir
     *
     * @throws FilePathException
     */
    public function getUniqueFileName($uploadDir, UploadedFile $file): string
    {
        $inputHelper       = $this->inputHelper;
        $fullName          = $file->getClientOriginalName();
        $fullNameSanitized = $inputHelper::filename($fullName);
        $ext               = $this->getFileExtension($file);
        $baseFileName      = pathinfo($fullNameSanitized, PATHINFO_FILENAME);
        $name              = $baseFileName;
        $filePath          = $this->getFilePath($uploadDir, $baseFileName, $ext);
        $i                 = 1;

        while ($this->filesystem->exists($filePath)) {
            $name     = $baseFileName.'-'.$i;
            $filePath = $this->getFilePath($uploadDir, $name, $ext);
            ++$i;

            if ($i > 1000) {
                throw new FilePathException('Could not generate path');
            }
        }

        return $name.$ext;
    }

    /**
     * @param string $directory
     *
     * @throws FilePathException
     */
    public function createDirectory($directory): void
    {
        if ($this->filesystem->exists($directory)) {
            return;
        }
        try {
            $this->filesystem->mkdir($directory);
        } catch (IOException) {
            throw new FilePathException('Could not create directory');
        }
    }

    /**
     * @param string $path
     */
    public function delete($path): void
    {
        if (!$this->filesystem->exists($path)) {
            return;
        }
        try {
            $this->filesystem->remove($path);
        } catch (IOException) {
        }
    }

    public function move(string $originPath, string $targetPath): void
    {
        $this->filesystem->rename($originPath, $targetPath);
    }

    /**
     * @param string $uploadDir
     * @param string $fileName
     * @param string $ext
     */
    private function getFilePath($uploadDir, $fileName, $ext): string
    {
        return $uploadDir.DIRECTORY_SEPARATOR.$fileName.$ext;
    }

    private function getFileExtension(UploadedFile $file): string
    {
        $ext = $file->getClientOriginalExtension();

        return ('' === $ext ? '' : '.').$ext;
    }
}