Skip to content

Commit

Permalink
added file download functionality and updated read me
Browse files Browse the repository at this point in the history
  • Loading branch information
darryldecode committed Oct 12, 2017
1 parent 35eed88 commit 118598b
Show file tree
Hide file tree
Showing 17 changed files with 712 additions and 444 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

WASK_FILE_DOWNLOAD_SECRET=yourRandomStringHere
7 changes: 7 additions & 0 deletions app/Contracts/FileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,11 @@ public function deleteFile($path);
* @return Result
*/
public function previewFile($data);

/**
* @param int $id
* @param string $token
* @return Result
*/
public function downloadFile($id,$token);
}
49 changes: 49 additions & 0 deletions app/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

namespace App;

use Carbon\Carbon;
use Firebase\JWT\JWT;
use Illuminate\Database\Eloquent\Model;

class File extends Model
{
const TAG = 'WASK_FILE_MANAGER';

protected $table = 'files';

protected $fillable = [
Expand All @@ -18,6 +22,51 @@ class File extends Model
'path',
];

protected $appends = ['file_token'];

/**
* generate a token on each file on the fly that will give authorization to be downloaded
*
* @return string
*/
public function getFileTokenAttribute()
{
$key = env('WASK_FILE_DOWNLOAD_SECRET');
$token = array(
"iss" => env('APP_URL'),
"aud" => env('APP_URL'),
"iat" => (Carbon::now())->timestamp,
"exp" => (Carbon::now()->addDays(1))->timestamp,
"file_id" => $this->id,
);
return JWT::encode($token, $key);
}

/**
* verify the token
*
* @param $token
* @return bool|object
*/
public function verifyFileToken($token)
{
try {
$key = env('WASK_FILE_DOWNLOAD_SECRET');
$decoded = JWT::decode($token, $key, array('HS256'));
} catch (\Exception $e)
{
\Log::error(self::TAG . ": Invalid file token.");
return false;
}

if(!$decoded->file_id==$this->id)
{
return false;
}

return $decoded;
}

/**
* the owner of the file
*
Expand Down
20 changes: 20 additions & 0 deletions app/Http/Controllers/Front/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function __construct(FileRepository $fileRepository)
}

/**
* preview file web request
*
* @param Request $request
* @param int $id
* @return mixed
Expand All @@ -46,4 +48,22 @@ public function filePreview(Request $request,$id)

return $res->getData();
}

/**
* download file web request
*
* @param Request $request
* @param int $id
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|null
*/
public function fileDownload(Request $request, $id)
{
$token = $request->get('file_token');

$res = $this->fileRepository->downloadFile($id,$token);

if($res->isSuccessful()) return $res->getData();

return view('errors.403');
}
}
28 changes: 28 additions & 0 deletions app/Repositories/MySQLFileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,32 @@ public function previewFile($data)

return new Result(false,Result::MESSAGE_NOT_FOUND,$res,200);
}

/**
* @param int $id
* @param string $token
* @return Result
*/
public function downloadFile($id, $token)
{
$File = File::find($id);

if(!$File) return new Result(false,Result::MESSAGE_NOT_FOUND,null,404);

if(!$File->verifyFileToken($token))
{
return new Result(false,"Invalid Token",null,403);
}

$fileFullPath = Storage::disk('local')->path($File->path);

if(!Storage::disk('local')->exists($File->path))
{
return new Result(false,"File not exist.",null,404);
}

$fileDownload = response()->download($fileFullPath,str_replace(' ','_',$File->name).".{$File->extension}");

return new Result(true,"Download success",$fileDownload,200);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"require": {
"php": ">=7.0.0",
"fideloper/proxy": "~3.3",
"firebase/php-jwt": "^5.0",
"intervention/image": "^2.4",
"laravel/framework": "5.5.*",
"laravel/tinker": "~1.0",
Expand Down
48 changes: 47 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
|
*/

'log' => env('APP_LOG', 'single'),
'log' => env('APP_LOG', 'daily'),

'log_level' => env('APP_LOG_LEVEL', 'debug'),

Expand Down
Loading

0 comments on commit 118598b

Please sign in to comment.