Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image uploads #76

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ app/config/parameters.yml

composer.phar
behat.yml
.idea
!web/uploads/.gitkeep
!web/uploads/media/.gitkeep
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ old view before 1.5 release
## License

All what you can find at this repo shared under the MIT License

## about filestack
As image uploader we use https://www.filestack.com/.
To use it You should visit https://dev.filestack.com/apps/ and create new app.
After that You should put your api_key to parameters.yml as filestack.api_key
6 changes: 5 additions & 1 deletion app/config/parameters.circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ parameters:
# default email for bot from which email is sent
from_email: [email protected]

admin_password: admin
admin_password: admin
#filestack params
media.uploadPath: %kernel.root_dir%/../web/uploads/media/
media.uploadUrl: /uploads/media/
filestack.api_key: A3w1aZwZmQBibC09rAcZnz
4 changes: 4 additions & 0 deletions app/config/parameters.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ parameters:
from_email: [email protected]

admin_password: admin

media.uploadPath: %kernel.root_dir%/../web/uploads/media/
media.uploadUrl: /uploads/media/
filestack.api_key: A3w1aZwZmQBibC09rAcZnz
1 change: 1 addition & 0 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ backend:
browser_name: chrome
goutte: ~
selenium2: ~
files_path: %behat.paths.base%/web/uploads/media/

Behat\Symfony2Extension\Extension:
mink_driver: true
Expand Down
1 change: 1 addition & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ test:
pre:
- chmod -R 777 app/cache
- chmod -R 777 app/logs
- chmod -R 777 web/uploads/media
- wget http://selenium-release.storage.googleapis.com/2.48/selenium-server-standalone-2.48.2.jar
- nohup bash -c "java -jar selenium-server-standalone-2.48.2.jar &"
override:
Expand Down
181 changes: 181 additions & 0 deletions features/backend/media.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
@backend
Feature: Media settings
In order to update media settings
As an admin
I should be able to edit media settings

Background:
Given following "Event":
| ref | title | description | startDate | endDate | venue | email | host |
| event | My event | My another awesome event! | 2016-03-01 10:00 | 2016-03-01 18:00 | Burj Khalifa Tower | [email protected] | http://localhost:8000 |
| event2 | My other event | My other awesome event! | 2016-03-01 10:00 | 2016-03-01 18:00 | Burj Khalifa Tower | [email protected] | http://eventator.loc:8080 |
| event3 | His event | His another awesome event! | 2016-04-01 10:00 | 2016-04-01 18:00 | Kuala-lumpur Tower | [email protected] | http://event.com |
And following "EventTranslation":
| event | locale |
| event | ru_RU |
| event | de_DE |
| event2 | ru_RU |
| event2 | de_DE |
| event3 | ru_RU |
And following "Organizer":
| ref | title | description | isActive | events |
| organizer | My organizer | My another awesome organizer! | 1 | event,event2,event3 |
| organizer2 | His organizer | His another awesome organizer! | 1 | event2,event3 |
And following "OrganizerTranslation":
| organizer | locale |
| organizer | ru_RU |
| organizer2 | de_DE |
And following "Speech":
| ref | title | description | language | event |
| speech | symfony propagation | world symfony expansion | ru | event |
| speech2 | php servers piece | php most popular language | en | event |
| speech3 | doctrine must have | what you docrtine project should have | en | event |
| speech4 | symfony propagation2 | world symfony expansion2 | en | event2 |
| speech5 | php servers piece2 | php most popular language2 | en | event2 |
| speech6 | doctrine must have2 | what you docrtine project should have2 | en | event2 |
| speech7 | symfony propagation2 | world symfony expansion2 | en | event3 |
| speech8 | php servers piece2 | php most popular language2 | en | event3 |
| speech9 | doctrine must have2 | what you docrtine project should have2 | en | event3 |

And following "SpeechTranslation":
| speech | locale |
| speech | ru_RU |
| speech2 | de_DE |
| speech3 | de_DE |
| speech4 | ru_RU |
| speech5 | de_DE |
| speech6 | de_DE |
| speech7 | ru_RU |
| speech8 | de_DE |
| speech9 | de_DE |
And following "Program":
| ref | title | isTopic | isActive | startDate | endDate | events | speech |
| program | keynote | 1 | 1 | 2016-03-01 10:00 | 2016-03-01 10:30 | event | |
| program2 | alex_symfony | 0 | 1 | 2016-03-01 10:30 | 2016-03-01 11:30 | event | speech |
| program3 | phil_php | 0 | 1 | 2016-03-01 11:30 | 2016-03-01 12:30 | event | speech2 |
| program4 | coffee1 | 1 | 1 | 2016-03-01 12:30 | 2016-03-01 13:00 | event | |
| program5 | phil_doctrine | 0 | 1 | 2016-03-01 13:00 | 2016-03-01 14:30 | event | speech3 |
| program6 | end_keynote | 1 | 1 | 2016-03-01 14:30 | 2016-03-01 15:00 | event | |
| program7 | after_party | 1 | 1 | 2016-03-01 15:00 | 2016-03-01 18:00 | event | |
| program8 | keynote | 1 | 1 | 2016-04-01 10:00 | 2016-04-01 10:30 | event2 | |
| program9 | alex_symfony | 0 | 1 | 2016-04-01 10:30 | 2016-04-01 11:30 | event2 | speech4 |
| program10 | phil_php | 0 | 1 | 2016-04-01 11:30 | 2016-04-01 12:30 | event2 | speech5 |
| program11 | coffee1 | 1 | 1 | 2016-04-01 12:30 | 2016-04-01 13:00 | event2 | |
| program12 | phil_doctrine | 0 | 1 | 2016-04-01 13:00 | 2016-04-01 14:30 | event2 | speech6 |
| program13 | end_keynote | 1 | 1 | 2016-04-01 14:30 | 2016-04-01 15:00 | event2 | |
| program14 | after_party | 1 | 1 | 2016-04-01 15:00 | 2016-04-01 18:00 | event2 | |
| program15 | keynote | 1 | 1 | 2016-04-01 10:00 | 2016-04-01 10:30 | event3 | |
| program16 | alex_symfony | 0 | 1 | 2016-04-01 10:30 | 2016-04-01 11:30 | event3 | speech7 |
| program17 | phil_php | 0 | 1 | 2016-04-01 11:30 | 2016-04-01 12:30 | event3 | speech8 |
| program18 | coffee1 | 1 | 1 | 2016-04-01 12:30 | 2016-04-01 13:00 | event3 | |
| program19 | phil_doctrine | 0 | 1 | 2016-04-01 13:00 | 2016-04-01 14:30 | event3 | speech9 |
| program20 | end_keynote | 1 | 1 | 2016-04-01 14:30 | 2016-04-01 15:00 | event3 | |
| program21 | after_party | 1 | 1 | 2016-04-01 15:00 | 2016-04-01 18:00 | event3 | |
And following "ProgramTranslation":
| program | locale |
| program | ru_RU |
| program2 | de_DE |
| program3 | de_DE |
| program4 | de_DE |
| program5 | de_DE |
| program6 | de_DE |
| program7 | de_DE |
| program8 | de_DE |
| program9 | de_DE |
| program10 | de_DE |
| program11 | de_DE |
| program12 | de_DE |
| program13 | de_DE |
| program14 | de_DE |
| program15 | de_DE |
| program16 | de_DE |
| program17 | de_DE |
| program18 | de_DE |
| program19 | de_DE |
| program20 | de_DE |
| program21 | de_DE |
And following "Speaker":
| ref | firstName | lastName | Company | email | homepage | twitter | events | speeches |
| speaker | Phill | Pilow | Reseach Supplier | | | | event,event2,event3 | speech2,speech3,speech5,speech6,speech8,speech9 |
| speaker2 | Alex | Demchenko | KnpLabs | | http://451f.com.ua | https://twitter.com/twitter | event,event2,event3 | speech,speech4,speech7 |
And following "SpeakerTranslation":
| speaker | locale |
| speaker | ru_RU |
| speaker2 | de_DE |
And following "Sponsor":
| ref | company | description | homepage | type | isActive | events |
| sponsor | Reseach Supplier | NASA research center | http://nasa.gov.us | 1 | 1 | event,event2,event3 |
| sponsor2 | KnpLabs | Happy awesome developer | http://knplabs.com | 2 | 1 | event |
And following "SponsorTranslation":
| sponsor | locale |
| sponsor | de_DE |
| sponsor2 | de_DE |
And following "Media":
| title | filename | created_date | updated_date |
| Media1 | logo.png | 2017-10-27 16:02 | 2017-10-27 16:02 |
| Media2 | logo.png | 2017-10-27 17:02 | 2017-10-27 17:02 |

@javascript
Scenario: Admin should have access to the media manage
Given I am sign in as admin
When I click "Media"
Then I wait for a form
Then I should see "Add Media"
And I should see the row containing "1;Media1"
And I should see the row containing "2;Media2"
When I click "Edit" on the row containing "1;Media1"
Then I wait for a form
Then I should see "Media settings"

@javascript
Scenario: Admin should be able to add media
Given I am sign in as admin
When I click "Media"
Then I wait for a form
Then I should see "Add Media"
And I click "Add Media"
Then I wait for a form
Then I should see "Media settings"
And I fill in "Title" with "Media3"
And I press "Upload file"
Then I wait "10" seconds
Then I should see " My Device "
And I attach the file "test.png" to "fsp-fileUpload"
Then I should see "Edit Image"
Then I wait "15" seconds
And I click on the element with css selector ".fsp-button--primary"
Then I wait "10" seconds
And I press "Add"
Then I wait for a form
Then I should see "Media Media3 added."
Then I should see the row containing "3;Media3"

@javascript
Scenario: Admin should be able to update media settings
Given I am sign in as admin
When I click "Media"
Then I wait for a form
Then I should see "Add Media"
And I should see the row containing "2;Media2"
When I click "Edit" on the row containing "2;Media2"
Then I wait for a form
Then I should see "Media settings"
And I fill in "Title" with "Media2_1"
And I fill in "Media Description" with "Awesome media"
And I fill in "Copyright Info" with "Test media"
And I press "Update"
Then I wait for a form
Then I should see "Media Media2_1 updated."
Then I should see the row containing "2;Media2_1"

@javascript
Scenario: Admin should be delete to the media
Given I am sign in as admin
When I click "Media"
Then I wait for a form
Then I should see "Add Media"
And I should see the row containing "1;Media1"
Then I delete the record with id "1"
Then I wait for a form
Then I should see "Media deleted."
Then I should not see the row containing "1;Media1"
121 changes: 121 additions & 0 deletions src/Event/EventBundle/Controller/Backend/MediaController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace Event\EventBundle\Controller\Backend;

use Guzzle\Http\Client;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\HttpFoundation\Request;
use Event\EventBundle\Controller\Controller;
use Event\EventBundle\Entity\Media;
use Event\EventBundle\Form\Type\MediaType;

class MediaController extends Controller
{

public function getUploadPath()
{
return $this->container->getParameter('media.uploadPath');
}

public function getUploadUrl(){
return $this->container->getParameter('media.uploadUrl');
}

public function indexAction()
{
return $this->render('EventEventBundle:Backend/Media:index.html.twig', array(
'media' => $this->getRepository('EventEventBundle:Media')->findAll(),
'uploadPath' => $this->getUploadPath(),
'uploadUrl' => $this->getUploadUrl()
));
}

public function manageAction(Request $request, $id = null)
{
$oldFileName = '';
if ($id === null) {
$media = new Media();
} else {
$media = $this->findOr404('EventEventBundle:Media', $id);
$oldFileName = $media->getFilename();
}

$form = $this->createForm(MediaType::class, $media);

if ($request->getMethod() === 'POST') {
$form->handleRequest($request);

if ($form->isValid()) {
$media->setUpdated(new \DateTime());

if ($oldFileName !== $media->getFilename()) {
$url = $request->request->get('file_url');
$path = $this->getUploadPath();
$media->setFilename(time() . '_' . $media->getFilename());
$file = $this->getFile($url);

$fs = new Filesystem();

try {
$fs->dumpFile($path . $media->getFilename(), $file, 0777);
} catch (IOExceptionInterface $e) {
throw new Exception($e->getMessage());
}
}

$this->getManager()->persist($media);
$this->getManager()->flush();

$successFlashText = sprintf('Media %s updated.', $media->getTitle());
if (!$id) {
$successFlashText = sprintf('Media %s added.', $media->getTitle());
}
$this->setSuccessFlash($successFlashText);

return $this->redirectToRoute('backend_media');
}
}

return $this->render('EventEventBundle:Backend/Media:manage.html.twig', [
'media' => $media,
'form' => $form->createView(),
'fs_api' => $this->container->getParameter('filestack.api_key'),
]);
}

protected function checkAccessFolder($path){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CS fix

if(is_dir($path)){
chmod($path, 0777);
} else {
mkdir($path, 0777, true);
}
}

protected function getFile($url)
{
$client = new Client();
$file = $client->createRequest('GET', $url)->send()->getBody(true);

return $file;
}

public function deleteAction($id)
{
$this->isGrantedAdmin();
$translator = $this->get('translator');

$entity = $this->findOr404('EventEventBundle:Media', $id);
$file = $this->getUploadPath() . $entity->getFilename();
$this->getManager()->remove($entity);
$this->getManager()->flush();
if (is_file($file)) {
unlink($file);
}

$this->setSuccessFlash($translator->trans('Media deleted.'));

return $this->redirectToRoute('backend_media');
}
}
Loading