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
4e3e1527
Unverified
Commit
4e3e1527
authored
Sep 22, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding feature to create files (db) in a folder
parent
a8f63696
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
3 deletions
+50
-3
EloquentFileRepository.php
...es/Media/Repositories/Eloquent/EloquentFileRepository.php
+16
-2
FileRepository.php
Modules/Media/Repositories/FileRepository.php
+2
-1
EloquentFileRepositoryTest.php
Modules/Media/Tests/EloquentFileRepositoryTest.php
+32
-0
No files found.
Modules/Media/Repositories/Eloquent/EloquentFileRepository.php
View file @
4e3e1527
...
...
@@ -12,6 +12,7 @@ use Modules\Media\Events\FileWasCreated;
use
Modules\Media\Events\FileWasUpdated
;
use
Modules\Media\Helpers\FileHelper
;
use
Modules\Media\Repositories\FileRepository
;
use
Modules\Media\Repositories\FolderRepository
;
use
Symfony\Component\HttpFoundation\File\UploadedFile
;
class
EloquentFileRepository
extends
EloquentBaseRepository
implements
FileRepository
...
...
@@ -37,9 +38,10 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
/**
* Create a file row from the given file
* @param UploadedFile $file
* @param int $parentId
* @return mixed
*/
public
function
createFromFile
(
UploadedFile
$file
)
public
function
createFromFile
(
UploadedFile
$file
,
int
$parentId
=
0
)
{
$fileName
=
FileHelper
::
slug
(
$file
->
getClientOriginalName
());
...
...
@@ -51,7 +53,7 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
$data
=
[
'filename'
=>
$fileName
,
'path'
=>
config
(
'asgard.media.config.files-path'
)
.
"
{
$fileName
}
"
,
'path'
=>
$this
->
getPathFor
(
$fileName
,
$parentId
)
,
'extension'
=>
substr
(
strrchr
(
$fileName
,
'.'
),
1
),
'mimetype'
=>
$file
->
getClientMimeType
(),
'filesize'
=>
$file
->
getFileInfo
()
->
getSize
(),
...
...
@@ -66,6 +68,18 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
return
$file
;
}
private
function
getPathFor
(
string
$filename
,
int
$folderId
)
{
if
(
$folderId
!==
0
)
{
$parent
=
app
(
FolderRepository
::
class
)
->
findFolder
(
$folderId
);
if
(
$parent
!==
null
)
{
return
$parent
->
path
->
getRelativeUrl
()
.
'/'
.
$filename
;
}
}
return
config
(
'asgard.media.config.files-path'
)
.
$filename
;
}
public
function
destroy
(
$file
)
{
$file
->
delete
();
...
...
Modules/Media/Repositories/FileRepository.php
View file @
4e3e1527
...
...
@@ -11,9 +11,10 @@ interface FileRepository extends BaseRepository
/**
* Create a file row from the given file
* @param UploadedFile $file
* @param int $parentId
* @return mixed
*/
public
function
createFromFile
(
UploadedFile
$file
);
public
function
createFromFile
(
UploadedFile
$file
,
int
$parentId
=
0
);
/**
* Find a file for the entity by zone
...
...
Modules/Media/Tests/EloquentFileRepositoryTest.php
View file @
4e3e1527
...
...
@@ -10,6 +10,7 @@ use Modules\Media\Events\FileIsUpdating;
use
Modules\Media\Events\FileWasCreated
;
use
Modules\Media\Events\FileWasUpdated
;
use
Modules\Media\Repositories\FileRepository
;
use
Modules\Media\Repositories\FolderRepository
;
use
Symfony\Component\Finder\SplFileInfo
;
use
Symfony\Component\HttpFoundation\File\UploadedFile
;
...
...
@@ -27,6 +28,14 @@ class EloquentFileRepositoryTest extends MediaTestCase
$this
->
resetDatabase
();
$this
->
file
=
app
(
FileRepository
::
class
);
$this
->
app
[
'config'
]
->
set
(
'asgard.media.config.files-path'
,
'/assets/media/'
);
}
public
function
tearDown
()
{
if
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'assets'
))
===
true
)
{
$this
->
app
[
'files'
]
->
deleteDirectory
(
public_path
(
'assets'
));
}
}
/** @test */
...
...
@@ -212,6 +221,29 @@ class EloquentFileRepositoryTest extends MediaTestCase
$this
->
assertEquals
(
'Hello World'
,
$file
->
translate
(
'en'
)
->
description
);
}
/** @test */
public
function
it_can_create_a_file_in_a_folder
()
{
$folder
=
app
(
FolderRepository
::
class
)
->
create
([
'name'
=>
'My Folder'
,
'parent_id'
=>
0
]);
$file
=
$this
->
file
->
createFromFile
(
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-file.jpg'
),
$folder
->
id
);
$this
->
assertCount
(
2
,
$this
->
file
->
all
());
$this
->
assertEquals
(
'/assets/media/my-folder/my-file.jpg'
,
$file
->
path
->
getRelativeUrl
());
}
/** @test */
public
function
it_can_create_a_file_in_sub_sub_folder
()
{
$folderRepository
=
app
(
FolderRepository
::
class
);
$folderRepository
->
create
([
'name'
=>
'My Folder'
,
'parent_id'
=>
0
]);
$nestedFolder
=
$folderRepository
->
create
([
'name'
=>
'Nested Folder'
,
'parent_id'
=>
1
]);
$file
=
$this
->
file
->
createFromFile
(
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-file.jpg'
),
$nestedFolder
->
id
);
$this
->
assertEquals
(
'/assets/media/my-folder/nested-folder/my-file.jpg'
,
$file
->
path
->
getRelativeUrl
());
}
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