FileStorageController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 49
rs 10
c 2
b 0
f 0
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B get() 0 47 8
1
<?php
2
3
namespace Epesi\FileStorage;
4
5
use Illuminate\Routing\Controller;
6
use Illuminate\Http\Request;
7
8
class FileStorageController extends Controller
9
{
10
    public function get(Request $request)
11
    {
12
    	try {
13
    		$file = Models\File::retrieve($request->get('id'));
14
    	} catch (\Exception $e) {
15
    		abort(404);
16
    	}
17
18
    	$useThumbnail = false;
19
    	$disposition = 'attachment';
20
    	switch ($request->get('action')) {	
21
    		case 'preview':
22
    			$useThumbnail = true;
23
    			// intended fallthrough
24
    		case 'inline':
25
    			$disposition = 'inline';
26
    			break;
27
    			
28
    		default:
29
    			break;
30
    	}
31
32
    	if ($useThumbnail && $request->get('thumbnail', 1) && ($thumbnail = $file['thumbnail'])) {
33
    		$mime = $thumbnail['mime'];
34
    		$filename = $thumbnail['name'];
35
    		$contents = $thumbnail['contents'];
36
    	}
37
    	else {
38
    		$content = $file->ref('content');
39
    		
40
    		$mime = $content['type'];
41
    		$filename = $file['name'];
42
    		$contents = $content['data'];
43
    	}
44
    	
45
    	$headers = [
46
    			'Content-Type' => $mime,
47
    			'Content-Length' => strlen($contents),
48
    			'Content-Disposition' => "$disposition; filename=\"$filename\"",
49
    	];
50
51
    	if ($request->get('nocache')) {
52
    		$headers['Pragma'] = 'no-cache';
53
    		$headers['Expires'] = '0';
54
    	}
55
    	
56
    	return response($contents, 200, $headers);
57
    }
58
}
59