Completed
Push — main ( 746cb6...22aa6c )
by PRATIK
41s queued 32s
created

EditorUploadController::upload()   B

Complexity

Conditions 6
Paths 21

Size

Total Lines 46
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 22
c 1
b 1
f 0
nc 21
nop 1
dl 0
loc 46
rs 8.9457
1
<?php
2
3
namespace Pratiksh\Adminetic\Http\Controllers\Admin;
4
5
use App\Http\Controllers\Controller;
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Http\Request;
7
8
class EditorUploadController extends Controller
9
{
10
    // public function upload(Request $request)
11
    // {
12
    //     if ($request->hasFile('upload')) {
13
    //         $originName = $request->file('upload')->getClientOriginalName();
14
    //         $fileName = pathinfo($originName, PATHINFO_FILENAME);
15
    //         $extension = $request->file('upload')->getClientOriginalExtension();
16
    //         $fileName = $fileName.'_'.time().'.'.$extension;
17
18
    //         $request->file('upload')->move(public_path('storage/editorimages'), $fileName);
19
20
    //         $CKEditorFuncNum = $request->input('CKEditorFuncNum');
21
    //         $url = asset('storage/editorimages/'.$fileName);
22
    //         $msg = 'Image uploaded successfully';
23
    //         $response = "<script>window.parent.CKEDITOR.tools.callFunction($CKEditorFuncNum, '$url', '$msg')</script>";
24
25
    //         @header('Content-type: text/html; charset=utf-8');
26
27
    //         return $response;
28
29
    //         return response()->json(['uploaded' => 1, 'fileName' => $fileName, 'url' => $url]);
30
    //     }
31
    // }
32
    public function upload(Request $request)
33
    {
34
        // Ensure a file was uploaded
35
        if (!$request->hasFile('upload')) {
36
            return response()->json(['uploaded' => 0, 'error' => ['message' => 'No file was uploaded.']], 400);
37
        }
38
39
        $file = $request->file('upload');
40
41
        // Validate file type to ensure it's an image
42
        $allowedExtensions = ['jpg', 'jpeg', 'png', 'gif'];
43
        $extension = $file->getClientOriginalExtension();
44
        if (!in_array(strtolower($extension), $allowedExtensions)) {
45
            return response()->json(['uploaded' => 0, 'error' => ['message' => 'Invalid file type.']], 400);
46
        }
47
48
        // Generate a unique file name
49
        $originName = $file->getClientOriginalName();
50
        $fileName = pathinfo($originName, PATHINFO_FILENAME) . '_' . time() . '.' . $extension;
0 ignored issues
show
Bug introduced by
Are you sure pathinfo($originName, Pr...dmin\PATHINFO_FILENAME) of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $fileName = /** @scrutinizer ignore-type */ pathinfo($originName, PATHINFO_FILENAME) . '_' . time() . '.' . $extension;
Loading history...
51
52
        try {
53
            // Ensure the upload directory exists and has correct permissions
54
            $uploadDir = public_path('storage/editorimages');
55
            if (!is_dir($uploadDir)) {
56
                mkdir($uploadDir, 0775, true); // Create directory if it doesn't exist
57
            }
58
59
            // Move the file to the upload directory
60
            $file->move($uploadDir, $fileName);
61
62
            // Build the URL for the uploaded file
63
            $fileUrl = asset('storage/editorimages/' . $fileName);
64
65
            // Check if the request contains the CKEditor function number
66
            if ($request->has('CKEditorFuncNum')) {
67
                $CKEditorFuncNum = $request->input('CKEditorFuncNum');
68
                $msg = 'Image uploaded successfully';
69
                // Return the JavaScript response for 'form' upload method
70
                return response("<script>window.parent.CKEDITOR.tools.callFunction($CKEditorFuncNum, '$fileUrl', '$msg');</script>")->header('Content-type', 'text/html; charset=utf-8');
71
            }
72
73
            // Return JSON response for 'xhr' upload method
74
            return response()->json(['uploaded' => 1, 'fileName' => $fileName, 'url' => $fileUrl]);
75
76
        } catch (\Exception $e) {
77
            return response()->json(['uploaded' => 0, 'error' => ['message' => 'Upload failed: ' . $e->getMessage()]], 500);
78
        }
79
    }
80
}
81