Commit 141a2950 authored by Nicolas Widart's avatar Nicolas Widart

Removing media module

parent 923353a1
rules:
php.interface_has_no_interface_suffix:
enabled: false
language: php
php:
- 5.6
- 5.5
- 5.4
- hhvm
This diff is collapsed.
This diff is collapsed.
<?php namespace Modules\Media\Composers;
use Illuminate\Contracts\View\View;
use Modules\Core\Composers\BaseSidebarViewComposer;
class SidebarViewComposer extends BaseSidebarViewComposer
{
public function compose(View $view)
{
$view->items->put('medias', [
'weight' => 6,
'request' => "*/$view->prefix/media*",
'route' => 'dashboard.media.index',
'icon-class' => 'fa fa-camera',
'title' => 'Medias',
'permission' => $this->auth->hasAccess('media.index')
]);
}
}
<?php
return [
/*
|--------------------------------------------------------------------------
| The path where the media files will be uploaded
|--------------------------------------------------------------------------
*/
'files-path' => '/assets/media/',
/*
|--------------------------------------------------------------------------
| Specify all the allowed file extensions a user can upload on the server
|--------------------------------------------------------------------------
*/
'allowed-types' => '.jpg,.png',
/*
|--------------------------------------------------------------------------
| Determine the max file size upload rate
| Defined in MB
|--------------------------------------------------------------------------
*/
'max-file-size' => '5'
];
<?php
return [
'media' => [
'index',
'create',
'store',
'edit',
'update',
'destroy',
]
];
<?php
return [
'smallThumb' => [
'fit' => [
'width' => 50,
'height' => 50,
'callback' => function($constraint) {
$constraint->upsize();
}
],
]
];
<?php namespace Modules\Media\Console;
use Illuminate\Console\Command;
use Modules\Media\Image\Imagy;
use Modules\Media\Repositories\FileRepository;
class RefreshThumbnailCommand extends Command
{
protected $name = 'asgard:media:refresh';
protected $description = 'Create and or refresh the thumbnails';
/**
* @var Imagy
*/
private $imagy;
/**
* @var FileRepository
*/
private $file;
public function __construct(Imagy $imagy, FileRepository $file)
{
parent::__construct();
$this->imagy = $imagy;
$this->file = $file;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$this->line('Preparing to regenerate all thumbnails...');
foreach ($this->file->all() as $file) {
$this->imagy->createAll($file->path);
}
$this->info('All thumbnails refreshed');
}
}
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFilesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('files', function(Blueprint $table)
{
$table->increments('id');
$table->string('filename');
$table->string('path');
$table->string('extension');
$table->string('mimetype');
$table->string('filesize');
$table->integer('folder_id')->unsigned();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('files');
}
}
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFileTranslationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(
'file_translations',
function (Blueprint $table) {
$table->increments('id');
$table->integer('file_id')->unsigned();
$table->string('locale')->index();
$table->string('description');
$table->string('alt_attribute');
$table->string('keywords');
$table->unique(['file_id', 'locale']);
$table->foreign('file_id')->references('id')->on('files')->onDelete('cascade');
}
);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('file_translations');
}
}
<?php namespace Modules\Media\Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class MediaDatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
}
}
<?php namespace Modules\Media\Entities;
use Dimsav\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model;
class File extends Model
{
use Translatable;
public $translatedAttributes = ['description', 'alt_attribute', 'keywords'];
protected $fillable = [
'description',
'alt_attribute',
'keywords',
'filename',
'path',
'extension',
'mimetype',
'width',
'height',
'filesize',
'folder_id'
];
}
<?php namespace Modules\Media\Entities;
use Illuminate\Database\Eloquent\Model;
class FileTranslation extends Model
{
public $timestamps = false;
protected $fillable = ['description', 'alt_attribute', 'keywords'];
}
<?php namespace Modules\Media\Helpers;
use Illuminate\Support\Str;
class FileHelper
{
public static function slug($name)
{
$extension = self::getExtension($name);
$name = str_replace($extension, '', $name);
$name = Str::slug($name);
return $name . $extension;
}
/**
* Get the extension from the given name
* @param $name
* @return string
*/
private static function getExtension($name)
{
return substr($name, strrpos($name, '.'));
}
}
<?php namespace Modules\Media\Http\Controllers\Admin;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
use Laracasts\Flash\Flash;
use Modules\Core\Http\Controllers\Admin\AdminBaseController;
use Modules\Media\Entities\File;
use Modules\Media\Http\Requests\UpdateMediaRequest;
use Modules\Media\Image\Imagy;
use Modules\Media\Repositories\FileRepository;
class MediaController extends AdminBaseController
{
/**
* @var FileRepository
*/
private $file;
/**
* @var Repository
*/
private $config;
/**
* @var Imagy
*/
private $imagy;
public function __construct(FileRepository $file, Repository $config, Imagy $imagy)
{
$this->file = $file;
$this->config = $config;
$this->imagy = $imagy;
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$files = $this->file->all();
$config = $this->config->get('media::config');
return View::make('media::admin.index', compact('files', 'config'));
}
public function gridFiles()
{
$files = $this->file->all();
return View::make('media::admin.grid', compact('files'));
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
return \View::make('media.create');
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param File $file
* @return Response
*/
public function edit(File $file)
{
return View::make('media::admin.edit', compact('file'));
}
/**
* Update the specified resource in storage.
*
* @param File $file
* @param UpdateMediaRequest $request
* @return Response
*/
public function update(File $file, UpdateMediaRequest $request)
{
$this->file->update($file, $request->all());
Flash::success(trans('media::messages.file updated'));
return Redirect::route('dashboard.media.index');
}
/**
* Remove the specified resource from storage.
*
* @param File $file
* @internal param int $id
* @return Response
*/
public function destroy(File $file)
{
$this->imagy->deleteAllFor($file);
$this->file->destroy($file);
Flash::success(trans('media::messages.file deleted'));
return Redirect::route('dashboard.media.index');
}
}
<?php namespace Modules\Media\Http\Controllers\Api;
use Illuminate\Support\Facades\Response;
use Modules\Media\Http\Requests\UploadMediaRequest;
use Modules\Media\Services\FileService;
class MediaController
{
/**
* @var FileService
*/
private $fileService;
public function __construct(FileService $fileService)
{
$this->fileService = $fileService;
}
/**
* Store a newly created resource in storage.
*
* @param UploadMediaRequest $request
* @return Response
*/
public function store(UploadMediaRequest $request)
{
$savedFile = $this->fileService->store($request->file('file'));
return Response::json($savedFile->toArray());
}
}
<?php namespace Modules\Media\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateMediaRequest extends FormRequest
{
public function rules()
{
return [];
}
public function authorize()
{
return true;
}
public function messages()
{
return [];
}
}
<?php namespace Modules\Media\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UploadMediaRequest extends FormRequest
{
public function rules()
{
return [];
}
public function authorize()
{
return true;
}
public function messages()
{
return [];
}
}
<?php
use Illuminate\Routing\Router;
$router->model('media', 'Modules\Media\Entities\File');
$router->group(['prefix' => LaravelLocalization::setLocale(), 'before' => 'LaravelLocalizationRedirectFilter|auth.admin|permissions'], function(Router $router)
{
$router->group(['prefix' => Config::get('core::core.admin-prefix'), 'namespace' => 'Modules\Media\Http\Controllers'], function(Router $router)
{
$router->resource('media', 'Admin\MediaController', ['except' => ['show'], 'names' => [
'index' => 'dashboard.media.index',
'create' => 'dashboard.media.create',
'store' => 'dashboard.media.store',
'edit' => 'dashboard.media.edit',
'update' => 'dashboard.media.update',
'destroy' => 'dashboard.media.destroy',
]]);
});
});
$router->get('admin/grid-files', 'Modules\Media\Http\Controllers\Admin\MediaController@gridFiles');
$router->group(['prefix' => 'api', 'namespace' => 'Modules\Media\Http\Controllers'], function (Router $router) {
$router->resource('file', 'Api\MediaController', ['only' => ['store']]);
});
<?php namespace Modules\Media\Image\Facade;
use Illuminate\Support\Facades\Facade;
class Imagy extends Facade
{
protected static function getFacadeAccessor() { return 'imagy'; }
}
<?php namespace Modules\Media\Image;
interface ImageFactoryInterface
{
/**
* Return a new Manipulation class
* @param string $manipulation
* @return \Modules\Media\Image\ImageHandlerInterface
*/
public function make($manipulation);
}
<?php namespace Modules\Media\Image;
interface ImageHandlerInterface
{
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options);
}
<?php namespace Modules\Media\Image;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider;
use Modules\Media\Image\Intervention\InterventionFactory;
class ImageServiceProvider extends ServiceProvider
{
public function boot()
{
$this->package('nwidart/imagy');
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->bind(
'Modules\Media\Image\ImageFactoryInterface',
'Modules\Media\Image\Intervention\InterventionFactory'
);
$this->app['imagy'] = $this->app->share(function ($app) {
$factory = new InterventionFactory;
$thumbnailManager = new ThumbnailsManager($app['config'], $app['modules']);
return new Imagy($factory, $thumbnailManager, $app['config']);
});
$this->app->booting(function()
{
$loader = AliasLoader::getInstance();
$loader->alias('Imagy', 'Modules\Media\Image\Facade\Imagy');
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['imagy'];
}
}
<?php namespace Modules\Media\Image;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Support\Facades\App;
use Modules\Media\Entities\File;
class Imagy
{
/**
* @var \Intervention\Image\Image
*/
private $image;
/**
* @var \Illuminate\Filesystem\Filesystem
*/
private $finder;
/**
* @var ImageFactoryInterface
*/
private $imageFactory;
/**
* @var ThumbnailsManager
*/
private $manager;
/**
* All the different images types where thumbnails should be created
* @var array
*/
private $imageExtensions = ['jpg', 'png', 'jpeg', 'gif'];
/**
* @var Repository
*/
private $config;
/**
* @param ImageFactoryInterface $imageFactory
* @param ThumbnailsManager $manager
* @param Repository $config
*/
public function __construct(ImageFactoryInterface $imageFactory, ThumbnailsManager $manager, Repository $config)
{
$this->image = App::make('Intervention\Image\ImageManager');
$this->finder = App::make('Illuminate\Filesystem\Filesystem');
$this->imageFactory = $imageFactory;
$this->manager = $manager;
$this->config = $config;
}
/**
* Get an image in the given thumbnail options
* @param string $path
* @param string $thumbnail
* @param bool $forceCreate
* @return string
*/
public function get($path, $thumbnail, $forceCreate = false)
{
if (!$this->isImage($path)) {
return;
}
$filename = $this->config->get('media::config.files-path') . $this->newFilename($path, $thumbnail);
if ($this->returnCreatedFile($filename, $forceCreate)) {
return $filename;
}
$this->makeNew($path, $filename, $thumbnail);
return $filename;
}
/**
* Return the thumbnail path
* @param string $originalImage
* @param string $thumbnail
* @return string
*/
public function getThumbnail($originalImage, $thumbnail)
{
if (!$this->isImage($originalImage)) {
return $originalImage;
}
return $this->config->get('media::config.files-path') . $this->newFilename($originalImage, $thumbnail);
}
/**
* Create all thumbnails for the given image path
* @param string $path
*/
public function createAll($path)
{
if (!$this->isImage($path)) {
return;
}
foreach ($this->manager->all() as $thumbName => $filters) {
$image = $this->image->make(public_path() . $path);
$filename = $this->config->get('media::config.files-path') . $this->newFilename($path, $thumbName);
foreach ($filters as $manipulation => $options) {
$image = $this->imageFactory->make($manipulation)->handle($image, $options);
}
$image = $image->encode(pathinfo($path, PATHINFO_EXTENSION));
$this->writeImage($filename, $image);
}
}
/**
* 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);
}
/**
* Return the already created file if it exists and force create is false
* @param string $filename
* @param bool $forceCreate
* @return bool
*/
private function returnCreatedFile($filename, $forceCreate)
{
return $this->finder->isFile(public_path() . $filename) && !$forceCreate;
}
/**
* Write the given image
* @param string $filename
* @param string $image
*/
private function writeImage($filename, $image)
{
$this->finder->put(public_path() . $filename, $image);
}
/**
* Make a new image
* @param string $path
* @param string $filename
* @param string null $thumbnail
*/
private function makeNew($path, $filename, $thumbnail)
{
$image = $this->image->make(public_path() . $path);
foreach ($this->manager->find($thumbnail) as $manipulation => $options) {
$image = $this->imageFactory->make($manipulation)->handle($image, $options);
}
$image = $image->encode(pathinfo($path, PATHINFO_EXTENSION));
$this->writeImage($filename, $image);
}
/**
* Check if the given path is en image
* @param string $path
* @return bool
*/
public function isImage($path)
{
return in_array(pathinfo($path, PATHINFO_EXTENSION), $this->imageExtensions);
}
/**
* Delete all files on disk for the given file in storage
* This means the original and the thumbnails
* @param $file
* @return bool
*/
public function deleteAllFor(File $file)
{
if (!$this->isImage($file->path)) {
return $this->finder->delete($file->path);
}
$paths[] = public_path() . $file->path;
$fileName = pathinfo($file->path, PATHINFO_FILENAME);
$extension = pathinfo($file->path, PATHINFO_EXTENSION);
foreach ($this->manager->all() as $thumbnail => $filters) {
$paths[] = public_path() . $this->config->get(
'media::config.files-path'
) . "{$fileName}_{$thumbnail}.{$extension}";
}
return $this->finder->delete($paths);
}
}
<?php namespace Modules\Media\Image\Intervention;
use Modules\Media\Image\ImageFactoryInterface;
class InterventionFactory implements ImageFactoryInterface
{
/**
* @param string $manipulation
* @return \Modules\Media\Image\ImageHandlerInterface
*/
public function make($manipulation)
{
$class = 'Modules\\Media\\Image\\Intervention\\Manipulations\\'. ucfirst($manipulation);
return new $class;
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Blur implements ImageHandlerInterface
{
private $defaults = [
'amount' => 1
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
return $image->blur($options['amount']);
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Brightness implements ImageHandlerInterface
{
private $defaults = [
'level' => 1
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
return $image->brightness($options['level']);
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Colorize implements ImageHandlerInterface
{
private $defaults = [
'red' => 100,
'green' => 100,
'blue' => 100,
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
return $image->colorize($options['red'], $options['green'], $options['blue']);
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Contrast implements ImageHandlerInterface
{
private $defaults = [
'level' => 0
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
return $image->contrast($options['level']);
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Crop implements ImageHandlerInterface
{
private $defaults = [
'width' => '100',
'height' => '100',
'x' => null,
'y' => null,
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return mixed
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
return $image->crop($options['width'], $options['height'], $options['x'], $options['y']);
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Fit implements ImageHandlerInterface
{
private $defaults = [
'width' => 100,
'height' => null,
'position' => 'center'
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
$callback = isset($options['callback']) ? $options['callback'] : null;
return $image->fit($options['width'], $options['height'], $callback, $options['position']);
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Flip implements ImageHandlerInterface
{
private $defaults = [
'mode' => 'h'
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
return $image->flip($options['mode']);
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Gamma implements ImageHandlerInterface
{
private $defaults = [
'correction' => 0
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
return $image->gamma($options['correction']);
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Greyscale implements ImageHandlerInterface
{
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
return $image->greyscale();
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Heighten implements ImageHandlerInterface
{
private $defaults = [
'height' => 0,
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
$callback = isset($options['callback']) ? $options['callback'] : null;
return $image->heighten($options['height'], $callback);
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Invert implements ImageHandlerInterface
{
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
return $image->invert();
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class LimitColors implements ImageHandlerInterface
{
private $defaults = [
'count' => 255,
'matte' => null
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
return $image->limitColors($options['count'], $options['matte']);
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Opacity implements ImageHandlerInterface
{
private $defaults = [
'transparency' => 50
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
return $image->opacity($options['transparency']);
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Orientate implements ImageHandlerInterface
{
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
return $image->orientate();
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Pixelate implements ImageHandlerInterface
{
private $defaults = [
'size' => 0
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
return $image->pixelate($options['size']);
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Resize implements ImageHandlerInterface
{
private $defaults = [
'width' => 200,
'height' => 200,
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
$callback = isset($options['callback']) ? $options['callback'] : null;
return $image->resize($options['width'], $options['height'], $callback);
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Rotate implements ImageHandlerInterface
{
private $defaults = [
'angle' => 45,
'bgcolor' => '#000000'
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
return $image->rotate($options['angle'], $options['bgcolor']);
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Sharpen implements ImageHandlerInterface
{
private $defaults = [
'amount' => 10
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
return $image->sharpen($options['amount']);
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Trim implements ImageHandlerInterface
{
private $defaults = [
'base' => 'top-left',
'away' => ['top', 'bottom', 'left', 'right'],
'tolerance' => 0,
'feather' => 0,
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
return $image->trim($options['base'], $options['away'], $options['tolerance'], $options['feather']);
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Widen implements ImageHandlerInterface
{
private $defaults = [
'width' => 0
];
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options)
{
$options = array_merge($this->defaults, $options);
$callback = isset($options['callback']) ? $options['callback'] : null;
return $image->widen($options['width'], $callback);
}
}
<?php namespace Modules\Media\Image;
use Illuminate\Contracts\Config\Repository;
class ThumbnailsManager
{
/**
* @var Module
*/
private $module;
/**
* @var Repository
*/
private $config;
/**
* @param Repository $config
*/
public function __construct(Repository $config)
{
$this->module = app('modules');
$this->config = $config;
}
/**
* Return all thumbnails for all modules
* @return array
*/
public function all()
{
$thumbnails = [];
foreach ($this->module->enabled() as $enabledModule) {
$configuration = $this->config->get(strtolower($enabledModule->getName()) . '::thumbnails');
if (!is_null($configuration)) $thumbnails = array_merge($thumbnails, $configuration);
}
return $thumbnails;
}
/**
* Find the filters for the given thumbnail
* @param $thumbnail
*/
public function find($thumbnail)
{
foreach ($this->all() as $thumbName => $filters) {
if ($thumbName == $thumbnail) {
return $filters;
}
}
}
}
<?php namespace Modules\Media\Providers;
use Illuminate\Support\ServiceProvider;
use Modules\Media\Console\RefreshThumbnailCommand;
use Modules\Media\Entities\File;
use Modules\Media\Image\Imagy;
use Modules\Media\Image\Intervention\InterventionFactory;
use Modules\Media\Image\ThumbnailsManager;
use Modules\Media\Repositories\Eloquent\EloquentFileRepository;
class MediaServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->booted(function () {
$this->registerBindings();
});
$this->registerCommands();
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array();
}
private function registerBindings()
{
$this->app->bind(
'Modules\Media\Repositories\FileRepository',
function($app) {
return new EloquentFileRepository(new File, $app['filesystem.disk']);
}
);
}
/**
* Register all commands for this module
*/
private function registerCommands()
{
$this->registerRefreshCommand();
}
/**
* Register the refresh thumbnails command
*/
private function registerRefreshCommand()
{
$this->app->bindShared('command.media.refresh', function($app) {
$thumbnailManager = new ThumbnailsManager($app['config'], $app['modules']);
$imagy = new Imagy(new InterventionFactory, $thumbnailManager, $app['config']);
return new RefreshThumbnailCommand($imagy, $app['Modules\Media\Repositories\FileRepository']);
});
$this->commands(
'command.media.refresh'
);
}
}
<?php namespace Modules\Media\Providers;
use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* The root namespace to assume when generating URLs to actions.
*
* @var string
*/
protected $rootUrlNamespace = 'Modules\Setting\Http\Controllers';
/**
* The controllers to scan for route annotations.
*
* @var array
*/
protected $scan = [
'Modules\Setting\Http\Controllers',
];
/**
* Called before routes are registered.
*
* Register any model bindings or pattern based filters.
*
* @param Router $router
* @return void
*/
public function before(Router $router)
{
//
}
/**
* Define the routes for the application.
*
* @return void
*/
public function map(Router $router)
{
require __DIR__ . '/../Http/routes.php';
}
}
<?php namespace Modules\Media\Repositories\Eloquent;
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Media\Entities\File;
use Modules\Media\Helpers\FileHelper;
use Modules\Media\Repositories\FileRepository;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class EloquentFileRepository extends EloquentBaseRepository implements FileRepository
{
/**
* Update a resource
* @param File $file
* @param $data
* @return mixed
*/
public function update($file, $data)
{
$file->update($data);
return $file;
}
/**
* Create a file row from the given file
* @param UploadedFile $file
* @return mixed
*/
public function createFromFile(UploadedFile $file)
{
$fileName = FileHelper::slug($file->getClientOriginalName());
return $this->model->create([
'filename' => $fileName,
'path' => "/assets/media/{$fileName}",
'extension' => $file->guessClientExtension(),
'mimetype' => $file->getClientMimeType(),
'filesize' => $file->getFileInfo()->getSize(),
]);
}
public function destroy($file)
{
$file->delete();
}
}
<?php namespace Modules\Media\Repositories;
use Modules\Core\Repositories\BaseRepository;
use Symfony\Component\HttpFoundation\File\UploadedFile;
interface FileRepository extends BaseRepository
{
/**
* Create a file row from the given file
* @param UploadedFile $file
* @return mixed
*/
public function createFromFile(UploadedFile $file);
}
<?php
return [
'title' => [
'media' => 'Media',
'edit media' => 'Edit media'
],
'breadcrumb' => [
'media' => 'Media'
],
'table' => [
'filename' => 'Filename',
'width' => 'Width',
'height' => 'Height'
],
'form' => [
'alt_attribute' => 'Alt attribute',
'description' => 'Description',
'keywords' => 'Keywords',
],
'choose file' => 'Choose a file',
'insert' => 'Insert this file',
'file picker' => 'File Picker'
];
<?php
return [
'file updated' => 'File updated',
'file deleted' => 'File deleted',
];
<?php
return [
'title' => [
'media' => 'Média',
'edit media' => 'Edition de média'
],
'breadcrumb' => [
'media' => 'Média'
],
'table' => [
'filename' => 'Nom de fichier',
'width' => 'Largeur',
'height' => 'Hauteur'
],
'choose file' => 'Choissisez un fichier',
'insert' => 'Sélectionner ce fichier',
'file picker' => 'Sélectionneur de fichier'
];
<?php
return [
'file updated' => 'Fichier mit à jour',
'file deleted' => 'File suprimer',
];
@extends('core::layouts.master')
@section('content-header')
<h1>
{{ trans('media::media.title.edit media') }} <small>{{ $file->name }}</small>
</h1>
<ol class="breadcrumb">
<li><a href="{{ URL::route('dashboard.index') }}"><i class="fa fa-dashboard"></i> {{ trans('core::core.breadcrumb.home') }}</a></li>
<li><a href="{{ URL::route('dashboard.media.index') }}">{{ trans('media::media.title.media') }}</a></li>
<li class="active">{{ trans('media::media.title.edit media') }}</li>
</ol>
@stop
@section('content')
{!! Form::open(['route' => ['dashboard.media.update', $file->id], 'method' => 'put']) !!}
<div class="row">
<div class="col-md-8">
<div class="nav-tabs-custom">
@include('core::partials.form-tab-headers')
<div class="tab-content">
<?php $i = 0; ?>
<?php foreach(LaravelLocalization::getSupportedLocales() as $locale => $language): ?>
<?php $i++; ?>
<div class="tab-pane {{ App::getLocale() == $locale ? 'active' : '' }}" id="tab_{{ $i }}">
@include('media::admin.partials.edit-fields', ['lang' => $locale])
</div>
<?php endforeach; ?>
<div class="box-footer">
<button type="submit" class="btn btn-primary btn-flat">{{ trans('core::core.button.update') }}</button>
<a class="btn btn-danger pull-right btn-flat" href="{{ URL::route('dashboard.media.index')}}"><i class="fa fa-times"></i> {{ trans('core::core.button.cancel') }}</a>
</div>
</div>
</div> {{-- end nav-tabs-custom --}}
</div>
<div class="col-md-4">
<img src="{{ $file->path }}" alt="" style="width: 100%;"/>
</div>
</div>
{!! Form::close() !!}
@stop
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ trans('media::media.file picker') }}</title>
<link href="{!! Module::asset('core:css/vendor/bootstrap.min.css') !!}" rel="stylesheet" type="text/css" />
<script src="{!! Module::asset('core:js/vendor/jquery.min.js') !!}"></script>
<style>
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-8">
<h1>{{ trans('media::media.choose file') }}</h1>
<?php if ($files): ?>
<ul class="list-unstyled">
<?php foreach($files as $file): ?>
<li class="pull-left" style="margin-right: 20px">
<img src="{{ $file->path }}" alt="" class="img-thumbnail" style="width: 250px;"/>
<a class="jsInsertImage btn btn-primary btn-flat" href="#" style="display: block">{{ trans('media::media.insert') }}</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
<script>
$( document ).ready(function() {
$('.jsInsertImage').on('click', function(e) {
e.preventDefault();
function getUrlParam( paramName ) {
var reParam = new RegExp( '(?:[\?&]|&)' + paramName + '=([^&]+)', 'i' ) ;
var match = window.location.search.match(reParam) ;
return ( match && match.length > 1 ) ? match[ 1 ] : null ;
}
var funcNum = getUrlParam( 'CKEditorFuncNum' );
window.opener.CKEDITOR.tools.callFunction( funcNum, $(this).parent().find('img').attr('src') );
window.close();
});
});
</script>
</body>
</html>
@extends('core::layouts.master')
@section('content-header')
<h1>
{{ trans('media::media.title.media') }}
</h1>
<ol class="breadcrumb">
<li><a href="{{ URL::route('dashboard.index') }}"><i class="fa fa-dashboard"></i> {{ trans('core::core.breadcrumb.home') }}</a></li>
<li><i class="fa fa-camera"></i> {{ trans('media::media.breadcrumb.media') }}</li>
</ol>
@stop
@section('styles')
<link href="{!! Module::asset('media:css/dropzone.css') !!}" rel="stylesheet" type="text/css" />
<style>
.dropzone {
border: 1px dashed #CCC;
min-height: 227px;
margin-bottom: 20px;
}
</style>
@stop
@section('content')
<div class="row col-md-12">
<form action="{{ URL::route('api.file.store')}}" method="POST" class="dropzone">
{!! Form::token() !!}
</form>
</div>
<div class="row">
<div class="col-md-12">
<div class="box box-info">
<div class="box-body table-responsive">
<table class="data-table table table-bordered table-hover jsFileList">
<thead>
<tr>
<th>{{ trans('media::media.table.filename') }}</th>
<th>{{ trans('core::core.table.thumbnail') }}</th>
<th>{{ trans('media::media.table.filename') }}</th>
<th>{{ trans('core::core.table.actions') }}</th>
</tr>
</thead>
<tbody>
<?php if ($files): ?>
<?php foreach($files as $file): ?>
<tr>
<td>
<a href="{{ URL::route('dashboard.media.edit', [$file->id]) }}">
{{ $file->created_at }}
</a>
</td>
<td>
<img src="{{ Imagy::getThumbnail($file->path, 'smallThumb') }}" alt=""/>
</td>
<td>
<a href="{{ URL::route('dashboard.media.edit', [$file->id]) }}">
{{ $file->filename }}
</a>
</td>
<td>
<div class="btn-group">
<a href="{{ URL::route('dashboard.media.edit', [$file->id]) }}" class="btn btn-default btn-flat"><i class="glyphicon glyphicon-pencil"></i></a>
<button class="btn btn-danger btn-flat" data-toggle="modal" data-target="#confirmation-{{ $file->id }}"><i class="glyphicon glyphicon-trash"></i></button>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
<tfoot>
<tr>
<th>{{ trans('core::core.table.created at') }}</th>
<th>{{ trans('core::core.table.thumbnail') }}</th>
<th>{{ trans('media::media.table.filename') }}</th>
<th>{{ trans('core::core.table.actions') }}</th>
</tr>
</tfoot>
</table>
<!-- /.box-body -->
</div>
</div>
</div>
</div>
<?php if ($files): ?>
<?php foreach($files as $file): ?>
<!-- Modal -->
<div class="modal fade" id="confirmation-{{ $file->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">{{ trans('core::core.modal.title') }}</h4>
</div>
<div class="modal-body">
{{ trans('core::core.modal.confirmation-message') }}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">{{ trans('core::core.button.cancel') }}</button>
{!! Form::open(['route' => ['dashboard.media.destroy', $file->id], 'method' => 'delete', 'class' => 'pull-left']) !!}
<button type="submit" class="btn btn-danger btn-flat"><i class="glyphicon glyphicon-trash"></i> {{ trans('core::core.button.delete') }}</button>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
@stop
@section('scripts')
<script src="{!! Module::asset('media:js/dropzone.js') !!}"></script>
<script>
$( document ).ready(function() {
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone(".dropzone", {
url: $(this).attr('action'),
autoProcessQueue: true,
maxFilesize: '<?php echo $config["max-file-size"] ?>',
acceptedFiles : '<?php echo $config["allowed-types"] ?>'
});
myDropzone.on("success", function(file, http) {
var _this = this;
window.setTimeout(function(){
$(file.previewElement).fadeOut('fast', function(){
_this.removeFile(file);
});
var tableRow = '<tr><td>' + http.created_at + '</td><td></td><td>'+http.filename+'</td><td></td></tr>';
var elem = $(tableRow).css('display', 'none');
$('.jsFileList tbody').prepend(elem);
elem.fadeIn();
}, 1000);
});
});
</script>
<?php $locale = App::getLocale(); ?>
<script type="text/javascript">
$(function () {
$('.data-table').dataTable({
"paginate": true,
"lengthChange": true,
"filter": true,
"sort": true,
"info": true,
"autoWidth": true,
"order": [[ 0, "desc" ]],
"language": {
"url": '<?php echo Module::asset("core:js/vendor/datatables/{$locale}.json") ?>'
},
"columns": [
null,
null,
null,
{ "sortable": false }
]
});
});
</script>
@stop
<?php $altAttribute = isset($file->translate($lang)->alt_attribute) ? $file->translate($lang)->alt_attribute : '' ?>
<div class='form-group{{ $errors->has("{$lang}[alt_attribute]") ? ' has-error' : '' }}'>
{!! Form::label("{$lang}[alt_attribute]", trans('media::media.form.alt_attribute')) !!}
{!! Form::text("{$lang}[alt_attribute]", Input::old("{$lang}[alt_attribute]", $altAttribute), ['class' => 'form-control', 'placeholder' => trans('media::media.form.alt_attribute')]) !!}
{!! $errors->first("{$lang}[alt_attribute]", '<span class="help-block">:message</span>') !!}
</div>
<?php $description = isset($file->translate($lang)->description) ? $file->translate($lang)->alt_attribute : '' ?>
<div class='form-group{{ $errors->has("{$lang}[description]") ? ' has-error' : '' }}'>
{!! Form::label("{$lang}[description]", trans('media::media.form.description')) !!}
{!! Form::textarea("{$lang}[description]", Input::old("{$lang}[description]", $description), ['class' => 'form-control', 'placeholder' => trans('media::media.form.description')]) !!}
{!! $errors->first("{$lang}[description]", '<span class="help-block">:message</span>') !!}
</div>
<?php $keywords = isset($file->translate($lang)->keywords) ? $file->translate($lang)->alt_attribute : '' ?>
<div class='form-group{{ $errors->has("{$lang}[keywords]") ? ' has-error' : '' }}'>
{!! Form::label("{$lang}[keywords]", trans('media::media.form.keywords')) !!}
{!! Form::text("{$lang}[keywords]", Input::old("{$lang}[keywords]", $keywords), ['class' => 'form-control', 'placeholder' => trans('media::media.form.keywords')]) !!}
{!! $errors->first("{$lang}[keywords]", '<span class="help-block">:message</span>') !!}
</div>
<?php namespace Modules\Media\Services;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Queue\Queue;
use Illuminate\Queue\Jobs\Job;
use Illuminate\Support\Facades\App;
use Modules\Media\Repositories\FileRepository;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class FileService
{
/**
* @var FileRepository
*/
private $file;
/**
* @var Repository
*/
private $config;
/**
* @var Queue
*/
private $queue;
public function __construct(
FileRepository $file,
Repository $config,
Queue $queue)
{
$this->file = $file;
$this->config = $config;
$this->queue = $queue;
}
/**
* @param UploadedFile $file
* @return mixed
*/
public function store(UploadedFile $file)
{
// Save the file info to db
$savedFile = $this->file->createFromFile($file);
// Move the uploaded file to files path
$file->move(public_path() . $this->config->get('media::config.files-path'), $savedFile->filename);
$this->createThumbnails($savedFile);
return $savedFile;
}
/**
* Create the necessary thumbnails for the given file
* @param $savedFile
*/
private function createThumbnails($savedFile)
{
$this->queue->push(function(Job $job) use ($savedFile)
{
App::make('imagy')->createAll($savedFile->path);
$job->delete();
});
}
}
<?php namespace Modules\Media\Tests;
use Modules\Core\Tests\BaseTestCase;
use Modules\Media\Helpers\FileHelper;
class FileHelperTest extends BaseTestCase
{
/** @test */
public function it_should_return_slugged_name_with_extension()
{
$expected = 'file-name.png';
$name = FileHelper::slug('File Name.png');
$this->assertEquals($expected, $name);
}
}
<?php namespace Modules\Media\Tests;
use Illuminate\Support\Facades\App;
use Modules\Core\Tests\BaseTestCase;
use Modules\Media\Image\Imagy;
use Modules\Media\Image\Intervention\InterventionFactory;
use Modules\Media\Image\ThumbnailsManager;
class ImagyTest extends BaseTestCase
{
/**
* @var Imagy
*/
protected $imagy;
/**
* @var \Illuminate\Filesystem\Filesystem
*/
protected $finder;
/**
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;
/**
* @var string
*/
protected $mediaPath;
/**
*
*/
public function setUp()
{
parent::setUp();
$this->config = App::make('Illuminate\Contracts\Config\Repository');
$module = App::make('modules');
$this->finder = App::make('Illuminate\Filesystem\Filesystem');
$this->imagy = new Imagy(new InterventionFactory, new ThumbnailsManager($this->config, $module), $this->config);
$this->mediaPath = $this->config->get('media::config.files-path');
}
/** @test */
public function it_should_create_a_file()
{
if ($this->finder->isFile(public_path() . "{$this->mediaPath}google-map_smallThumb.png")) {
$this->finder->delete(public_path() . "{$this->mediaPath}google-map_smallThumb.png");
}
$this->imagy->get("{$this->mediaPath}google-map.png", 'smallThumb', true);
$this->assertTrue($this->finder->isFile(public_path() . "{$this->mediaPath}google-map_smallThumb.png"));
}
/** @test */
public function it_should_not_create_thumbs_for_pdf_files()
{
$this->imagy->get("{$this->mediaPath}test-pdf.pdf", 'smallThumb', true);
$this->assertFalse($this->finder->isFile(public_path() . "{$this->mediaPath}test-pdf_smallThumb.png"));
}
/** @test */
public function it_should_return_thumbnail_path()
{
$path = $this->imagy->getThumbnail("{$this->mediaPath}google-map.png", 'smallThumb');
$expected = "{$this->mediaPath}google-map_smallThumb.png";
$this->assertEquals($expected, $path);
}
/** @test */
public function it_should_return_same_path_for_non_images()
{
$path = $this->imagy->getThumbnail("{$this->mediaPath}test-pdf.pdf", 'smallThumb');
$expected = "{$this->mediaPath}test-pdf.pdf";
$this->assertEquals($expected, $path);
}
/** @test */
public function it_should_detect_an_image()
{
$jpg = $this->imagy->isImage('image.jpg');
$png = $this->imagy->isImage('image.png');
$pdf = $this->imagy->isImage('pdf.pdf');
$this->assertTrue($jpg);
$this->assertTrue($png);
$this->assertFalse($pdf);
}
}
{
"name": "asgardcms/media-module",
"type": "asgard-module",
"description": "Media module for AsgardCMS. Handles the media library.",
"keywords": [
"asgardcms",
"media",
"files",
"library",
"thumbnails",
"filters"
],
"license": "MIT",
"authors": [
{
"name": "Nicolas Widart",
"email": "info@asgardcms.com",
"role": "Developer"
}
],
"support": {
"email": "support@asgardcms.com",
"issues": "https://github.com/AsgardCms/Media/issues",
"source": "https://github.com/AsgardCms/Media"
},
"require": {
"php": ">=5.4",
"composer/installers": "~1.0"
},
"minimum-stability": "dev"
}
<?php
View::composer('core::partials.sidebar-nav', 'Modules\Media\Composers\SidebarViewComposer');
{
"name": "Media",
"alias": "media",
"description": "",
"keywords": [
],
"active": 1,
"require": {
"intervention/image": "~2.0"
},
"providers": [
"Modules\\Media\\Providers\\MediaServiceProvider",
"Modules\\Media\\Providers\\RouteServiceProvider",
"Modules\\Media\\Image\\ImageServiceProvider"
],
"files": [
"start.php"
]
}
# Media Module
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/648270bf-8b9c-4994-b006-a948fef307b2/mini.png)](https://insight.sensiolabs.com/projects/648270bf-8b9c-4994-b006-a948fef307b2)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/AsgardCms/Media/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/AsgardCms/Media/?branch=master)
[![Code Climate](https://codeclimate.com/github/AsgardCms/Media/badges/gpa.svg)](https://codeclimate.com/github/AsgardCms/Media)
<?php
require __DIR__ . '/composers.php';
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment