Passed
Push — dev5a ( 328ab1...cf3a23 )
by Ron
07:38
created

SetFiles::cleanFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Domains\Files;
4
5
use App\Files;
6
7
use Illuminate\Http\UploadedFile;
8
use Illuminate\Support\Facades\Log;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\Storage;
11
12
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;
13
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory;
14
15
class SetFiles
16
{
17
    protected $path, $disk;
18
19
    //  Constructor will set the default path for files to be stored
20
    public function __construct()
21
    {
22
        $this->path = config('filesystems.paths.default');
23
        $this->disk = 'local';
24
    }
25
26
    //  Process a single chunk of a larger file
27 2
    protected function getChunk($request)
28
    {
29 2
        $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
30 2
        $save = $receiver->receive();
31
32 2
        if($save->isFinished())
33
        {
34 2
            return $this->saveFile($save->getFile());
35
        }
36
37
        return false;
38
    }
39
40
    //  Save the file to the storage system
41 2
    protected function saveFile(UploadedFile $file)
42
    {
43 2
        $fileName = $this->cleanFilename($file->getClientOriginalName());
44 2
        $fileName = $this->checkForDuplicate($fileName);
45
46 2
        $file->storeAs($this->path, $fileName);
47 2
        $user = isset(Auth::user()->full_name) ? Auth::user()->full_name : \Request::ip();
48 2
        Log::info('New file '.$fileName.' stored in location - '.$this->path.' by '.$user);
49
50 2
        return $fileName;
51
    }
52
53
    //  Delete a file from the database and file system - note will fail if the file is linked in the database
54 4
    protected function deleteFile($fileID)
55
    {
56 4
        $fileData = Files::find($fileID);
57 4
        $fileLink = $fileData->file_link.$fileData->file_name;
58
59
        try
60
        {
61
            //  Try to delete file from database - will throw error if foreign key is in use
62 4
            $fileData->delete();
63
        }
64
        catch(\Illuminate\Database\QueryException $e)
65
        {
66
            //  Unable to remove file from the database
67
            Log::warning('Attempt to delete file failed.  Reason - '.$e.'. Additional File Data - ', ['file_id' => $fileID, 'file_name' => $fileLink]);
68
            return false;
69
        }
70
71
        //  Delete the file from the storage system
72 4
        Storage::delete($fileLink);
73
74 4
        Log::notice('File deleted. File Information - ', ['file_id' => $fileID, 'file_name' => $fileLink]);
75 4
        return true;
76
    }
77
78
    //  Add the new file to the database
79 2
    protected function addDatabaseRow($filename, $path)
80
    {
81 2
        $file = Files::create([
82 2
            'file_name' => $filename,
83 2
            'file_link' => $path.DIRECTORY_SEPARATOR,
84
        ]);
85
86 2
        return $file->file_id;
87
    }
88
89
    //  Sanitize the filename to remove any illegal characters and spaces
90 2
    protected function cleanFilename($name)
91
    {
92 2
        return preg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $name);
93
    }
94
95
    //  Check to see if the file already exists, if so append filename as needed
96 2
    protected function checkForDuplicate($name)
97
    {
98 2
        if(Storage::disk($this->disk)->exists($this->path.DIRECTORY_SEPARATOR.$name))
99
        {
100
            $parts = pathinfo($name);
101
            $ext   = isset($parts['extension']) ? ('.'.$parts['extension']) : '';
102
            $base = $parts['filename'];
103
             $number = 0;
104
105
            do
106
            {
107
                $name = $base.'('.++$number.')'.$ext;
108
            } while(Storage::disk($this->disk)->exists($this->path.DIRECTORY_SEPARATOR.$name));
109
        }
110
111 2
        return $name;
112
    }
113
}
114