Skip to content

Commit

Permalink
Merge pull request #13 from ARCANESOFT/patch-1
Browse files Browse the repository at this point in the history
Refactoring & Cleaning the package
  • Loading branch information
arcanedev-maroc authored Sep 7, 2017
2 parents 09db718 + 66a5818 commit 354c8bd
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 114 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ matrix:
allow_failures:
- php: nightly

env:
- TESTBENCH_VERSION=3.4.*

before_script:
- travis_retry composer self-update
- travis_retry composer require --prefer-source --no-interaction --dev "orchestra/testbench-browser-kit:${TESTBENCH_VERSION}"
- travis_retry composer install --prefer-source --no-interaction

script:
- composer validate
Expand Down
21 changes: 7 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
"license": "MIT",
"require": {
"php": ">=5.6.4",
"arcanesoft/auth": "~3.0",
"arcanesoft/core": "~2.4"
"arcanesoft/auth": "~3.0.0",
"arcanesoft/core": "~2.6.0"
},
"require-dev": {
"arcanesoft/foundation": "~2.2",
"phpunit/phpcov": "~3.0",
"phpunit/phpunit": "~5.0",
"mockery/mockery": "0.9.*"
"arcanesoft/foundation": "~2.3.0",
"phpunit/phpcov": "~3.0",
"phpunit/phpunit": "~5.0",
"mockery/mockery": "~0.9.0",
"orchestra/testbench-browser-kit": "~3.4.0"
},
"autoload": {
"psr-4": {
Expand All @@ -35,13 +36,5 @@
"psr-4": {
"Arcanesoft\\Media\\Tests\\": "tests/"
}
},
"scripts": {
"testbench": "composer require --dev \"orchestra/testbench-browser-kit=~3.4.0\""
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}
File renamed without changes.
4 changes: 4 additions & 0 deletions src/Contracts/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ interface Media
| Constants
| -----------------------------------------------------------------
*/

const MEDIA_TYPE_DIRECTORY = 'directory';
const MEDIA_TYPE_FILE = 'file';

/* -----------------------------------------------------------------
| Getters & Setters
| -----------------------------------------------------------------
*/

/**
* Get the default disk name.
*
Expand All @@ -46,6 +48,7 @@ public function getExcludedFiles();
| Main Methods
| -----------------------------------------------------------------
*/

/**
* Get a filesystem adapter.
*
Expand Down Expand Up @@ -153,6 +156,7 @@ public function move($from, $to);
| Check Methods
| -----------------------------------------------------------------
*/

/**
* Determine if a file/directory exists.
*
Expand Down
175 changes: 113 additions & 62 deletions src/Http/Controllers/Admin/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function __construct(Media $media)
| Main Methods
| -----------------------------------------------------------------
*/

/**
* Get the the media files.
*
Expand All @@ -65,10 +66,10 @@ public function getAll(Request $request)
{
$this->authorize(MediasPolicy::PERMISSION_LIST);

$location = $request->get('location', '/');

return $this->jsonResponseSuccess([
'medias' => $this->media->all($location),
'medias' => $this->media->all(
$request->get('location', '/')
),
]);
}

Expand All @@ -83,6 +84,7 @@ public function uploadMedia(Request $request)
{
$this->authorize(MediasPolicy::PERMISSION_CREATE);

// TODO: Refactor this with the Laravel 5.5 new Exception Handler & Form Request
$validator = validator($request->all(), [
'location' => ['required', 'string'],
'medias' => ['required', 'array'],
Expand Down Expand Up @@ -115,8 +117,8 @@ public function createDirectory(Request $request)
{
$this->authorize(MediasPolicy::PERMISSION_CREATE);

$data = $request->all();
$validator = validator($data, [
// TODO: Refactor this with the Laravel 5.5 new Exception Handler & Form Request
$validator = validator($data = $request->all(), [
'name' => ['required', 'string'], // TODO: check if the folder does not exists
'location' => ['required', 'string'],
]);
Expand All @@ -138,7 +140,8 @@ public function renameMedia(Request $request)
{
$this->authorize(MediasPolicy::PERMISSION_UPDATE);

// TODO: check if the folder does not exists
// TODO: Refactor this with the Laravel 5.5 new Exception Handler & Form Request
// TODO: Check if the folder does not exists
$validator = validator($data = $request->all(), [
'media' => ['required', 'array'],
'media.type' => ['required', 'string'],
Expand All @@ -153,25 +156,18 @@ public function renameMedia(Request $request)
], 422);
}

// TODO Refactor this...
$location = trim($data['location'], '/');
$from = $location.'/'.$data['media']['name'];

switch (strtolower($data['media']['type'])) {
case Media::MEDIA_TYPE_FILE:
$path = $this->moveFile($location, $from, $data['newName']);
break;

case Media::MEDIA_TYPE_DIRECTORY:
$path = $this->moveDirectory($location, $from, $data['newName']);
break;
$path = $this->performMoveMedia(
$data['media']['type'],
trim($data['location'], '/'),
$data['media']['name'],
$data['newName']
);

default:
return $this->jsonResponseError([
'message' => 'Something wrong was happened while renaming the media.',
], 500);
if ($path === false) {
return $this->jsonResponseError([
'message' => 'Something wrong was happened while renaming the media.',
], 500);
}

return $this->jsonResponseSuccess([
'data' => compact('path'),
]);
Expand All @@ -181,7 +177,7 @@ public function deleteMedia(Request $request)
{
$this->authorize(MediasPolicy::PERMISSION_DELETE);

// TODO: Add validation
// TODO: Refactor this with the Laravel 5.5 new Exception Handler & Form Request
$validator = validator($data = $request->all(), [
'media' => ['required', 'array'],
'media.type' => ['required', 'string'],
Expand All @@ -194,40 +190,20 @@ public function deleteMedia(Request $request)
], 422);
}

$type = $data['media']['type'];
$path = $data['media']['path'];

// TODO Refactor this...
$deleted = false;

if ($type == Media::MEDIA_TYPE_FILE) {
$deleted = $this->media->deleteFile($path);
}
elseif ($type == Media::MEDIA_TYPE_DIRECTORY) {
$deleted = $this->media->deleteDirectory(trim($path, '/'));
}

return $deleted ? $this->jsonResponseSuccess() : $this->jsonResponseError();
return $this->performDeleteMedia($data['media']['type'], $data['media']['path'])
? $this->jsonResponseSuccess()
: $this->jsonResponseError();
}

public function moveLocations(Request $request)
{
$this->authorize(MediasPolicy::PERMISSION_UPDATE);

$location = $request->get('location', '/');
$name = $request->get('name');
$isHome = $location === '/';
$selected = $isHome ? $name : $location.'/'.$name;

$destinations = $this->media->directories($location)
->pluck('path')
->reject(function ($path) use ($selected) {
return $path === $selected;
})
->values();

if ( ! $isHome)
$destinations->prepend('..');
// TODO: Adding validation ?
$destinations = $this->getDestinations(
$request->get('name'),
$request->get('location', '/')
);

return $this->jsonResponseSuccess(compact('destinations'));
}
Expand All @@ -236,6 +212,7 @@ public function moveMedia(Request $request)
{
$this->authorize(MediasPolicy::PERMISSION_UPDATE);

// TODO: Refactor this with the Laravel 5.5 new Exception Handler & Form Request
$validator = validator($data = $request->all(), [
'old_path' => ['required', 'string'],
'new_path' => ['required', 'string'],
Expand All @@ -258,38 +235,112 @@ public function moveMedia(Request $request)
*/

/**
* Move a file.
* Get the destinations paths.
*
* @param string $name
* @param string $location
*
* @return \Arcanesoft\Media\Entities\DirectoryCollection
*/
private function getDestinations($name, $location)
{
$selected = ($isHome = $location === '/') ? $name : "{$location}/{$name}";
$destinations = $this->media->directories($location)
->pluck('path')
->reject(function ($path) use ($selected) {
return $path === $selected;
})
->values();

if ( ! $isHome)
$destinations->prepend('..');

return $destinations;
}

/**
* Perform the media movement.
*
* @param string $type
* @param string $location
* @param string $oldName
* @param string $newName
*
* @return bool|string
*/
private function performMoveMedia($type, $location, $oldName, $newName)
{
$from = "{$location}/{$oldName}";

switch (Str::lower($type)) {
case Media::MEDIA_TYPE_FILE:
return $this->moveFile($from, $location, $newName);

case Media::MEDIA_TYPE_DIRECTORY:
return $this->moveDirectory($from, $location, $newName);

default:
return false;
}
}

/**
* Move file.
*
* @param string $location
* @param string $location
* @param string $from
* @param string $newName
*
* @return string
*/
private function moveFile($location, $from, $newName)
private function moveFile($from, $location, $newName)
{
$filename = Str::slug(pathinfo($newName, PATHINFO_FILENAME)).'.'.pathinfo($newName, PATHINFO_EXTENSION);
$filename = Str::slug(pathinfo($newName, PATHINFO_FILENAME));
$extension = pathinfo($newName, PATHINFO_EXTENSION);

$this->media->move($from, $to = $location.'/'.$filename);
$this->media->move($from, $to = "{$location}/{$filename}.{$extension}");

return $to;
}

/**
* Move a directory.
* Move directory.
*
* @param string $location
* @param string $from
* @param string $location
* @param string $newName
*
* @return string
*/
private function moveDirectory($location, $from, $newName)
private function moveDirectory($from, $location, $newName)
{
$to = $location.'/'.Str::slug($newName);
$newName = Str::slug($newName);

$this->media->move($from, $to);
return tap("{$location}/{$newName}", function ($to) use ($from) {
$this->media->move($from, $to);
});
}

return $to;
/**
* Perform the media deletion.
*
* @param string $type
* @param string $path
*
* @return bool
*/
private function performDeleteMedia($type, $path)
{
switch (Str::lower($type)) {
case Media::MEDIA_TYPE_FILE:
return $this->media->deleteFile($path);

case Media::MEDIA_TYPE_DIRECTORY:
return $this->media->deleteDirectory(trim($path, '/'));

default:
return false;
}
}
}
16 changes: 2 additions & 14 deletions src/Http/Controllers/Admin/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,19 @@ abstract class Controller extends AdminController
| Traits
| -----------------------------------------------------------------
*/

use Notifyable,
JsonResponses;

/* -----------------------------------------------------------------
| Properties
| -----------------------------------------------------------------
*/

/**
* The view namespace.
*
* @var string
*/
protected $viewNamespace = 'media';

/* -----------------------------------------------------------------
| Constructor
| -----------------------------------------------------------------
*/
/**
* Instantiate the controller.
*/
public function __construct()
{
parent::__construct();

$this->addBreadcrumbRoute('Media', 'admin::media.index');
}
}
Loading

0 comments on commit 354c8bd

Please sign in to comment.