Passed
Push — master ( 1d6d9a...2a90e7 )
by Ron
02:47 queued 12s
created

Files   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 33
c 0
b 0
f 0
dl 0
loc 69
ccs 0
cts 28
cp 0
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A cleanFileName() 0 36 5
A deleteFile() 0 23 2
1
<?php
2
3
namespace App;
4
5
use Illuminate\Support\Facades\Log;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Facades\Storage;
9
10
class Files extends Model
11
{
12
    protected $primaryKey = 'file_id';
13
    protected $fillable   = ['file_name', 'file_link', 'mime_type'];
14
    protected $hidden     = ['created_at', 'updated_at', 'file_link'];
15
16
    //  Remove any illegal characters from filename and make sure it is unique
17
    public static function cleanFileName($path, $fileName)
18
    {
19
        Log::debug('Preparing to clean a file. Current Name - '.$fileName.'. Current Path - '.$path);
20
        //  Remove all spaces
21
        $fileName = str_replace(' ', '_', $fileName);
22
        Log::debug('Cleaned Filename. Current Name - ' . $fileName . '. Current Path - ' . $path);
23
24
        //  Determine if the filename already exists
25
        if(Storage::exists($path.DIRECTORY_SEPARATOR.$fileName))
26
        {
27
            $fileParts = pathinfo($fileName);
28
            $extension = isset($fileParts['extension']) ? ('.'.$fileParts['extension']) : '';
29
30
            //  Look to see if a number is already appended to a file.  (example - file(1).pdf)
31
            if(preg_match('/(.*?)(\d+)$/', $fileParts['filename'], $match))
32
            {
33
                // Has a number, increment it
34
                $base = $match[1];
35
                $number = intVal($match[2]);
36
            }
37
            else
38
            {
39
                // No number, add one
40
                $base = $fileParts['filename'];
41
                $number = 0;
42
            }
43
44
            //  Increase the number until one that is not in use is found
45
            do
46
            {
47
                Log::debug('Filename ' . $fileName . ' already exists.  Appending name.');
48
                $fileName = $base.'('.++$number.')'.$extension;
49
            } while(Storage::exists($path.DIRECTORY_SEPARATOR.$fileName));
50
        }
51
52
        return $fileName;
53
    }
54
55
    //  Delete a file from both the database and file system
56
    public static function deleteFile($fileID)
57
    {
58
        Log::debug('Attempting to delete file ID '.$fileID);
59
        $fileLink = '';
60
        try
61
        {
62
            //  Try to delete file from database - will throw error if foreign key is in use
63
            $fileData = Files::find($fileID);
64
            $fileLink = $fileData->file_link.$fileData->file_name;
65
            $fileData->delete();
66
        }
67
        catch(\Illuminate\Database\QueryException $e)
68
        {
69
            //  Unable to remove file from the database
70
            Log::warning('Attempt to delete file failed.  Reason - '.$e.'. Additional Data - ', ['file_id' => $fileID, 'file_name' => $fileLink, 'user_id' => Auth::user()->user_id]);
71
            return false;
72
        }
73
74
        //  Delete the file from the storage system
75
        Storage::delete($fileLink);
76
77
        Log::notice('File deleted by '.Auth::user()->full_name.'. Additional Information - ', ['file_id' => $fileID, 'file_name' => $fileLink, 'user_id' => Auth::user()->user_id]);
78
        return true;
79
    }
80
}
81