Skip to content

Commit 8168461

Browse files
authored
Merge pull request #1 from nauvalazhar/dev-master
Beta version is ready
2 parents 73618e6 + a79ef28 commit 8168461

File tree

15 files changed

+5195
-2
lines changed

15 files changed

+5195
-2
lines changed

README.md

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,111 @@
1-
# midia
2-
Simple Media manager for your Laravel project
1+
# Midia (Beta)
2+
Simple media manager for your Laravel project. This package lets you open your files as inline modal.
3+
4+
# Features
5+
- Fully responsive
6+
- Read, rename, delete file
7+
- Infinite scroll
8+
- Search
9+
- Upload multiple
10+
- More ...
11+
12+
# Requirements
13+
- PHP >= 5.6.4
14+
- jQuery >= 1.12
15+
- Dropzone.js
16+
- Laravel 5
17+
18+
# Tested
19+
- [ ] Laravel 5.5
20+
- [x] Laravel 5.4
21+
- [ ] Laravel 5.3
22+
- [ ] Laravel 5.2
23+
- [ ] Laravel 5.1
24+
- [ ] Laravel 5.0
25+
26+
~~ # Installation (Development Mode)
27+
This package is still available for development only, not ready for production mode 'cause as it has to do testing to find some bugs and compatibility.
28+
1. Create a new folder `packages/nauvalazhar`
29+
2. Clone this package inside `packages/nauvalazhar`
30+
3. Add these lines manually in the `autoload` key in your `composer.json` file
31+
```
32+
"psr-4": {
33+
"Nauvalazhar\\Midia\\": "packages/nauvalazhar/midia/src/"
34+
},
35+
```
36+
4. Run `composer dump-autoload`
37+
5. Done~~
38+
39+
Now, this package is available for production but still beta, need further testing. You can install this package using these steps.
40+
1. Run `composer require nauvalazhar/midia`
41+
2. Put this line into `config/app.php` in the `providers`
42+
```
43+
Nauvalazhar\Midia\MidiaServiceProvider::class,
44+
```
45+
3. Done
46+
47+
48+
# Usage
49+
1. Run `php artisan vendor:publish --tag=midia`
50+
2. Add these lines before the `</head>` tag
51+
```
52+
<link rel="stylesheet" href="{{asset('vendor/midia/midia.css')}}">
53+
<link rel="stylesheet" href="{{asset('vendor/midia/dropzone.css')}}">
54+
```
55+
3. Add these lines before the `</body>` tag
56+
```
57+
<script src="{{asset('vendor/midia/dropzone.js')}}"></script>
58+
<script src="{{asset('vendor/midia/midia.js')}}"></script>
59+
```
60+
61+
# Integration
62+
Here we have documented how to use it with TinyMCE 4 and as a stand-alone button. But, you can also try it yourself to be integrated with other editors like: CKEditor, Summernote, etc.
63+
64+
If you successfully integrate with other editors, then you can either create `issue` or change the `readme.md` file to document how you do it.
65+
66+
### TinyMCE 4
67+
```
68+
<textarea class="tinymce"></textarea>
69+
<script>
70+
var editor_config = {
71+
path_absolute : "{{url('')}}/",
72+
selector: "textarea.tinymce",
73+
plugins: [
74+
"advlist autolink lists link image charmap print preview hr anchor pagebreak",
75+
"searchreplace wordcount visualblocks visualchars code fullscreen",
76+
"insertdatetime media nonbreaking save table contextmenu directionality",
77+
"emoticons template paste textcolor colorpicker textpattern"
78+
],
79+
toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image media",
80+
relative_urls: false,
81+
file_browser_callback : function(field_name, url, type, win) {
82+
var x = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth;
83+
var y = window.innerHeight|| document.documentElement.clientHeight|| document.getElementsByTagName('body')[0].clientHeight;
84+
85+
var cmsURL = editor_config.path_absolute + 'midia/open/tinymce4?field_name=' + field_name;
86+
87+
tinyMCE.activeEditor.windowManager.open({
88+
file : cmsURL,
89+
title : 'Filemanager',
90+
width : x * 0.8,
91+
height : y * 0.8,
92+
resizable : "yes",
93+
close_previous : "no"
94+
});
95+
}
96+
};
97+
98+
tinymce.init(editor_config);
99+
</script>
100+
```
101+
### Standalone Button
102+
```
103+
<input type="text" id="my-file">
104+
<button class="midia-toggle">Select File</button>
105+
106+
<script>
107+
$(".midia-toggle").midia({
108+
base_url: '{{url('')}}'
109+
});
110+
</script>
111+
```

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "nauvalazhar/midia",
3+
"description": "Simple Media manager for your Laravel project",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Muhamad Nauval Azhar",
9+
"email": "nauvalazhar2@gmail.com"
10+
}
11+
],
12+
"require": {},
13+
"autoload": {
14+
"classmap": [
15+
"src"
16+
],
17+
"psr-4": {
18+
"Nauvalazhar\\Midia\\": "src/"
19+
}
20+
}
21+
}

src/Config/midia.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
return [
3+
// Target diirectory
4+
'directory' => storage_path('media'),
5+
// For URL (e.g: http://base/media/filename.ext)
6+
'directory_name' => 'media',
7+
'prefix' => 'midia'
8+
];

