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
3b7f3d37
Unverified
Commit
3b7f3d37
authored
Oct 08, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ability to move files with its thumbnails
parent
0a247740
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
121 additions
and
6 deletions
+121
-6
MoveFileOnDisk.php
Modules/Media/Events/Handlers/MoveFileOnDisk.php
+98
-3
EloquentFileRepositoryTest.php
Modules/Media/Tests/EloquentFileRepositoryTest.php
+23
-3
No files found.
Modules/Media/Events/Handlers/MoveFileOnDisk.php
View file @
3b7f3d37
...
...
@@ -4,6 +4,9 @@ namespace Modules\Media\Events\Handlers;
use
Illuminate\Contracts\Filesystem\Factory
;
use
Modules\Media\Events\FileStartedMoving
;
use
Modules\Media\Image\Thumbnail
;
use
Modules\Media\Image\ThumbnailManager
;
use
Modules\Media\ValueObjects\MediaPath
;
class
MoveFileOnDisk
{
...
...
@@ -11,21 +14,65 @@ class MoveFileOnDisk
* @var Factory
*/
private
$filesystem
;
/**
* All the different images types where thumbnails should be created
* @var array
*/
private
$imageExtensions
=
[
'jpg'
,
'png'
,
'jpeg'
,
'gif'
];
/**
* @var ThumbnailManager
*/
private
$manager
;
public
function
__construct
(
Factory
$filesystem
)
public
function
__construct
(
Factory
$filesystem
,
ThumbnailManager
$manager
)
{
$this
->
filesystem
=
$filesystem
;
$this
->
manager
=
$manager
;
}
public
function
handle
(
FileStartedMoving
$event
)
{
$this
->
moveOriginal
(
$event
);
if
(
$this
->
isImage
(
$event
->
previousData
[
'path'
]
->
getRelativeUrl
()))
{
$this
->
moveThumbnails
(
$event
);
}
}
private
function
moveOriginal
(
$event
)
{
$this
->
move
(
$event
->
previousData
[
'path'
]
->
getRelativeUrl
(),
$event
->
file
->
path
->
getRelativeUrl
());
}
private
function
moveThumbnails
(
$event
)
{
foreach
(
$this
->
manager
->
all
()
as
$thumbnail
)
{
$fromPath
=
$this
->
getFilenameFor
(
$event
->
previousData
[
'path'
],
$thumbnail
);
$toPath
=
$this
->
getFilenameFor
(
$event
->
file
->
path
,
$thumbnail
);
$this
->
move
(
$fromPath
,
$toPath
);
}
}
private
function
move
(
$fromPath
,
$toPath
)
{
$this
->
filesystem
->
disk
(
$this
->
getConfiguredFilesystem
())
->
move
(
$this
->
getDestinationPath
(
$
event
->
previousData
[
'path'
]
->
getRelativeUrl
()
),
$this
->
getDestinationPath
(
$
event
->
file
->
path
->
getRelativeUrl
()
)
$this
->
getDestinationPath
(
$
fromPath
),
$this
->
getDestinationPath
(
$
toPath
)
);
}
/**
* Check if the given path is en image
* @param string $path
* @return bool
*/
private
function
isImage
(
$path
)
{
return
in_array
(
pathinfo
(
$path
,
PATHINFO_EXTENSION
),
$this
->
imageExtensions
);
}
private
function
getDestinationPath
(
$path
)
:
string
{
if
(
$this
->
getConfiguredFilesystem
()
===
'local'
)
{
...
...
@@ -42,4 +89,52 @@ class MoveFileOnDisk
{
return
config
(
'asgard.media.config.filesystem'
);
}
/**
* @param MediaPath $path
* @param Thumbnail|string $thumbnail
* @return string
*/
private
function
getFilenameFor
(
MediaPath
$path
,
$thumbnail
)
{
if
(
$thumbnail
instanceof
Thumbnail
)
{
$thumbnail
=
$thumbnail
->
name
();
}
$filenameWithoutPrefix
=
$this
->
removeConfigPrefix
(
$path
->
getRelativeUrl
());
$filename
=
substr
(
strrchr
(
$filenameWithoutPrefix
,
'/'
),
1
);
$folders
=
str_replace
(
$filename
,
''
,
$filenameWithoutPrefix
);
if
(
$filename
===
false
)
{
return
config
(
'asgard.media.config.files-path'
)
.
$this
->
newFilename
(
$path
,
$thumbnail
);
}
return
config
(
'asgard.media.config.files-path'
)
.
$folders
.
$this
->
newFilename
(
$path
,
$thumbnail
);
}
/**
* @param string $path
* @return string
*/
private
function
removeConfigPrefix
(
string
$path
)
:
string
{
$configAssetPath
=
config
(
'asgard.media.config.files-path'
);
return
str_replace
([
$configAssetPath
,
ltrim
(
$configAssetPath
,
'/'
),
],
''
,
$path
);
}
/**
* Prepend the thumbnail name to filename
* @param $path
* @param $thumbnail
* @return mixed|string
*/
private
function
newFilename
(
$path
,
$thumbnail
)
{
$filename
=
pathinfo
(
$path
,
PATHINFO_FILENAME
);
return
$filename
.
'_'
.
$thumbnail
.
'.'
.
pathinfo
(
$path
,
PATHINFO_EXTENSION
);
}
}
Modules/Media/Tests/EloquentFileRepositoryTest.php
View file @
3b7f3d37
...
...
@@ -264,12 +264,13 @@ class EloquentFileRepositoryTest extends MediaTestCase
$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
=
\Illuminate\Http\UploadedFile
::
fake
()
->
create
(
'my-file.pdf'
);
$file
=
app
(
FileService
::
class
)
->
store
(
$file
);
$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
());
$this
->
assertEquals
(
'my-file.
pdf
'
,
$file
->
filename
);
$this
->
assertEquals
(
'/assets/media/my-folder/child-folder/my-file.
pdf
'
,
$file
->
path
->
getRelativeUrl
());
}
/** @test */
...
...
@@ -279,13 +280,32 @@ class EloquentFileRepositoryTest extends MediaTestCase
$parentFolder
=
$folderRepository
->
create
([
'name'
=>
'My Folder'
,
'parent_id'
=>
0
]);
$folder
=
$folderRepository
->
create
([
'name'
=>
'Child Folder'
,
'parent_id'
=>
$parentFolder
->
id
]);
$file
=
\Illuminate\Http\UploadedFile
::
fake
()
->
create
(
'my-file.pdf'
);
$file
=
app
(
FileService
::
class
)
->
store
(
$file
);
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'/assets/media/my-file.pdf'
)));
$this
->
file
->
move
(
$file
,
$folder
);
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'/assets/media/my-folder/child-folder/my-file.pdf'
)));
}
/** @test */
public
function
it_can_move_file_with_thumbnails_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'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'/assets/media/my-folder/child-folder/my-file_smallThumb.jpg'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
exists
(
public_path
(
'/assets/media/my-folder/child-folder/my-file_mediumThumb.jpg'
)));
}
private
function
createFile
(
$fileName
=
'random/name.jpg'
)
...
...
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