Skip to content

Commit 40ee5c1

Browse files
author
AliGhaleyan
committed
Bug - get correct file extension and limit for generate random name.
1 parent c6b3d76 commit 40ee5c1

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/BaseType.php

+23-5
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,17 @@ public function fetchProperties(array $config = null)
128128
* @param int $length
129129
* @return string
130130
*/
131-
protected function generateRandomName(int $length = 15)
131+
protected function generateRandomName(int $length = 20)
132132
{
133+
$counter = 1;
133134
do {
134-
$randomName = Str::random($length);
135+
$randomName = str_replace('.', '', Str::random($length));
135136
$check = File::query()
136137
->where("name", $randomName)
137138
->first();
139+
if ($counter > 5)
140+
throw new \Exception("Too much effort to select the file name.");
141+
$counter++;
138142
} while (!empty($check));
139143

140144
return $randomName;
@@ -150,6 +154,7 @@ protected function generateRandomName(int $length = 15)
150154
public function useFileNameToUpload($status = true)
151155
{
152156
$this->useFileNameToUpload = $status;
157+
153158
return $this;
154159
}
155160

@@ -164,9 +169,9 @@ public function useFileNameToUpload($status = true)
164169
*/
165170
public function upload($file)
166171
{
167-
$nameSplit = explode('.', $file->getClientOriginalName());
168-
$fileName = $nameSplit[0];
169-
$format = $nameSplit[1];
172+
$fileInfo = $file->getClientOriginalName();
173+
$fileName = pathinfo($fileInfo, PATHINFO_FILENAME);
174+
$format = $file->getClientOriginalExtension();
170175
if (!$this->getName()) {
171176
if ($this->useFileNameToUpload) {
172177
$this->setName($fileName);
@@ -198,6 +203,7 @@ protected function createFileRow($name = null)
198203
"private" => $this->public ? false : true,
199204
]);
200205
$this->setFile($file);
206+
201207
return $file;
202208
}
203209

@@ -232,6 +238,7 @@ public function getFile($name = null)
232238
public function setFile(File $file)
233239
{
234240
$this->file = $file;
241+
235242
return $this;
236243
}
237244

@@ -279,6 +286,7 @@ abstract protected function handleDelete(File $file);
279286
public function setConfig(array $config)
280287
{
281288
$this->config = $config;
289+
282290
return $this;
283291
}
284292

@@ -302,6 +310,7 @@ public function getConfig($name = null)
302310
if (is_null($name)) return $config;
303311
$find = $config[$name];
304312
if (!isset($find)) return false;
313+
305314
return $find;
306315
}
307316

@@ -316,6 +325,7 @@ public function getConfig($name = null)
316325
public function setType($type)
317326
{
318327
$this->type = $type;
328+
319329
return $this;
320330
}
321331

@@ -340,6 +350,7 @@ public function getType()
340350
public function setPath($path)
341351
{
342352
$this->path = $path;
353+
343354
return $this;
344355
}
345356

@@ -403,6 +414,7 @@ public function getUploadPath()
403414
public function dateTimePrefix($value = true)
404415
{
405416
$this->dateTimePrefix = $value;
417+
406418
return $this;
407419
}
408420

@@ -427,6 +439,7 @@ public function getPrefix()
427439
public function setPrefix($prefix)
428440
{
429441
$this->prefix = $prefix;
442+
430443
return $this;
431444
}
432445

@@ -444,6 +457,7 @@ protected function getStorageFolder($src)
444457
return storage_path($src);
445458
if ($this->storageFolder == "public")
446459
return public_path($src);
460+
447461
return public_path($src);
448462
}
449463

@@ -457,6 +471,7 @@ protected function getStorageFolder($src)
457471
public function setName(string $name)
458472
{
459473
$this->name = $name;
474+
460475
return $this;
461476
}
462477

@@ -482,6 +497,7 @@ public function getName()
482497
public function setFormat(string $format)
483498
{
484499
$this->format = $format;
500+
485501
return $this;
486502
}
487503

@@ -508,6 +524,7 @@ public function isPrivate()
508524
{
509525
$this->storageFolder = "storage";
510526
$this->public = false;
527+
511528
return $this;
512529
}
513530

@@ -523,6 +540,7 @@ public function isPublic()
523540
{
524541
$this->storageFolder = "public";
525542
$this->public = true;
543+
526544
return $this;
527545
}
528546

0 commit comments

Comments
 (0)