src/Controller/MidiaController.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
namespace Nauvalazhar\Midia\Controller;
3+
4+
use Illuminate\Routing\Controller;
5+
use Illuminate\Http\Request;
6+
use File;
7+
8+
class MidiaController extends Controller {
9+
protected $directory;
10+
protected $directory_name;
11+
12+
public function __construct() {
13+
$this->directory = config('midia.directory', storage_path('media'));
14+
$this->directory_name = config('midia.directory_name', 'media');
15+
}
16+
17+
public function index($limit) {
18+
$dir = $this->directory;
19+
$q = request()->key;
20+
21+
if(!is_dir($dir)) {
22+
return response(['data' => 'Directory can\'t be found'], 404);
23+
}
24+
25+
$exec = scandir($dir);
26+
$exec = array_splice($exec, 2);
27+
28+
$files = [];
29+
foreach($exec as $file) {
30+
$files[$file] = filemtime($dir . '/' . $file);
31+
}
32+
33+
arsort($files);
34+
$files = array_keys($files);
35+
$exec = $files;
36+
37+
$_files = [];
38+
foreach($exec as $i => $item) {
39+
if(!is_dir($this->directory . '/' . $item)) {
40+
$_files[$i]['fullname'] = $item;
41+
$_files[$i]['name'] = pathinfo($item, PATHINFO_FILENAME);
42+
$_files[$i]['url'] = url($this->directory_name . '/' . $item);
43+
$_files[$i]['extension'] = pathinfo($item,PATHINFO_EXTENSION);
44+
$_files[$i]['size'] = $this->toMb(filesize($this->directory . '/' . $item));
45+
$_files[$i]['filetime'] = filemtime($this->directory . '/' . $item);
46+
}
47+
}
48+
49+
$total_all = count($_files);
50+
51+
if(isset($q) && trim($q) !== '') {
52+
$_search = [];
53+
foreach($_files as $key => $file) {
54+
if(strpos($file['fullname'], $q) !== false) {
55+
$_search[$key] = $file;
56+
}
57+
}
58+
$_files = $_search;
59+
}
60+
61+
$page = $limit;
62+
$perPage = 20;
63+
$offset = ($page * $perPage) - $perPage;
64+
65+
$exec = array_slice($_files, $offset, $perPage);
66+
$dir = json_encode(["files" => $exec, "total" => count($_files), "total_all" => $total_all]);
67+
68+
return response($dir, 200)
69+
->header('Content-Type', 'application/json');
70+
}
71+
72+
public function toMb($bytes) {
73+
for ($i=0;$bytes>=1024&&$i<5;$i++)
74+
$bytes/=1024;
75+
76+
return round($bytes,2).[' B',' KB',' MB',' GB',' TB',' PB'][$i];
77+
}
78+
79+
public function open($editor) {
80+
return view('midia::app', compact('editor'));
81+
}
82+
83+
public function delete($file) {
84+
unlink($this->directory . '/' . $file);
85+
return response(['data' => 'ok'], 200)
86+
->header('Content-Type', 'application/json');
87+
}
88+
89+
public function upload(Request $request) {
90+
$file = $request->file('file');
91+
$fileName = $file->getClientOriginalName();
92+
$wo_extension = basename($fileName, "." . $file->getClientOriginalExtension());
93+
94+
$inc = 1;
95+
while(file_exists($this->directory . '/' . $fileName)) {
96+
$name = $wo_extension . '-' . $inc;
97+
$fileName = $name . '.' . $file->getClientOriginalExtension();
98+
$inc++;
99+
}
100+
$file->move($this->directory, $fileName);
101+
return response()->json(['success'=>$fileName]);
102+
}
103+
104+
public function rename(Request $request, $file) {
105+
$fileName = $request->newName;
106+
$wo_extension = pathinfo($fileName, PATHINFO_FILENAME);
107+
$_extension = explode(".", $fileName);
108+
$extension = end($_extension);
109+
110+
$inc = 1;
111+
while(file_exists($this->directory . '/' . $fileName)) {
112+
$name = $wo_extension . '-' . $inc;
113+
$fileName = $name . '.' . $extension;
114+
$inc++;
115+
}
116+
117+
rename($this->directory . '/' . $file, $this->directory . '/' . $fileName);
118+
119+
$new_data = [];
120+
$new_data['fullname'] = $fileName;
121+
$new_data['name'] = pathinfo($fileName, PATHINFO_FILENAME);
122+
$new_data['url'] = url($this->directory_name . '/' . $fileName);
123+
124+
return response()->json(['success'=>$new_data]);
125+
}
126+
127+
public function demo() {
128+
return view('midia::demo');
129+
}
130+
}

src/MidiaServiceProvider.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Nauvalazhar\Midia;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class MidiaServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap the application services.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
15+
{
16+
$this->loadRoutesFrom(__DIR__ . '/Route/web.php');
17+
$this->loadViewsFrom(__DIR__ . '/View', 'midia');
18+
19+
$this->publishes([
20+
__DIR__ . '/Config/midia.php' => config_path('midia.php'),
21+
__DIR__ . '/Public/midia.js' => public_path('vendor/midia/midia.js'),
22+
__DIR__ . '/Public/midia.css' => public_path('vendor/midia/midia.css'),
23+
__DIR__ . '/Public/dropzone.js' => public_path('vendor/midia/dropzone.js'),
24+
__DIR__ . '/Public/dropzone.css' => public_path('vendor/midia/dropzone.css'),
25+
__DIR__ . '/Public/spinner.svg' => public_path('vendor/midia/spinner.svg')
26+
], 'midia');
27+
}
28+
29+
/**
30+
* Register the application services.
31+
*
32+
* @return void
33+
*/
34+
public function register()
35+
{
36+
$this->mergeConfigFrom(__DIR__ . '/Config/midia.php', config_path('midia.php'));
37+
}
38+
}

0 commit comments

Comments
 (0)