Passed
Push — dev5a ( e86993...328ab1 )
by Ron
10:15 queued 39s
created

SetFiles::deleteFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.108

Importance

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