Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Platform
Commits
ebac68ee
Unverified
Commit
ebac68ee
authored
Oct 10, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make sure a file can be stored with the same name in another location, without having a _1 suffix.
parent
5e169555
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
3 deletions
+27
-3
EloquentFileRepository.php
...es/Media/Repositories/Eloquent/EloquentFileRepository.php
+7
-3
EloquentFileRepositoryTest.php
Modules/Media/Tests/EloquentFileRepositoryTest.php
+20
-0
No files found.
Modules/Media/Repositories/Eloquent/EloquentFileRepository.php
View file @
ebac68ee
...
@@ -49,7 +49,7 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
...
@@ -49,7 +49,7 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
$exists
=
$this
->
model
->
whereFilename
(
$fileName
)
->
first
();
$exists
=
$this
->
model
->
whereFilename
(
$fileName
)
->
first
();
if
(
$exists
)
{
if
(
$exists
)
{
$fileName
=
$this
->
getNewUniqueFilename
(
$fileName
);
$fileName
=
$this
->
getNewUniqueFilename
(
$fileName
,
$parentId
);
}
}
$data
=
[
$data
=
[
...
@@ -123,14 +123,15 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
...
@@ -123,14 +123,15 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
/**
/**
* @param $fileName
* @param $fileName
* @param int $parentId
* @return string
* @return string
*/
*/
private
function
getNewUniqueFilename
(
$fileName
)
private
function
getNewUniqueFilename
(
$fileName
,
int
$parentId
=
0
)
{
{
$fileNameOnly
=
pathinfo
(
$fileName
,
PATHINFO_FILENAME
);
$fileNameOnly
=
pathinfo
(
$fileName
,
PATHINFO_FILENAME
);
$extension
=
pathinfo
(
$fileName
,
PATHINFO_EXTENSION
);
$extension
=
pathinfo
(
$fileName
,
PATHINFO_EXTENSION
);
$models
=
$this
->
model
->
where
(
'filename'
,
'LIKE'
,
"
$fileNameOnly
%"
)
->
get
();
$models
=
$this
->
model
->
where
(
'filename'
,
'LIKE'
,
"
$fileNameOnly
%"
)
->
where
(
'folder_id'
,
$parentId
)
->
get
();
$versionCurrent
=
$models
->
reduce
(
function
(
$carry
,
$model
)
{
$versionCurrent
=
$models
->
reduce
(
function
(
$carry
,
$model
)
{
$latestFilename
=
pathinfo
(
$model
->
filename
,
PATHINFO_FILENAME
);
$latestFilename
=
pathinfo
(
$model
->
filename
,
PATHINFO_FILENAME
);
...
@@ -143,6 +144,9 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
...
@@ -143,6 +144,9 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
return
(
$version
>
$carry
)
?
$version
:
$carry
;
return
(
$version
>
$carry
)
?
$version
:
$carry
;
},
0
);
},
0
);
if
(
$versionCurrent
===
0
&&
$models
->
count
()
===
0
)
{
return
$fileName
;
}
return
$fileNameOnly
.
'_'
.
(
$versionCurrent
+
1
)
.
'.'
.
$extension
;
return
$fileNameOnly
.
'_'
.
(
$versionCurrent
+
1
)
.
'.'
.
$extension
;
}
}
...
...
Modules/Media/Tests/EloquentFileRepositoryTest.php
View file @
ebac68ee
...
@@ -343,6 +343,26 @@ class EloquentFileRepositoryTest extends MediaTestCase
...
@@ -343,6 +343,26 @@ class EloquentFileRepositoryTest extends MediaTestCase
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'/assets/media/my-file_mediumThumb.jpg'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'/assets/media/my-file_mediumThumb.jpg'
)));
}
}
/** @test */
public
function
it_can_store_same_filename_in_other_folder_with_no_suffix
()
{
$folderRepository
=
app
(
FolderRepository
::
class
);
$folder
=
$folderRepository
->
create
([
'name'
=>
'My Folder'
,
'parent_id'
=>
0
]);
$file
=
app
(
FileService
::
class
)
->
store
(
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-file.jpg'
),
$folder
->
id
);
$fileTwo
=
app
(
FileService
::
class
)
->
store
(
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-file.jpg'
));
$subFolder
=
$folderRepository
->
create
([
'name'
=>
'My Sub Folder'
,
'parent_id'
=>
$folder
->
id
]);
$fileThree
=
app
(
FileService
::
class
)
->
store
(
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-file.jpg'
),
$subFolder
->
id
);
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'/assets/media/my-file.jpg'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'/assets/media/my-folder/my-file.jpg'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'/assets/media/my-folder/my-sub-folder/my-file.jpg'
)));
$this
->
assertEquals
(
'/assets/media/my-folder/my-file.jpg'
,
$file
->
path
->
getRelativeUrl
());
$this
->
assertEquals
(
'/assets/media/my-file.jpg'
,
$fileTwo
->
path
->
getRelativeUrl
());
$this
->
assertEquals
(
'/assets/media/my-folder/my-sub-folder/my-file.jpg'
,
$fileThree
->
path
->
getRelativeUrl
());
}
private
function
createFile
(
$fileName
=
'random/name.jpg'
)
private
function
createFile
(
$fileName
=
'random/name.jpg'
)
{
{
return
File
::
create
([
return
File
::
create
([
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment