Skip to content

Commit 8415f69

Browse files
committed
Create a file with new number if it exists
1 parent 030d5c7 commit 8415f69

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

src/LfmPath.php

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ private function uploadValidator($file)
288288

289289
$new_file_name = $this->getNewName($file);
290290

291-
if ($this->setName($new_file_name)->exists() && !config('lfm.over_write_on_duplicate', false)) {
291+
if ($this->setName($new_file_name)->exists() && config('lfm.rename_file.duplicate', '-%s') === false) {
292292
return $this->error('file-exist');
293293
}
294294

@@ -310,24 +310,54 @@ private function uploadValidator($file)
310310
return true;
311311
}
312312

313-
private function getNewName($file)
313+
/**
314+
* Generate new name for file
315+
*
316+
* @param UploadedFile $file
317+
* @return string
318+
*/
319+
private function getNewName($file): string
314320
{
315-
$new_file_name = $this->helper
316-
->translateFromUtf8(trim(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)));
321+
$name = $this->helper->translateFromUtf8(trim(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)));
317322

318323
if (config('lfm.rename_file.uniqid', false) === true) {
319-
$new_file_name = uniqid();
324+
$name = uniqid();
320325
} elseif (config('lfm.rename_file.slug', true) === true) {
321-
$new_file_name = Str::slug($new_file_name);
326+
$name = Str::slug($name);
322327
}
323328

324-
$extension = $file->getClientOriginalExtension();
329+
return $this->uniquifyName($name, $file->getClientOriginalExtension());
330+
}
325331

326-
if ($extension) {
327-
$new_file_name .= '.' . $extension;
328-
}
329332

330-
return $new_file_name;
333+
/**
334+
* Generate unique name for file if it necessary
335+
*
336+
* @param string $name
337+
* @param string $extension
338+
* @return string
339+
*/
340+
private function uniquifyName(string $name, string $extension): string
341+
{
342+
$duplicate = config('lfm.rename_file.duplicate', '-%s');
343+
$i = 0;
344+
345+
do {
346+
$result = $name;
347+
348+
if ($i && is_string($duplicate)) {
349+
$result .= sprintf($duplicate, $i);
350+
}
351+
352+
// Append the extension if it exists
353+
if ($extension) {
354+
$result .= '.' . $extension;
355+
}
356+
357+
$i++;
358+
} while (is_string($duplicate) ? $this->setName($result)->exists() : false);
359+
360+
return $result;
331361
}
332362

333363
private function saveFile($file, $new_file_name)

src/config/lfm.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@
3838

3939
/*
4040
|--------------------------------------------------------------------------
41-
| Shared folder / Private folder
41+
| Folder privacy
4242
|--------------------------------------------------------------------------
4343
|
4444
| If both options are set to false, then shared folder will be activated.
4545
|
4646
*/
4747

48-
'allow_private_folder' => true,
48+
'allow_private_folder' => false,
4949

5050
// Flexible way to customize client folders accessibility
5151
// If you want to customize client folders, publish tag="lfm_handler"
@@ -123,7 +123,7 @@
123123

124124
/*
125125
|--------------------------------------------------------------------------
126-
| Upload / Validation
126+
| Upload and Validation
127127
|--------------------------------------------------------------------------
128128
|
129129
| We highly recommend you to leave alphanumeric setting by default (true)
@@ -139,44 +139,45 @@
139139
'should_validate_size' => true,
140140
'should_validate_mime' => true,
141141

142-
// behavior on files with identical name
143-
// setting it to true cause old file replace with new one
144-
// setting it to false show `error-file-exist` error and stop upload
145-
'over_write_on_duplicate' => false,
146-
147142
/*
148143
|--------------------------------------------------------------------------
149-
| File naming
144+
| File naming [Upload]
150145
|--------------------------------------------------------------------------
151146
|
152-
| It works only when you're uploading a new file!
147+
| 'uniqid' - Generate a unique ID (length:13) (e.g. 5e94a2653ef6a)
148+
| 'slug' - Generate friendly "slug" ("Te sT 1.txt" => "te-st-1.txt")
153149
|
154-
| rename_file.uniqid - Generate a unique ID (length:13) (e.g 5e94a2653ef6a)
155-
| rename_file.slug - Generate friendly "slug" ("Te sT .txt" => "te-st.txt")
150+
| 'duplicate' - behavior on files with identical name
151+
| '-%s' - create new file with postfix file.txt => file-1.txt, file-2.txt
152+
| where '%s' is number '-1', '-2' will be generated automatically
153+
| true - old file will be replaced with new one
154+
| false - will show `error-file-exist` error and stop upload
156155
|
157-
| Make sure that you have enabled only one feature of rename_file because
158-
| of conflicts
156+
| Note!
157+
| Make sure that you have enabled only one feature of rename_file
158+
| 'uniqid' or 'slug' because of conflicts
159159
|
160160
*/
161161

162162
'rename_file' => [
163163
'uniqid' => false,
164164
'slug' => true,
165+
166+
'duplicate' => '-%s',
165167
],
166168

167169
/*
168170
|--------------------------------------------------------------------------
169-
| Compressing
171+
| Compression
170172
|--------------------------------------------------------------------------
171173
|
172174
| Define the quality of the image. Data ranging from 0
173175
| (poor quality, small file) to 100 (best quality, big file).
174176
| Quality is only applied if you're encoding JPG format
175177
| since PNG compression is lossless and does not affect image quality.
176178
|
177-
| Default: 90
178-
| Recommended: 80-90
179-
| Disable: false
179+
| 90 - default value, recommended value is between 80-90
180+
| false - to disable compression
180181
|
181182
*/
182183

0 commit comments

Comments
 (0)