Skip to content

Commit ff07256

Browse files
committed
Update custom Filename
1 parent 501bbb3 commit ff07256

File tree

8 files changed

+123
-117
lines changed

8 files changed

+123
-117
lines changed

README.md

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
# 🛡️Laravel Encrypt Upload File
21

3-
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/irfaardy/encrypt-file-laravel/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/irfaardy/encrypt-file-laravel/?branch=master) [![Build Status](https://scrutinizer-ci.com/g/irfaardy/encrypt-file-laravel/badges/build.png?b=master)](https://scrutinizer-ci.com/g/irfaardy/encrypt-file-laravel/build-status/master) [![Latest Stable Version](https://poser.pugx.org/irfa/encrypt-file-laravel/v/stable)](https://packagist.org/packages/irfa/encrypt-file-laravel) [![GitHub license](https://img.shields.io/github/license/irfaardy/encrypt-file-laravel?style=flat-square)](https://github.com/irfaardy/encrypt-file-laravel/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)
2+
3+
# 🚀Simple Laravel Encrypt Upload File
4+
[![GitHub license](https://img.shields.io/github/license/irfaardy/encrypt-file-laravel?style=flat-square)](https://github.com/irfaardy/encrypt-file-laravel/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)
45

56
<p>The Simple Laravel Encrypt Upload File uses the default encryption of Laravel which is implemented in upload file.<p>
67
<h3>🛠️ Installation with Composer </h3>
78

8-
9-
```php
10-
composer require irfa/encrypt-file-laravel
11-
```
9+
composer require irfa/encrypt-file-laravel
1210

1311
>You can get Composer [ here]( https://getcomposer.org/download/)
1412
@@ -19,24 +17,20 @@ composer require irfa/encrypt-file-laravel
1917

2018
<h3>Add to config/app.php</h3>
2119

22-
```php
23-
'providers' => [
24-
....
25-
Irfa\FileSafe\FileSafeServiceProvider::class,
26-
];
27-
```
20+
'providers' => [
21+
....
22+
Irfa\FileSafe\FileSafeServiceProvider::class,
23+
];
2824

2925

3026

3127
<h3>Add to config/app.php</h3>
3228

33-
```php
34-
'aliases' => [
35-
....
36-
'FileSafe' => Irfa\FileSafe\Facades\FileSafe::class,
37-
38-
],
39-
```
29+
'aliases' => [
30+
....
31+
'FileSafe' => Irfa\FileSafe\Facades\FileSafe::class,
32+
33+
],
4034

4135
<h2>Publish Vendor</h2>
4236

@@ -50,51 +44,59 @@ composer require irfa/encrypt-file-laravel
5044
<h2>Example store file</h2>
5145

5246

53-
```php
54-
<?php
55-
56-
namespace App\Http\Controllers;
57-
58-
use Filesafe;
59-
use Illuminate\Http\Request;
60-
use App\Http\Controllers\Controller;
61-
62-
class FileController extends Controller
63-
{
64-
65-
public function upload_file(Request $request)
47+
<?php
48+
49+
namespace App\Http\Controllers;
50+
51+
use Filesafe;
52+
use Illuminate\Http\Request;
53+
use App\Http\Controllers\Controller;
54+
55+
class FileController extends Controller
6656
{
67-
$file = $request->file('file');
68-
FileSafe::store($file);
69-
//This is to encrypt the file before it is uploaded to the server.
70-
57+
58+
public function upload_file(Request $request)
59+
{
60+
$file = $request->file('file');
61+
FileSafe::store($file);
62+
//This is to encrypt the file before it is uploaded to the server.
63+
64+
}
7165
}
72-
}
73-
```
7466

7567
<h2>Example download file</h2>
7668

7769

78-
```php
79-
...
80-
$file = 'path/to/encrypted_file.txt';
81-
return FileSafe::download($file);
82-
//decrypt file to be downloaded.
83-
...
84-
```
70+
<?php
71+
72+
namespace App\Http\Controllers;
73+
74+
use FileSafe;
75+
use Illuminate\Http\Request;
76+
use App\Http\Controllers\Controller;
77+
78+
class FileController extends Controller
79+
{
80+
81+
public function upload_file(Request $request)
82+
{
83+
$file = 'encrypted_file.txt';
84+
return FileSafe::download($file);
85+
//decrypt files to be downloaded.
86+
}
87+
}
8588

8689
<h3>View Raw File</h3>
8790

88-
```php
89-
...
90-
$file = 'encrypt_file.txt';
91-
return FileSafe::file($file)->raw();
92-
...
93-
```
91+
...
92+
$file = 'encrypt_file.txt';
93+
return FileSafe::file($file)->raw();
94+
...
9495

9596
<h2>Contribution</h2>
96-
9797
ENG:<br>
9898
If you want to contribute this project and make it better, your help is very welcome.<br>
9999
IDN:<br>
100100
Jika Anda ingin berkontribusi dalam proyek ini dan menjadikannya lebih baik, bantuan Anda sangat kami harapkan.
101+
102+
***

config/irfa/filesafe.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
return [
33

4-
'random_filename' => true, //Generate random alnum for upload filename,
4+
'random_filename' => true,//Generate random alnum for upload filename,
55

6-
'path' => 'uploaded/', //Uploaded file is located in storage/app.
6+
'path' => 'uploaded/',//Uploaded file is located in storage/app.
77

88

99

src/Facades/FileSafe.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
class FileSafe extends Facade
1010
{
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-
}
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+
}
2020
}

src/FileSafeServiceProvider.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@
66

77
class FileSafeServiceProvider extends ServiceProvider
88
{
9-
/**
10-
* Register services.
11-
*
12-
* @return void
13-
*/
14-
public function register()
15-
{
16-
//
17-
}
9+
/**
10+
* Register services.
11+
*
12+
* @return void
13+
*/
14+
public function register()
15+
{
16+
//
17+
}
1818

19-
/**
20-
* Bootstrap services.
21-
*
22-
* @return void
23-
*/
24-
public function boot()
25-
{
19+
/**
20+
* Bootstrap services.
21+
*
22+
* @return void
23+
*/
24+
public function boot()
25+
{
2626

27-
$this->publishes([
28-
__DIR__.'/../config/irfa/' => config_path('irfa') ], 'file-safe');
27+
$this->publishes([
28+
__DIR__.'/../config/irfa/' => config_path('irfa')],'file-safe');
2929

3030

31-
}
31+
}
3232
}

src/Func/File.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php
22
namespace Irfa\FileSafe\Func;
33

44
use Irfa\FileSafe\Security\CryptFile;
@@ -7,15 +7,15 @@
77
class File extends CryptFile
88
{
99

10-
protected static function upload($file) {
11-
return CryptFile::encrypt($file);
10+
protected static function upload($file,$filename){
11+
return CryptFile::encrypt($file,$filename);
1212
}
13-
protected static function download_file($file) {
13+
protected static function download_file($file){
1414
return CryptFile::decrypt($file);
1515
}
16-
protected static function view_raw($file) {
17-
return CryptFile::decrypt($file, true);
16+
protected static function view_raw($file){
17+
return CryptFile::decrypt($file,true);
1818
}
1919

2020

21-
}
21+
}

src/Func/FileSafe.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php
22
namespace Irfa\FileSafe\Func;
33

44
use Irfa\FileSafe\Func\File;
@@ -7,36 +7,36 @@ class FileSafe extends File
77
{
88
private static $file;
99

10-
public static function file($file) {
10+
public static function file($file){
1111
self::$file = $file;
1212

1313
return new static();
1414
}
15-
public static function store($file = null) {
16-
if ($file == null) {
15+
public static function store($file=null,$filename=null){
16+
if($file == null){
1717
$fl = self::$file;
18-
} else {
18+
} else{
1919
$fl = $file;
2020
}
21-
self::upload($fl);
21+
self::upload($fl,$filename);
2222
}
2323

24-
public static function download($file = null) {
25-
if ($file == null) {
24+
public static function download($file=null){
25+
if($file == null){
2626
$fl = self::$file;
27-
} else {
27+
} else{
2828
$fl = $file;
2929
}
3030
return self::download_file($fl);
3131
}
3232

33-
public static function raw($file = null) {
34-
if ($file == null) {
33+
public static function raw($file=null){
34+
if($file == null){
3535
$fl = self::$file;
36-
} else {
36+
} else{
3737
$fl = $file;
3838
}
3939
return self::view_raw($fl);
4040
}
4141

42-
}
42+
}

src/Security/CryptFile.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php
22
namespace Irfa\FileSafe\Security;
33

44
use Illuminate\Support\Facades\Hash;
@@ -9,39 +9,43 @@
99
class CryptFile extends File
1010
{
1111
private static $file;
12-
protected static function encrypt($file) {
12+
protected static function encrypt($file,$filename){
1313
$fileContent = $file->get();
1414
self::$file = $file;
1515
$encryptedContent = Crypt::encrypt($fileContent);
16-
self::store_file(self::GenerateFileName(), $encryptedContent);
16+
self::store_file(self::GenerateFileName($filename),$encryptedContent);
1717

1818
self::$file = null;
1919

20-
20+
2121
}
2222

23-
protected static function decrypt($file, $raw = false) {
23+
protected static function decrypt($file,$raw=false){
2424
$fl = self::get_file($file);
2525
$decryptedContent = Crypt::decrypt($fl);
26-
if ($raw) {
26+
if($raw){
2727
$raw = $decryptedContent;
2828
return $raw;
29-
} else {
29+
} else{
3030

3131
return response()->streamDownload(function() use ($decryptedContent) {
32-
echo $decryptedContent;
32+
echo $decryptedContent;
3333
}, $file);
3434
}
3535
}
36-
private static function GenerateFileName() {
37-
if (config("irfa.filesafe.random_filename")) {
36+
private static function GenerateFileName($filename=null){
37+
if(config("irfa.filesafe.random_filename")){
3838
$rand = time()."_".Str::random(20).".".self::$file->getClientOriginalExtension();
39-
} else {
39+
} else{
4040
$rand = self::$file->getClientOriginalName();
4141
}
42+
if(!empty($filename))
43+
{
44+
$rand = $filename;
45+
}
4246
return $rand;
4347
}
4448

45-
4649

47-
}
50+
51+
}

src/Security/File.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77

88
class File {
9-
protected static function store_file($randomname, $encryptedContent) {
9+
protected static function store_file($randomname,$encryptedContent){
1010
$path = config("irfa.filesafe.path");
1111
Storage::disk('local')->put($path.$randomname, $encryptedContent);
1212
}
13-
protected static function get_file($filename) {
13+
protected static function get_file($filename){
1414
$path = config("irfa.filesafe.path");
1515
return Storage::disk('local')->get($path.$filename);
1616
}

0 commit comments

Comments
 (0)