Skip to content

Commit 3878e70

Browse files
committed
Initial Version
0 parents  commit 3878e70

16 files changed

+462
-0
lines changed

.styleci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
preset: laravel

LICENSE.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
MIT License
2+
3+
Copyright (c) BinBytes [email protected] (https://binbytes.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
23+
...Add your license text here...

changelog.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changelog
2+
3+
All notable changes to `LaravelModalMediaBackup` will be documented in this file.
4+
5+
## Version 1.0
6+
7+
### Added
8+
- Everything

composer.json

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "binbytes/laravel-model-media-backup",
3+
"description": "Take media backup for model in Laravel.",
4+
"license": "license",
5+
"authors": [
6+
{
7+
"name": "BinBytes",
8+
"email": "[email protected]",
9+
"homepage": "https://binbytes.com"
10+
}
11+
],
12+
"homepage": "https://github.com/binbytes/laravel-model-media-backup",
13+
"keywords": ["Laravel", "Model Media Backup", "Backup"],
14+
"require": {
15+
"illuminate/support": "~5"
16+
},
17+
"require-dev": {
18+
"phpunit/phpunit": "~7.0",
19+
"mockery/mockery": "^1.1",
20+
"orchestra/testbench": "~3.0",
21+
"sempro/phpunit-pretty-print": "^1.0"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"BinBytes\\ModelMediaBackup\\": "src/"
26+
}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"BinBytes\\ModelMediaBackup\\Tests\\": "tests"
31+
}
32+
},
33+
"extra": {
34+
"laravel": {
35+
"providers": [
36+
"BinBytes\\ModelMediaBackup\\ModelBackupServiceProvider"
37+
],
38+
"aliases": {
39+
"ModelMediaBackup": "BinBytes\\ModelMediaBackup\\Facades\\ModelMediaBackup"
40+
}
41+
}
42+
}
43+
}

config/modelmediabackup.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
'Models' => [
5+
// Example 'App\User'
6+
],
7+
'BACKUP_DISK' => null, // FILESYSTEM_DRIVER
8+
'ChunkSize' => 100,
9+
'EnableNotification' => false,
10+
'Notification' => [
11+
'MailTo' => null, // email address where you want to receive email alert
12+
]
13+
];

