Completed
Push — dev5 ( 5c4670...707f8f )
by Ron
09:35
created

DownloadController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 13 3
A downloadAll() 0 25 3
A flashDownload() 0 6 1
A downloadCustNote() 0 12 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use PDF;
6
use Zip;
7
use App\Files;
8
use App\Customers;
9
use App\CustomerNotes;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\Log;
12
use Illuminate\Support\Facades\Auth;
13
use Illuminate\Support\Facades\Route;
14
use Illuminate\Support\Facades\Storage;
15
16
class DownloadController extends Controller
17
{
18
    //  Download one file
19
    public function index($fileID, $fileName)
20
    {
21
        $fileData = Files::where('file_id', $fileID)->where('file_name', $fileName)->first();
22
        //  Check that the file exists before allowing it to be downloaded
23
        if(!empty($fileData) && Storage::exists($fileData->file_link.$fileData->file_name))
24
        {
25
            Log::info('File Downloaded', ['file_id' => $fileID]);
26
            return Storage::download($fileData->file_link.$fileData->file_name);
27
        }
28
        
29
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
30
        Log::notice('File Not Found', ['file_id' => $fileID, 'file_name' => $fileName]);
31
        return view('err.badFile');
32
    }
33
    
34
    //  Put the download files into the flash data
35
    public function flashDownload(Request $request)
36
    {
37
        session()->flash('fileArr', $request->fileArr);
38
        
39
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
40
        return response()->json($request);
41
    }
42
    
43
    //  Download an array of files
44
    public function downloadAll()
45
    {
46
        //  Get files and base path
47
        $fileData = Files::findMany(session('fileArr'));
48
        $path = config('filesystems.disks.local.root').DIRECTORY_SEPARATOR;
49
        
50
        //  Verify that the file array is not empty
51
        if(empty($fileData))
52
        {
53
            Log::info('Files Not Found', [session('fileArr')]);
54
            return view('err.badFile');
55
        }
56
        
57
        //  Package the files in a zip file 
58
        $zip = Zip::create($path.'download.zip');
59
        foreach($fileData as $file)
60
        {
61
            $zip->add($path.$file->file_link.$file->file_name);
62
        }
63
        $zip->close();
64
        
65
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
66
        
67
        //  Download zip file and remove it from the server
68
        return response()->download($path.'download.zip')->deleteFileAfterSend(true);
69
    }
70
    
71
    //  Download Customer Note as PDF
72
    public function downloadCustNote($id)
73
    {
74
        $note = CustomerNotes::find($id);
75
        $cust = Customers::find($note->cust_id);
76
        
77
        $pdf = PDF::loadView('pdf.customerNote', [
78
            'cust_name'   => $cust->name,
79
            'note_subj'   => $note->subject,
80
            'description' => $note->description
81
        ]);
82
        
83
        return $pdf->download($cust->name.' - Note: '.$note->subject.'.pdf');
84
    }
85
}
86