Skip to content

Commit

Permalink
Changes to allow encryption and upload to S3 via queued jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
soarecostin committed Nov 28, 2019
1 parent 5ee0c02 commit 4f4cdf0
Show file tree
Hide file tree
Showing 7 changed files with 713 additions and 7 deletions.
15 changes: 10 additions & 5 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Http\Controllers;

use App\Jobs\EncryptFile;
use App\Jobs\MoveFileToS3;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
Expand All @@ -26,9 +28,10 @@ public function __construct()
*/
public function index()
{
$files = Storage::files('files/' . auth()->user()->id);
$localFiles = Storage::files('files/' . auth()->user()->id);
$s3Files = Storage::disk('s3')->files('files/' . auth()->user()->id);

return view('home', compact('files'));
return view('home', compact('localFiles', 's3Files'));
}

/**
Expand All @@ -44,7 +47,9 @@ public function store(Request $request)

// check if we have a valid file uploaded
if ($filename) {
FileVault::encrypt($filename);
EncryptFile::withChain([
new MoveFileToS3($filename),
])->dispatch($filename);
}
}

Expand All @@ -60,12 +65,12 @@ public function store(Request $request)
public function downloadFile($filename)
{
// Basic validation to check if the file exists and is in the user directory
if (!Storage::has('files/' . auth()->user()->id . '/' . $filename)) {
if (!Storage::disk('s3')->has('files/' . auth()->user()->id . '/' . $filename)) {
abort(404);
}

return response()->streamDownload(function () use ($filename) {
FileVault::streamDecrypt('files/' . auth()->user()->id . '/' . $filename);
FileVault::disk('s3')->streamDecrypt('files/' . auth()->user()->id . '/' . $filename);
}, Str::replaceLast('.enc', '', $filename));
}

Expand Down
37 changes: 37 additions & 0 deletions app/Jobs/EncryptFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use SoareCostin\FileVault\Facades\FileVault;

class EncryptFile implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $filename;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct($filename)
{
$this->filename = $filename;
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
FileVault::encrypt($this->filename);
}
}
57 changes: 57 additions & 0 deletions app/Jobs/MoveFileToS3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Jobs;

use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\File;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;

class MoveFileToS3 implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $filename;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct($filename)
{
$this->filename = $filename . '.enc';
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// Upload file to S3
$result = Storage::disk('s3')->putFileAs(
'/',
new File(storage_path('app/' . $this->filename)),
$this->filename
);

// Forces collection of any existing garbage cycles
// If we don't add this, in some cases the file remains locked
gc_collect_cycles();

if ($result == false) {
throw new Exception("Couldn't upload file to S3");
}

// delete file from local filesystem
if (!Storage::disk('local')->delete($this->filename)) {
throw new Exception('File could not be deleted from the local filesystem ');
}
}
}
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"fideloper/proxy": "^4.0",
"laravel/framework": "^6.2",
"laravel/tinker": "^1.0",
"league/flysystem-aws-s3-v3": "^1.0",
"league/flysystem-cached-adapter": "^1.0",
"soarecostin/file-vault": "^1.0"
},
"require-dev": {
Expand Down
Loading

0 comments on commit 4f4cdf0

Please sign in to comment.