contributing.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Contributing
2+
3+
Contributions are welcome and will be fully credited.
4+
5+
Contributions are accepted via Pull Requests on [Github](https://github.com/binbytes/laravel-model-media-backup).
6+
7+
# Things you could do
8+
If you want to contribute but do not know where to start, this list provides some starting points.
9+
- Add license text
10+
- Remove rewriteRules.php
11+
- Set up TravisCI, StyleCI, ScrutinizerCI
12+
- Write a comprehensive ReadMe
13+
14+
## Pull Requests
15+
16+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
17+
18+
- **Document any change in behaviour** - Make sure the `readme.md` and any other relevant documentation are kept up-to-date.
19+
20+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
21+
22+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
23+
24+
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
25+
26+
27+
**Happy coding**!

phpunit.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Package">
14+
<directory suffix=".php">./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory>src/</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

readme.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Laravel Model Media Backup
2+
Take newly added media backup associated with any model rather than full backup on daily basis.
3+
4+
## Installation
5+
6+
Via Composer
7+
8+
``` bash
9+
$ composer require binbytes/laravel-model-media-backup
10+
```
11+
12+
## Security
13+
14+
If you discover any security related issues, please email author email instead of using the issue tracker.
15+
16+
## Credits
17+
18+
- Nikunj Kanetiya[https://github.com/nikkanetiya]
19+
20+
## License
21+
22+
license. Please see the [license file](LICENSE.md) for more information.
23+
24+
[link-author]: https://github.com/binbytes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@component('mail::message')
2+
# Backup has been completed at {{ \Carbon\Carbon::now() }}
3+
4+
@component('mail::panel')
5+
Here are the records those are taken in backup : --- {{ $records && is_array($records) ? implode(', ', $records) : 'None' }}
6+
@endcomponent
7+
8+
Thanks,<br>
9+
{{ config('app.name') }}
10+
@endcomponent

src/Commands/ModelMediaBackup.php

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace BinBytes\ModelMediaBackup\Commands;
4+
5+
use BinBytes\ModelMediaBackup\Interfaces\ShouldBackupFiles;
6+
use BinBytes\ModelMediaBackup\Mail\MediaBackupTaken;
7+
use Illuminate\Console\Command;
8+
9+
class ModelMediaBackup extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'model:media:backup';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Take backup of resource by model';
24+
25+
/**
26+
* Create a new command instance.
27+
*
28+
* @return void
29+
*/
30+
public function __construct()
31+
{
32+
parent::__construct();
33+
}
34+
35+
/**
36+
* Execute the console command.
37+
*
38+
* @return mixed
39+
*/
40+
public function handle()
41+
{
42+
$backupDisk = config('modelmediabackup.BACKUP_DISK');
43+
44+
if(!$backupDisk) {
45+
return;
46+
}
47+
48+
$storage = \Storage::disk($backupDisk);
49+
$chunkSize = config('modelmediabackup.ChunkSize');
50+
51+
foreach ($models = config('modelmediabackup.Models') as $model) {
52+
if(in_array(ShouldBackupFiles::class, class_implements($model)) === false) {
53+
continue;
54+
}
55+
56+
$recordsBackup = [];
57+
58+
$model::backupRecords()
59+
->chunk($chunkSize, function ($records) use($storage, &$recordsBackup) {
60+
$records->each(function ($record) use($storage, &$recordsBackup) {
61+
if ($backupFiles = $record->backupFiles()) {
62+
foreach (is_array($backupFiles) ? $backupFiles : [$backupFiles] as $backupFile) {
63+
if($this->takeBackup($storage, $backupFile)) {
64+
$recordsBackup[] = $record->getKey();
65+
}
66+
}
67+
}
68+
});
69+
});
70+
71+
if($recordsBackup) {
72+
$this->sendNotifications($recordsBackup);
73+
}
74+
}
75+
}
76+
77+
/**
78+
* Copy file to destination/backup disk
79+
*
80+
* @param $storage
81+
* @param $file
82+
*
83+
* @return bool
84+
*/
85+
protected function takeBackup($storage, $file)
86+
{
87+
if(\Storage::exists($file) && $storage->exists($file) == false) {
88+
$stream = \Storage::getDriver()->readStream($file);
89+
90+
return $storage->put($file, $stream);
91+
}
92+
93+
return false;
94+
}
95+
96+
/**
97+
* Send notifications after backup
98+
*
99+
* @param $recordsBackup
100+
*/
101+
protected function sendNotifications(array $recordsBackup)
102+
{
103+
if(!config('modelmediabackup.EnableNotification')) {
104+
return;
105+
}
106+
107+
if($mailTo = config('modelmediabackup.Notification.MailTo')) {
108+
$this->sendNotificationMail($mailTo, $recordsBackup);
109+
}
110+
}
111+
112+
/**
113+
* Send notification mail
114+
*/
115+
protected function sendNotificationMail($mailTo, array $recordsBackup)
116+
{
117+
\Mail::to($mailTo)
118+
->send(new MediaBackupTaken(
119+
$recordsBackup
120+
));
121+
}
122+
}

src/Facades/MediaModelBackup.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace BinBytes\ModelMediaBackup\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class MediaModelBackup extends Facade
8+
{
9+
/**
10+
* Get the registered name of the component.
11+
*
12+
* @return string
13+
*/
14+
protected static function getFacadeAccessor()
15+
{
16+
return 'mediamodelbackup';
17+
}
18+
}

src/Interfaces/ShouldBackupFiles.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace BinBytes\ModelMediaBackup\Interfaces;
4+
5+
/**
6+
* Interface ShouldBackupFiles
7+
* @package BinBytes\ModalBackup\Interfaces
8+
*/
9+
interface ShouldBackupFiles
10+
{
11+
/**
12+
* Files those are need to backup with associate with record
13+
* @return string|array
14+
*/
15+
public function backupFiles();
16+
17+
public static function backupRecords();
18+
}

src/Mail/MediaBackupTaken.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace BinBytes\ModelMediaBackup\Mail;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Mail\Mailable;
7+
use Illuminate\Queue\SerializesModels;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
10+
class MediaBackupTaken extends Mailable implements ShouldQueue
11+
{
12+
use Queueable, SerializesModels, Queueable;
13+
14+
/**
15+
* Backup Taken of these records
16+
* @var array
17+
*/
18+
public $records;
19+
20+
/**
21+
* Create a new message instance.
22+
*
23+
* @return void
24+
*/
25+
public function __construct(array $records)
26+
{
27+
$this->records = $records;
28+
}
29+
30+
/**
31+
* Build the message.
32+
*
33+
* @return $this
34+
*/
35+
public function build()
36+
{
37+
return $this->markdown('modelmediabackup::mail.backup.processed', [
38+
'records' => $this->records
39+
]);
40+
}
41+
}

src/ModelMediaBackup.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace BinBytes\ModelMediaBackup;
4+
5+
class ModelMediaBackup
6+
{
7+
// Build wonderful things
8+
}

0 commit comments

Comments
 (0)