1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers\Dashboard;
|
---|
4 |
|
---|
5 | use App\Helpers\Alert;
|
---|
6 | use App\Http\Controllers\Controller;
|
---|
7 | use App\Http\Requests\Dashboard\FileNameRequest;
|
---|
8 | use App\Http\Requests\Dashboard\FileRequest;
|
---|
9 | use App\Models\FileType;
|
---|
10 | use App\Models\Folder;
|
---|
11 | use App\Models\File;
|
---|
12 | use App\Models\User;
|
---|
13 | use App\Notifications\NewFileCreated;
|
---|
14 | use Carbon\Carbon;
|
---|
15 | use Illuminate\Support\Facades\Notification;
|
---|
16 | use Illuminate\Support\Facades\Storage;
|
---|
17 |
|
---|
18 | class FilesController extends Controller
|
---|
19 | {
|
---|
20 | public function index ()
|
---|
21 | {
|
---|
22 | return view("dashboard.files.index")->with([
|
---|
23 | "files" => File::all(),
|
---|
24 | "folders" => Folder::all(),
|
---|
25 | "excelExt" => array("xls", "xlsx", "xls", "csv"),
|
---|
26 | "textExt" => array("txt", "doc", "docx"),
|
---|
27 | "imageExt" => array("png", "jpg", "jpeg"),
|
---|
28 | "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes))
|
---|
29 | ]);
|
---|
30 | }
|
---|
31 |
|
---|
32 | public function deleteFile($id)
|
---|
33 | {
|
---|
34 | $file = File::find($id);
|
---|
35 | $file->delete();
|
---|
36 | Storage::disk('uploads')->delete($file->location);
|
---|
37 | $file->folder()->decrement('no_of_files');
|
---|
38 |
|
---|
39 | Alert::flash($file->name . " deleted successfully");
|
---|
40 |
|
---|
41 | return redirect()->back();
|
---|
42 | }
|
---|
43 |
|
---|
44 | public function store(FileRequest $request)
|
---|
45 | {
|
---|
46 | $file = new File();
|
---|
47 |
|
---|
48 | $folder = Folder::find($request->folder);
|
---|
49 |
|
---|
50 | $location = $folder->location;
|
---|
51 |
|
---|
52 | $users = User::all();
|
---|
53 |
|
---|
54 | if ($request->has('file_item')) {
|
---|
55 | foreach ($request->file_item as $file) {
|
---|
56 | $fileName = $folder->name . '-' . uniqid() . '.' . $file->getClientOriginalExtension();
|
---|
57 | $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName);
|
---|
58 | $newFile = new File();
|
---|
59 | $newFile->name = $fileName;
|
---|
60 | $newFile->location = $location . DIRECTORY_SEPARATOR . $fileName;
|
---|
61 | $newFile->folder()->associate($folder);
|
---|
62 | $newFile->folder()->increment('no_of_files');
|
---|
63 | $newFile->save();
|
---|
64 | }
|
---|
65 | Notification::send($users, new NewFileCreated("New files added"));
|
---|
66 |
|
---|
67 | Alert::flash("New files added successfully");
|
---|
68 |
|
---|
69 | return redirect()->back();
|
---|
70 | }
|
---|
71 | else {
|
---|
72 | Alert::flash("No files were uploaded", "error");
|
---|
73 |
|
---|
74 | return redirect()->back();
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public function downloadFile($id)
|
---|
79 | {
|
---|
80 | $file = File::find($id);
|
---|
81 | return Storage::download($file->location);
|
---|
82 | }
|
---|
83 |
|
---|
84 | public function renameFile(FileNameRequest $request, $id)
|
---|
85 | {
|
---|
86 | $file = File::find($id);
|
---|
87 | $fileExtension = explode('.', $file->name)[1];
|
---|
88 |
|
---|
89 | $file->name = $request->name . '.' . $fileExtension;
|
---|
90 | $newLocation = 'Departments' . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[1] . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[2] . DIRECTORY_SEPARATOR . $file->name;
|
---|
91 |
|
---|
92 | if(Storage::disk('uploads')->has($newLocation)) {
|
---|
93 | Alert::flash("A file with the same name already exists", "error");
|
---|
94 | return redirect()->back();
|
---|
95 | }
|
---|
96 | else {
|
---|
97 | Storage::disk('uploads')->move($file->location, $newLocation);
|
---|
98 |
|
---|
99 | $file->location = $newLocation;
|
---|
100 | $file->updated_at = Carbon::now();
|
---|
101 | $file->save();
|
---|
102 |
|
---|
103 | Alert::flash($file->name . " updated successfully");
|
---|
104 | return redirect()->back();
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|