Skip to content

Commit 94d0d9a

Browse files
committed
✏️first commit
0 parents  commit 94d0d9a

File tree

10 files changed

+285
-0
lines changed

10 files changed

+285
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Irfaardy - Irfa Ardiansyah
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.

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
# 🚀Simple Laravel Encrypt Upload File
3+
[![GitHub license](https://img.shields.io/github/license/irfaardy/raja-ongkir?style=flat-square)](https://github.com/irfaardy/raja-ongkir/blob/master/LICENSE) [![Support me](https://img.shields.io/badge/Support-Buy%20me%20a%20coffee-yellow.svg?style=flat-square)](https://www.buymeacoffee.com/OBaAofN)
4+
<h3>🛠️ Installation with Composer </h3>
5+
6+
composer require irfa/raja-ongkir
7+
8+
>You can get Composer [ here]( https://getcomposer.org/download/)
9+
10+
***
11+
12+
13+
<h2>🛠️ Laravel Setup </h2>
14+
15+
<h3>Add to config/app.php</h3>
16+
17+
'providers' => [
18+
....
19+
Irfa\FileSafe\FileSafeServiceProvider::class,
20+
];
21+
22+
23+
24+
<h3>Add to config/app.php</h3>
25+
26+
'aliases' => [
27+
....
28+
'FileSafe' => Irfa\FileSafe\Facades\FileSafe::class,
29+
30+
],
31+
32+
<h2>Publish Vendor</h2>
33+
34+
35+
php artisan vendor:publish --tag=file-safe
36+
37+
<h2>Config File</h2>
38+
39+
config/irfa/filesafe.php
40+
41+
<h2>Example store file</h2>
42+
43+
44+
<?php
45+
46+
namespace App\Http\Controllers;
47+
48+
use Illuminate\Http\Request;
49+
use App\Http\Controllers\Controller;
50+
51+
class FileController extends Controller
52+
{
53+
54+
public function upload_file(Request $request)
55+
{
56+
$file = $request->file('file');
57+
FileSafe::store($file);
58+
//
59+
}
60+
}
61+
62+
<h2>Example download file</h2>
63+
64+
65+
<?php
66+
67+
namespace App\Http\Controllers;
68+
69+
use Illuminate\Http\Request;
70+
use App\Http\Controllers\Controller;
71+
72+
class FileController extends Controller
73+
{
74+
75+
public function upload_file(Request $request)
76+
{
77+
$file = 'images.jpg';
78+
return FileSafe::download($file);
79+
//
80+
}
81+
}
82+

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "irfa/encrypt-file-laravel",
3+
"description": "\"Encrypt & decrypt uploaded file for Laravel\"",
4+
"type": "package",
5+
"license": "MIT",
6+
"version": "v1.0",
7+
"keywords": ["laravel", "security", "encrypt", "decrypt", "secure","upload file"],
8+
"authors": [
9+
{
10+
"name": "Irfa A",
11+
"email": "[email protected]"
12+
}
13+
],
14+
"require": {
15+
"php": "^7.2"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"Irfa\\FileSafe\\": "src/"
20+
}
21+
}
22+
}

config/irfa/filesafe.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
return [
3+
4+
'random_filename' => true,//Generate random alnum for upload filename,
5+
6+
'path' => 'uploaded/',//Location uploaded file storage/app,
7+
8+
9+
10+
];

src/Facades/FileSafe.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/*
3+
Author: Irfa Ardiansyah <[email protected]>
4+
*/
5+
namespace Irfa\FileSafe\Facades;
6+
7+
use Illuminate\Support\Facades\Facade;
8+
9+
class FileSafe extends Facade
10+
{
11+
/**
12+
* Get the registered name of the component.
13+
*
14+
* @return string
15+
*/
16+
protected static function getFacadeAccessor()
17+
{
18+
return \Irfa\FileSafe\Func\FileSafe::class;
19+
}
20+
}

src/FileSafeServiceProvider.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Irfa\FileSafe;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class FileSafeServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Register services.
11+
*
12+
* @return void
13+
*/
14+
public function register()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Bootstrap services.
21+
*
22+
* @return void
23+
*/
24+
public function boot()
25+
{
26+
27+
$this->publishes([
28+
__DIR__.'/../config/irfa/' => config_path('irfa')],'file-safe');
29+
30+
31+
}
32+
}

src/Func/File.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
namespace Irfa\FileSafe\Func;
3+
4+
use Irfa\FileSafe\Security\CryptFile;
5+
use Illuminate\Support\Facades\Storage;
6+
7+
class File extends CryptFile
8+
{
9+
10+
protected static function upload($file){
11+
return CryptFile::encrypt($file);
12+
}
13+
protected static function download_file($file){
14+
return CryptFile::decrypt($file);
15+
}
16+
17+
18+
}

src/Func/FileSafe.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace Irfa\FileSafe\Func;
3+
4+
5+
use Irfa\FileSafe\Func\File;
6+
7+
class FileSafe extends File
8+
{
9+
10+
public static function store($file){
11+
self::upload($file);
12+
}
13+
14+
public static function download($file){
15+
return self::download_file($file);
16+
}
17+
18+
19+
}

src/Security/CryptFile.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
namespace Irfa\FileSafe\Security;
3+
4+
use Illuminate\Support\Facades\Hash;
5+
use Illuminate\Support\Facades\Crypt;
6+
use Illuminate\Support\Str;
7+
use Irfa\FileSafe\Security\File;
8+
9+
class CryptFile extends File
10+
{
11+
private static $file;
12+
protected static function encrypt($file){
13+
$fileContent = $file->get();
14+
self::$file = $file;
15+
$encryptedContent = Crypt::encrypt($fileContent);
16+
self::store_file(self::GenerateFileName(),$encryptedContent);
17+
18+
self::$file = null;
19+
20+
21+
}
22+
23+
protected static function decrypt($file){
24+
$fl = self::get_file($file);
25+
$decryptedContent = Crypt::decrypt($fl);
26+
27+
return response()->streamDownload(function() use ($decryptedContent) {
28+
echo $decryptedContent;
29+
}, $file);
30+
}
31+
private static function GenerateFileName(){
32+
if(config("irfa.filesafe.random_filename")){
33+
$rand = time()."_".Str::random(20).".".self::$file->getClientOriginalExtension();
34+
} else{
35+
$rand = self::$file->getClientOriginalName();
36+
}
37+
return $rand;
38+
}
39+
40+
41+
42+
}

src/Security/File.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace Irfa\FileSafe\Security;
3+
4+
use Irfa\FileSafe\Security\CryptFile;
5+
use Illuminate\Support\Facades\Storage;
6+
7+
8+
class File {
9+
protected static function store_file($randomname,$encryptedContent){
10+
$path = config("irfa.filesafe.path");
11+
Storage::disk('local')->put($path.$randomname, $encryptedContent);
12+
}
13+
protected static function get_file($filename){
14+
$path = config("irfa.filesafe.path");
15+
return Storage::disk('local')->get($path.$filename);
16+
}
17+
18+
19+
}

0 commit comments

Comments
 (0)