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
0a247740
Unverified
Commit
0a247740
authored
Oct 08, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding logic to move files
parent
ac8e803d
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
123 additions
and
0 deletions
+123
-0
FileStartedMoving.php
Modules/Media/Events/FileStartedMoving.php
+23
-0
MoveFileOnDisk.php
Modules/Media/Events/Handlers/MoveFileOnDisk.php
+45
-0
MediaServiceProvider.php
Modules/Media/Providers/MediaServiceProvider.php
+3
-0
EloquentFileRepository.php
...es/Media/Repositories/Eloquent/EloquentFileRepository.php
+17
-0
FileRepository.php
Modules/Media/Repositories/FileRepository.php
+3
-0
EloquentFileRepositoryTest.php
Modules/Media/Tests/EloquentFileRepositoryTest.php
+32
-0
No files found.
Modules/Media/Events/FileStartedMoving.php
0 → 100644
View file @
0a247740
<?php
namespace
Modules\Media\Events
;
use
Modules\Media\Entities\File
;
class
FileStartedMoving
{
/**
* @var File
*/
public
$file
;
/**
* @var array
*/
public
$previousData
;
public
function
__construct
(
File
$file
,
array
$previousData
)
{
$this
->
file
=
$file
;
$this
->
previousData
=
$previousData
;
}
}
Modules/Media/Events/Handlers/MoveFileOnDisk.php
0 → 100644
View file @
0a247740
<?php
namespace
Modules\Media\Events\Handlers
;
use
Illuminate\Contracts\Filesystem\Factory
;
use
Modules\Media\Events\FileStartedMoving
;
class
MoveFileOnDisk
{
/**
* @var Factory
*/
private
$filesystem
;
public
function
__construct
(
Factory
$filesystem
)
{
$this
->
filesystem
=
$filesystem
;
}
public
function
handle
(
FileStartedMoving
$event
)
{
$this
->
filesystem
->
disk
(
$this
->
getConfiguredFilesystem
())
->
move
(
$this
->
getDestinationPath
(
$event
->
previousData
[
'path'
]
->
getRelativeUrl
()),
$this
->
getDestinationPath
(
$event
->
file
->
path
->
getRelativeUrl
())
);
}
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 @
0a247740
...
...
@@ -20,7 +20,9 @@ use Modules\Media\Events\FolderWasUpdated;
use
Modules\Media\Events\Handlers\CreateFolderOnDisk
;
use
Modules\Media\Events\Handlers\DeleteAllChildrenOfFolder
;
use
Modules\Media\Events\Handlers\DeleteFolderOnDisk
;
use
Modules\Media\Events\FileStartedMoving
;
use
Modules\Media\Events\Handlers\HandleMediaStorage
;
use
Modules\Media\Events\Handlers\MoveFileOnDisk
;
use
Modules\Media\Events\Handlers\RegisterMediaSidebar
;
use
Modules\Media\Events\Handlers\RemovePolymorphicLink
;
use
Modules\Media\Events\Handlers\RenameFolderOnDisk
;
...
...
@@ -80,6 +82,7 @@ class MediaServiceProvider extends ServiceProvider
$events
->
listen
(
FolderWasUpdated
::
class
,
RenameFolderOnDisk
::
class
);
$events
->
listen
(
FolderIsDeleting
::
class
,
DeleteFolderOnDisk
::
class
);
$events
->
listen
(
FolderIsDeleting
::
class
,
DeleteAllChildrenOfFolder
::
class
);
$events
->
listen
(
FileStartedMoving
::
class
,
MoveFileOnDisk
::
class
);
$this
->
app
[
TagManager
::
class
]
->
registerNamespace
(
new
File
());
$this
->
registerThumbnails
();
...
...
Modules/Media/Repositories/Eloquent/EloquentFileRepository.php
View file @
0a247740
...
...
@@ -8,6 +8,7 @@ use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use
Modules\Media\Entities\File
;
use
Modules\Media\Events\FileIsCreating
;
use
Modules\Media\Events\FileIsUpdating
;
use
Modules\Media\Events\FileStartedMoving
;
use
Modules\Media\Events\FileWasCreated
;
use
Modules\Media\Events\FileWasUpdated
;
use
Modules\Media\Helpers\FileHelper
;
...
...
@@ -193,4 +194,20 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
{
return
$this
->
model
->
where
(
'is_folder'
,
0
)
->
get
();
}
public
function
move
(
File
$file
,
File
$destination
)
:
File
{
$previousData
=
[
'filename'
=>
$file
->
filename
,
'path'
=>
$file
->
path
,
];
$this
->
update
(
$file
,
[
'path'
=>
$this
->
getPathFor
(
$file
->
filename
,
$destination
->
id
),
]);
event
(
new
FileStartedMoving
(
$file
,
$previousData
));
return
$file
;
}
}
Modules/Media/Repositories/FileRepository.php
View file @
0a247740
...
...
@@ -5,6 +5,7 @@ namespace Modules\Media\Repositories;
use
Illuminate\Database\Eloquent\Collection
;
use
Illuminate\Http\Request
;
use
Modules\Core\Repositories\BaseRepository
;
use
Modules\Media\Entities\File
;
use
Symfony\Component\HttpFoundation\File\UploadedFile
;
interface
FileRepository
extends
BaseRepository
...
...
@@ -48,4 +49,6 @@ interface FileRepository extends BaseRepository
public
function
findForVirtualPath
(
string
$path
);
public
function
allForGrid
()
:
Collection
;
public
function
move
(
File
$file
,
File
$destination
)
:
File
;
}
Modules/Media/Tests/EloquentFileRepositoryTest.php
View file @
0a247740
...
...
@@ -11,6 +11,7 @@ use Modules\Media\Events\FileWasCreated;
use
Modules\Media\Events\FileWasUpdated
;
use
Modules\Media\Repositories\FileRepository
;
use
Modules\Media\Repositories\FolderRepository
;
use
Modules\Media\Services\FileService
;
use
Symfony\Component\Finder\SplFileInfo
;
use
Symfony\Component\HttpFoundation\File\UploadedFile
;
...
...
@@ -256,6 +257,37 @@ class EloquentFileRepositoryTest extends MediaTestCase
$this
->
assertCount
(
2
,
$this
->
file
->
allForGrid
());
}
/** @test */
public
function
it_can_move_a_file_database
()
{
$folderRepository
=
app
(
FolderRepository
::
class
);
$parentFolder
=
$folderRepository
->
create
([
'name'
=>
'My Folder'
,
'parent_id'
=>
0
]);
$folder
=
$folderRepository
->
create
([
'name'
=>
'Child Folder'
,
'parent_id'
=>
$parentFolder
->
id
]);
$file
=
$this
->
createFile
(
'my-file.jpg'
);
$file
=
$this
->
file
->
move
(
$file
,
$folder
);
$this
->
assertEquals
(
'my-file.jpg'
,
$file
->
filename
);
$this
->
assertEquals
(
'/assets/media/my-folder/child-folder/my-file.jpg'
,
$file
->
path
->
getRelativeUrl
());
}
/** @test */
public
function
it_can_move_file_on_disk
()
{
$folderRepository
=
app
(
FolderRepository
::
class
);
$parentFolder
=
$folderRepository
->
create
([
'name'
=>
'My Folder'
,
'parent_id'
=>
0
]);
$folder
=
$folderRepository
->
create
([
'name'
=>
'Child Folder'
,
'parent_id'
=>
$parentFolder
->
id
]);
$file
=
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-file.jpg'
);
$file
=
app
(
FileService
::
class
)
->
store
(
$file
);
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'/assets/media/my-file.jpg'
)));
$this
->
file
->
move
(
$file
,
$folder
);
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'/assets/media/my-folder/child-folder/my-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