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
f5001e8d
Unverified
Commit
f5001e8d
authored
Oct 09, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding login to move folders to new locations
parent
b99e2af8
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
211 additions
and
0 deletions
+211
-0
FolderStartedMoving.php
Modules/Media/Events/FolderStartedMoving.php
+23
-0
MoveFolderOnDisk.php
Modules/Media/Events/Handlers/MoveFolderOnDisk.php
+87
-0
MediaServiceProvider.php
Modules/Media/Providers/MediaServiceProvider.php
+3
-0
EloquentFolderRepository.php
.../Media/Repositories/Eloquent/EloquentFolderRepository.php
+23
-0
FolderRepository.php
Modules/Media/Repositories/FolderRepository.php
+2
-0
EloquentFolderRepositoryTest.php
Modules/Media/Tests/EloquentFolderRepositoryTest.php
+73
-0
No files found.
Modules/Media/Events/FolderStartedMoving.php
0 → 100644
View file @
f5001e8d
<?php
namespace
Modules\Media\Events
;
use
Modules\Media\Entities\File
;
class
FolderStartedMoving
{
/**
* @var File
*/
public
$folder
;
/**
* @var array
*/
public
$previousData
;
public
function
__construct
(
File
$folder
,
array
$previousData
)
{
$this
->
folder
=
$folder
;
$this
->
previousData
=
$previousData
;
}
}
Modules/Media/Events/Handlers/MoveFolderOnDisk.php
0 → 100644
View file @
f5001e8d
<?php
namespace
Modules\Media\Events\Handlers
;
use
Illuminate\Contracts\Filesystem\Factory
;
use
Modules\Media\Events\FolderStartedMoving
;
use
Modules\Media\Repositories\FileRepository
;
class
MoveFolderOnDisk
{
/**
* @var Factory
*/
private
$filesystem
;
/**
* @var FileRepository
*/
private
$file
;
public
function
__construct
(
Factory
$filesystem
,
FileRepository
$file
)
{
$this
->
filesystem
=
$filesystem
;
$this
->
file
=
$file
;
}
public
function
handle
(
FolderStartedMoving
$event
)
{
$this
->
moveOriginal
(
$event
);
$this
->
renameDatabaseReferences
(
$event
);
}
private
function
moveOriginal
(
FolderStartedMoving
$event
)
{
$this
->
move
(
$event
->
previousData
[
'path'
]
->
getRelativeUrl
(),
$event
->
folder
->
path
->
getRelativeUrl
());
}
private
function
renameDatabaseReferences
(
FolderStartedMoving
$event
)
{
$previousPath
=
$event
->
previousData
[
'path'
]
->
getRelativeUrl
();
$newPath
=
$event
->
folder
->
path
->
getRelativeUrl
();
$this
->
replacePathReferences
(
$event
->
folder
->
id
,
$previousPath
,
$newPath
);
}
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
);
}
}
}
private
function
move
(
$fromPath
,
$toPath
)
{
$this
->
filesystem
->
disk
(
$this
->
getConfiguredFilesystem
())
->
move
(
$this
->
getDestinationPath
(
$fromPath
),
$this
->
getDestinationPath
(
$toPath
)
);
}
private
function
getDestinationPath
(
$path
)
:
string
{
if
(
$this
->
getConfiguredFilesystem
()
===
'local'
)
{
return
basename
(
public_path
())
.
$path
;
}
return
$path
;
}
/**
* @return string
*/
private
function
getConfiguredFilesystem
()
:
string
{
return
config
(
'asgard.media.config.filesystem'
);
}
}
Modules/Media/Providers/MediaServiceProvider.php
View file @
f5001e8d
...
...
@@ -16,6 +16,7 @@ use Modules\Media\Contracts\StoringMedia;
use
Modules\Media\Entities\File
;
use
Modules\Media\Events\FileStartedMoving
;
use
Modules\Media\Events\FolderIsDeleting
;
use
Modules\Media\Events\FolderStartedMoving
;
use
Modules\Media\Events\FolderWasCreated
;
use
Modules\Media\Events\FolderWasUpdated
;
use
Modules\Media\Events\Handlers\CreateFolderOnDisk
;
...
...
@@ -23,6 +24,7 @@ use Modules\Media\Events\Handlers\DeleteAllChildrenOfFolder;
use
Modules\Media\Events\Handlers\DeleteFolderOnDisk
;
use
Modules\Media\Events\Handlers\HandleMediaStorage
;
use
Modules\Media\Events\Handlers\MoveFileOnDisk
;
use
Modules\Media\Events\Handlers\MoveFolderOnDisk
;
use
Modules\Media\Events\Handlers\RegisterMediaSidebar
;
use
Modules\Media\Events\Handlers\RemovePolymorphicLink
;
use
Modules\Media\Events\Handlers\RenameFolderOnDisk
;
...
...
@@ -83,6 +85,7 @@ class MediaServiceProvider extends ServiceProvider
$events
->
listen
(
FolderIsDeleting
::
class
,
DeleteFolderOnDisk
::
class
);
$events
->
listen
(
FolderIsDeleting
::
class
,
DeleteAllChildrenOfFolder
::
class
);
$events
->
listen
(
FileStartedMoving
::
class
,
MoveFileOnDisk
::
class
);
$events
->
listen
(
FolderStartedMoving
::
class
,
MoveFolderOnDisk
::
class
);
$this
->
app
[
TagManager
::
class
]
->
registerNamespace
(
new
File
());
$this
->
registerThumbnails
();
...
...
Modules/Media/Repositories/Eloquent/EloquentFolderRepository.php
View file @
f5001e8d
...
...
@@ -8,6 +8,7 @@ use Modules\Media\Entities\File;
use
Modules\Media\Events\FolderIsCreating
;
use
Modules\Media\Events\FolderIsDeleting
;
use
Modules\Media\Events\FolderIsUpdating
;
use
Modules\Media\Events\FolderStartedMoving
;
use
Modules\Media\Events\FolderWasCreated
;
use
Modules\Media\Events\FolderWasUpdated
;
use
Modules\Media\Repositories\FolderRepository
;
...
...
@@ -89,6 +90,28 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
return
new
NestedFoldersCollection
(
$this
->
all
());
}
public
function
move
(
File
$folder
,
File
$destination
)
:
File
{
$previousData
=
[
'filename'
=>
$folder
->
filename
,
'path'
=>
$folder
->
path
,
];
$folder
->
update
([
'path'
=>
$this
->
getNewPathFor
(
$folder
->
filename
,
$destination
),
'folder_id'
=>
$destination
->
id
,
]);
event
(
new
FolderStartedMoving
(
$folder
,
$previousData
));
return
$folder
;
}
private
function
getNewPathFor
(
string
$filename
,
File
$folder
)
{
return
$folder
->
path
->
getRelativeUrl
()
.
'/'
.
str_slug
(
$filename
);
}
/**
* @param array $data
* @return string
...
...
Modules/Media/Repositories/FolderRepository.php
View file @
f5001e8d
...
...
@@ -23,4 +23,6 @@ interface FolderRepository extends BaseRepository
public
function
allChildrenOf
(
File
$folder
);
public
function
allNested
()
:
NestedFoldersCollection
;
public
function
move
(
File
$folder
,
File
$destination
)
:
File
;
}
Modules/Media/Tests/EloquentFolderRepositoryTest.php
View file @
f5001e8d
...
...
@@ -330,6 +330,79 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
$this
->
assertCount
(
2
,
File
::
all
());
}
/** @test */
public
function
it_can_move_folder_in_database
()
{
$folder
=
$this
->
folder
->
create
([
'name'
=>
'My Folder'
]);
$folderTwo
=
$this
->
folder
->
create
([
'name'
=>
'Future Child folder'
]);
$this
->
assertEquals
(
'/assets/media/future-child-folder'
,
$folderTwo
->
path
->
getRelativeUrl
());
$this
->
folder
->
move
(
$folderTwo
,
$folder
);
$this
->
assertEquals
(
'/assets/media/my-folder/future-child-folder'
,
$folderTwo
->
path
->
getRelativeUrl
());
}
/** @test */
public
function
it_can_move_folder_on_disk
()
{
$folder
=
$this
->
folder
->
create
([
'name'
=>
'My Folder'
]);
$folderTwo
=
$this
->
folder
->
create
([
'name'
=>
'Future Child folder'
]);
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'/assets/media/future-child-folder'
)));
$this
->
folder
->
move
(
$folderTwo
,
$folder
);
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'/assets/media/my-folder/future-child-folder'
)),
'Folder was not moved'
);
}
/** @test */
public
function
it_can_move_folder_with_folders_and_files_in_it_database
()
{
$mainFolder
=
$this
->
folder
->
create
([
'name'
=>
'My Folder'
]);
$folderTwo
=
$this
->
folder
->
create
([
'name'
=>
'Second folder'
]);
$folderThird
=
$this
->
folder
->
create
([
'name'
=>
'Third folder'
,
'parent_id'
=>
$folderTwo
->
id
]);
$file
=
app
(
FileService
::
class
)
->
store
(
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-file.jpg'
),
$folderTwo
->
id
);
$fileTwo
=
app
(
FileService
::
class
)
->
store
(
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-other-file.jpg'
),
$folderThird
->
id
);
$this
->
assertEquals
(
'/assets/media/second-folder'
,
$folderTwo
->
path
->
getRelativeUrl
());
$this
->
assertEquals
(
'/assets/media/second-folder/third-folder'
,
$folderThird
->
path
->
getRelativeUrl
());
$this
->
assertEquals
(
'/assets/media/second-folder/my-file.jpg'
,
$file
->
path
->
getRelativeUrl
());
$this
->
assertEquals
(
'/assets/media/second-folder/third-folder/my-other-file.jpg'
,
$fileTwo
->
path
->
getRelativeUrl
());
$this
->
folder
->
move
(
$folderTwo
,
$mainFolder
);
$folderTwo
->
refresh
();
$folderThird
->
refresh
();
$file
->
refresh
();
$fileTwo
->
refresh
();
$this
->
assertEquals
(
'/assets/media/my-folder/second-folder'
,
$folderTwo
->
path
->
getRelativeUrl
());
$this
->
assertEquals
(
'/assets/media/my-folder/second-folder/third-folder'
,
$folderThird
->
path
->
getRelativeUrl
());
$this
->
assertEquals
(
'/assets/media/my-folder/second-folder/my-file.jpg'
,
$file
->
path
->
getRelativeUrl
());
$this
->
assertEquals
(
'/assets/media/my-folder/second-folder/third-folder/my-other-file.jpg'
,
$fileTwo
->
path
->
getRelativeUrl
());
}
/** @test */
public
function
it_can_move_folder_with_folders_and_files_in_it_disk
()
{
$mainFolder
=
$this
->
folder
->
create
([
'name'
=>
'My Folder'
]);
$folderTwo
=
$this
->
folder
->
create
([
'name'
=>
'Second folder'
]);
$folderThird
=
$this
->
folder
->
create
([
'name'
=>
'Third folder'
,
'parent_id'
=>
$folderTwo
->
id
]);
$file
=
app
(
FileService
::
class
)
->
store
(
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-file.jpg'
),
$folderTwo
->
id
);
$fileTwo
=
app
(
FileService
::
class
)
->
store
(
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-other-file.jpg'
),
$folderThird
->
id
);
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'/assets/media/second-folder'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'/assets/media/second-folder/third-folder'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'/assets/media/second-folder/my-file.jpg'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'/assets/media/second-folder/third-folder/my-other-file.jpg'
)));
$this
->
folder
->
move
(
$folderTwo
,
$mainFolder
);
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'/assets/media/my-folder/second-folder'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'/assets/media/my-folder/second-folder/third-folder'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'/assets/media/my-folder/second-folder/my-file.jpg'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'/assets/media/my-folder/second-folder/third-folder/my-other-file.jpg'
)));
}
private
function
createFile
(
$fileName
=
'random/name.jpg'
)
{
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