Passed
Push — dev5a ( 96dd50...915c64 )
by Ron
07:38
created

SetFiles::moveFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 2
rs 10
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 10
    public function __construct()
21
    {
22 10
        $this->path = config('filesystems.paths.default');
23 10
        $this->disk = 'local';
24 10
    }
25
26
    //  Process a single chunk of a larger file
27 4
    protected function getChunk($request)
28
    {
29 4
        $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
30 4
        $save = $receiver->receive();
31
32 4
        if($save->isFinished())
33
        {
34 4
            return $this->saveFile($save->getFile());
35
        }
36
37
        return false;
38
    }
39
40
    //  Save the file to the storage system
41 4
    protected function saveFile(UploadedFile $file)
42
    {
43 4
        $fileName = $this->cleanFilename($file->getClientOriginalName());
44 4
        $fileName = $this->checkForDuplicate($fileName);
45
46 4
        $file->storeAs($this->path, $fileName);
47 4
        $user = isset(Auth::user()->full_name) ? Auth::user()->full_name : \Request::ip();
48 4
        Log::info('New file '.$fileName.' stored in location - '.$this->path.' by '.$user);
49
50 4
        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
    //  Move a file from one folder to another
79
    protected function moveFile($fileID, $newPath)
80
    {
81
        $file = Files::find($fileID);
82
        Storage::move($file->file_link.$file->file_name, $newPath.$file->file_name);
83
        $file->update([
84
            'file_link' => $newPath
85
        ]);
86
87
        return true;
88
    }
89
90
    //  Add the new file to the database
91 4
    protected function addDatabaseRow($filename, $path)
92
    {
93 4
        $file = Files::create([
94 4
            'file_name' => $filename,
95 4
            'file_link' => $path.DIRECTORY_SEPARATOR,
96
        ]);
97
98 4
        return $file->file_id;
99
    }
100
101
    //  Sanitize the filename to remove any illegal characters and spaces
102 4
    protected function cleanFilename($name)
103
    {
104 4
        return preg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $name);
105
    }
106
107
    //  Check to see if the file already exists, if so append filename as needed
108 4
    protected function checkForDuplicate($name)
109
    {
110 4
        if(Storage::disk($this->disk)->exists($this->path.DIRECTORY_SEPARATOR.$name))
111
        {
112
            $parts = pathinfo($name);
113
            $ext   = isset($parts['extension']) ? ('.'.$parts['extension']) : '';
114
            $base = $parts['filename'];
115
             $number = 0;
116
117
            do
118
            {
119
                $name = $base.'('.++$number.')'.$ext;
120
            } while(Storage::disk($this->disk)->exists($this->path.DIRECTORY_SEPARATOR.$name));
121
        }
122
123 4
        return $name;
124
    }
125
}
126