1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodeBlog\DataUploader; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class CodeBlog DataUploader |
7
|
|
|
* |
8
|
|
|
* @author Whallysson Avelino <https://github.com/whallysson> |
9
|
|
|
* @package CodeBlog\DataUploader |
10
|
|
|
*/ |
11
|
|
|
abstract class DataUploader |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
protected $path; |
16
|
|
|
|
17
|
|
|
/** @var resource */ |
18
|
|
|
protected $file; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected $name; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
protected $ext; |
25
|
|
|
|
26
|
|
|
/** @var array */ |
27
|
|
|
protected static $allowJPG = []; |
28
|
|
|
|
29
|
|
|
/** @var array */ |
30
|
|
|
protected static $allowPNG = []; |
31
|
|
|
|
32
|
|
|
/** @var array */ |
33
|
|
|
protected static $allowGIF = []; |
34
|
|
|
|
35
|
|
|
/** @var array */ |
36
|
|
|
protected static $allowTypes = []; |
37
|
|
|
|
38
|
|
|
/** @var array */ |
39
|
|
|
protected static $extensions = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $uploadDir |
43
|
|
|
* @param string $fileTypeDir |
44
|
|
|
* @example $uploade = new DataUploader("storage/uploads", "images"); |
45
|
|
|
*/ |
46
|
|
|
public function __construct(string $uploadDir, string $fileTypeDir) |
47
|
|
|
{ |
48
|
|
|
$this->dir($uploadDir); |
49
|
|
|
$this->dir("{$uploadDir}/{$fileTypeDir}"); |
50
|
|
|
$this->path("{$uploadDir}/{$fileTypeDir}"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public static function isAllowed() |
57
|
|
|
{ |
58
|
|
|
return array_merge(static::$allowJPG, static::$allowPNG, static::$allowGIF, static::$allowTypes); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public static function isExtension(): array |
65
|
|
|
{ |
66
|
|
|
return static::$extensions; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $name |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
protected function name(string $name) |
74
|
|
|
{ |
75
|
|
|
$name = filter_var(mb_strtolower($name), FILTER_SANITIZE_STRIPPED); |
76
|
|
|
$formats = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜüÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿRr"!@#$%&*()_-+={[}]/?;:.,\\\'<>°ºª¹²³'; |
77
|
|
|
$replace = 'aaaaaaaceeeeiiiidnoooooouuuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr '; |
78
|
|
|
$name = str_replace(["-----", "----", "---", "--"], "-", |
79
|
|
|
str_replace(" ", "-", trim(strtr(utf8_decode($name), utf8_decode($formats), $replace)))); |
80
|
|
|
|
81
|
|
|
$this->name = "{$name}." . $this->ext; |
82
|
|
|
|
83
|
|
|
if (file_exists("{$this->path}/{$this->name}") && is_file("{$this->path}/{$this->name}")) { |
84
|
|
|
$this->name = "{$name}-" . time() . ".{$this->ext}"; |
85
|
|
|
} |
86
|
|
|
return $this->name; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $dir |
91
|
|
|
* @param int $mode |
92
|
|
|
*/ |
93
|
|
|
protected function dir(string $dir, int $mode = 0755) |
94
|
|
|
{ |
95
|
|
|
if (!file_exists($dir) || !is_dir($dir)) { |
96
|
|
|
mkdir($dir, $mode); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $path |
102
|
|
|
*/ |
103
|
|
|
protected function path(string $path) |
104
|
|
|
{ |
105
|
|
|
list($yearPath, $mothPath) = explode("/", date("Y/m")); |
106
|
|
|
|
107
|
|
|
$this->dir("{$path}/{$yearPath}"); |
108
|
|
|
$this->dir("{$path}/{$yearPath}/{$mothPath}"); |
109
|
|
|
$this->path = "{$path}/{$yearPath}/{$mothPath}"; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|