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
69192d8c
Unverified
Commit
69192d8c
authored
Sep 22, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
It can recursively update folders
parent
1228dd8f
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
79 additions
and
1 deletion
+79
-1
RenameFolderOnDisk.php
Modules/Media/Events/Handlers/RenameFolderOnDisk.php
+37
-1
EloquentFileRepository.php
...es/Media/Repositories/Eloquent/EloquentFileRepository.php
+9
-0
EloquentFolderRepository.php
.../Media/Repositories/Eloquent/EloquentFolderRepository.php
+1
-0
FileRepository.php
Modules/Media/Repositories/FileRepository.php
+7
-0
EloquentFolderRepositoryTest.php
Modules/Media/Tests/EloquentFolderRepositoryTest.php
+25
-0
No files found.
Modules/Media/Events/Handlers/RenameFolderOnDisk.php
View file @
69192d8c
...
@@ -4,6 +4,7 @@ namespace Modules\Media\Events\Handlers;
...
@@ -4,6 +4,7 @@ namespace Modules\Media\Events\Handlers;
use
Illuminate\Contracts\Filesystem\Factory
;
use
Illuminate\Contracts\Filesystem\Factory
;
use
Modules\Media\Events\FolderWasUpdated
;
use
Modules\Media\Events\FolderWasUpdated
;
use
Modules\Media\Repositories\FileRepository
;
class
RenameFolderOnDisk
class
RenameFolderOnDisk
{
{
...
@@ -11,12 +12,32 @@ class RenameFolderOnDisk
...
@@ -11,12 +12,32 @@ class RenameFolderOnDisk
* @var Factory
* @var Factory
*/
*/
private
$filesystem
;
private
$filesystem
;
/**
* @var FileRepository
*/
private
$file
;
public
function
__construct
(
Factory
$filesystem
)
public
function
__construct
(
Factory
$filesystem
,
FileRepository
$file
)
{
{
$this
->
filesystem
=
$filesystem
;
$this
->
filesystem
=
$filesystem
;
$this
->
file
=
$file
;
}
}
public
function
handle
(
FolderWasUpdated
$event
)
public
function
handle
(
FolderWasUpdated
$event
)
{
$this
->
renameFolder
(
$event
);
$this
->
renameDatabaseReferences
(
$event
);
}
private
function
renameDatabaseReferences
(
FolderWasUpdated
$event
)
{
$previousPath
=
$event
->
previousFolderData
[
'path'
]
->
getRelativeUrl
();
$newPath
=
$event
->
folder
->
path
->
getRelativeUrl
();
$this
->
replacePathReferences
(
$event
->
folder
->
id
,
$previousPath
,
$newPath
);
}
private
function
renameFolder
(
$event
)
{
{
$this
->
filesystem
->
disk
(
$this
->
getConfiguredFilesystem
())
$this
->
filesystem
->
disk
(
$this
->
getConfiguredFilesystem
())
->
move
(
->
move
(
...
@@ -41,4 +62,19 @@ class RenameFolderOnDisk
...
@@ -41,4 +62,19 @@ class RenameFolderOnDisk
{
{
return
config
(
'asgard.media.config.filesystem'
);
return
config
(
'asgard.media.config.filesystem'
);
}
}
private
function
replacePathReferences
(
$folderId
,
$previousPath
,
$newPath
)
{
$medias
=
$this
->
file
->
allChildrenOf
(
$folderId
);
foreach
(
$medias
as
$media
)
{
$oldPath
=
$media
->
path
->
getRelativeUrl
();
$media
->
update
([
'path'
=>
str_replace
(
$previousPath
,
$newPath
,
$oldPath
),
]);
if
(
$media
->
isFolder
()
===
true
)
{
$this
->
replacePathReferences
(
$media
->
id
,
$previousPath
,
$newPath
);
}
}
}
}
}
Modules/Media/Repositories/Eloquent/EloquentFileRepository.php
View file @
69192d8c
...
@@ -163,4 +163,13 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
...
@@ -163,4 +163,13 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
return
$media
->
paginate
(
$request
->
get
(
'per_page'
,
10
));
return
$media
->
paginate
(
$request
->
get
(
'per_page'
,
10
));
}
}
/**
* @param int $folderId
* @return Collection
*/
public
function
allChildrenOf
(
int
$folderId
)
:
Collection
{
return
$this
->
model
->
where
(
'folder_id'
,
$folderId
)
->
get
();
}
}
}
Modules/Media/Repositories/Eloquent/EloquentFolderRepository.php
View file @
69192d8c
...
@@ -48,6 +48,7 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
...
@@ -48,6 +48,7 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
'filename'
=>
array_get
(
$data
,
'name'
),
'filename'
=>
array_get
(
$data
,
'name'
),
'path'
=>
$this
->
getPath
(
$data
),
'path'
=>
$this
->
getPath
(
$data
),
];
];
dd
(
$previousData
,
$formattedData
);
event
(
$event
=
new
FolderIsUpdating
(
$formattedData
));
event
(
$event
=
new
FolderIsUpdating
(
$formattedData
));
$model
->
update
(
$event
->
getAttributes
());
$model
->
update
(
$event
->
getAttributes
());
...
...
Modules/Media/Repositories/FileRepository.php
View file @
69192d8c
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
namespace
Modules\Media\Repositories
;
namespace
Modules\Media\Repositories
;
use
Illuminate\Database\Eloquent\Collection
;
use
Illuminate\Http\Request
;
use
Illuminate\Http\Request
;
use
Modules\Core\Repositories\BaseRepository
;
use
Modules\Core\Repositories\BaseRepository
;
use
Symfony\Component\HttpFoundation\File\UploadedFile
;
use
Symfony\Component\HttpFoundation\File\UploadedFile
;
...
@@ -37,4 +38,10 @@ interface FileRepository extends BaseRepository
...
@@ -37,4 +38,10 @@ interface FileRepository extends BaseRepository
* @return mixed
* @return mixed
*/
*/
public
function
serverPaginationFilteringFor
(
Request
$request
);
public
function
serverPaginationFilteringFor
(
Request
$request
);
/**
* @param int $folderId
* @return Collection
*/
public
function
allChildrenOf
(
int
$folderId
)
:
Collection
;
}
}
Modules/Media/Tests/EloquentFolderRepositoryTest.php
View file @
69192d8c
...
@@ -189,4 +189,29 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
...
@@ -189,4 +189,29 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'assets/media/new-name/my-file_smallThumb.jpg'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'assets/media/new-name/my-file_smallThumb.jpg'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'assets/media/new-name/my-file_mediumThumb.jpg'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'assets/media/new-name/my-file_mediumThumb.jpg'
)));
}
}
/** @test */
public
function
it_renames_folder_and_database_references_to_that_folder
()
{
$parentFolder
=
$this
->
folder
->
create
([
'name'
=>
'My Folder'
]);
$folderTwo
=
$this
->
folder
->
create
([
'name'
=>
'Child folder'
,
'parent_id'
=>
$parentFolder
->
id
]);
$file
=
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-file.jpg'
);
$fileTwo
=
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-second-file.jpg'
);
$fileThree
=
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-third-file.jpg'
);
$fileOne
=
app
(
FileService
::
class
)
->
store
(
$file
,
$parentFolder
->
id
);
$fileTwo
=
app
(
FileService
::
class
)
->
store
(
$fileTwo
,
$parentFolder
->
id
);
$fileThree
=
app
(
FileService
::
class
)
->
store
(
$fileThree
,
$folderTwo
->
id
);
$parentFolder
=
$this
->
folder
->
update
(
$parentFolder
,
[
'name'
=>
'New Name!'
]);
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'assets/media/new-name'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'assets/media/new-name/my-file.jpg'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'assets/media/new-name/child-folder/my-third-file.jpg'
)));
$fileOne
->
refresh
();
$fileTwo
->
refresh
();
$fileThree
->
refresh
();
$this
->
assertEquals
(
'/assets/media/new-name/my-file.jpg'
,
$fileOne
->
path
->
getRelativeUrl
());
$this
->
assertEquals
(
'/assets/media/new-name/my-second-file.jpg'
,
$fileTwo
->
path
->
getRelativeUrl
());
$this
->
assertEquals
(
'/assets/media/new-name/child-folder/my-third-file.jpg'
,
$fileThree
->
path
->
getRelativeUrl
());
}
}
}
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