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(',', explode(':', FileType::find('1')->mimes)[1]))
|
---|
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 |
|
---|
38 | Alert::flash($file->name . " deleted successfully");
|
---|
39 |
|
---|
40 | return redirect()->back();
|
---|
41 | }
|
---|
42 |
|
---|
43 | public function store(FileRequest $request)
|
---|
44 | {
|
---|
45 | $file = new File();
|
---|
46 |
|
---|
47 | $folder = Folder::find($request->folder);
|
---|
48 |
|
---|
49 | $location = $folder->location;
|
---|
50 |
|
---|
51 | $users = User::all();
|
---|
52 |
|
---|
53 | if ($request->has('file_item')) {
|
---|
54 | foreach ($request->file_item as $file) {
|
---|
55 | $fileName = $folder->name . '-' . uniqid() . '.' . $file->getClientOriginalExtension();
|
---|
56 | $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName);
|
---|
57 | $newFile = new File();
|
---|
58 | $newFile->name = $fileName;
|
---|
59 | $newFile->location = $location . DIRECTORY_SEPARATOR . $fileName;
|
---|
60 | $newFile->folder()->associate($folder);
|
---|
61 | $newFile->save();
|
---|
62 | }
|
---|
63 | Notification::send($users, new NewFileCreated("New files added"));
|
---|
64 |
|
---|
65 | Alert::flash("New files added successfully");
|
---|
66 |
|
---|
67 | return redirect()->route("dashboard.files.index");
|
---|
68 | }
|
---|
69 | else {
|
---|
70 | Alert::flash("No files were uploaded", "error");
|
---|
71 |
|
---|
72 | return redirect()->route("dashboard.files.index");
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public function downloadFile($id)
|
---|
77 | {
|
---|
78 | $file = File::find($id);
|
---|
79 | return Storage::download($file->location);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public function renameFile(FileNameRequest $request, $id)
|
---|
83 | {
|
---|
84 | $file = File::find($id);
|
---|
85 | $fileExtension = explode('.', $file->name)[1];
|
---|
86 |
|
---|
87 | $file->name = $request->name . '.' . $fileExtension;
|
---|
88 | $newLocation = 'Departments' . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[1] . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[2] . DIRECTORY_SEPARATOR . $file->name;
|
---|
89 |
|
---|
90 | if(Storage::disk('uploads')->has($newLocation)) {
|
---|
91 | Alert::flash("A file with the same name already exists", "error");
|
---|
92 | return redirect()->back();
|
---|
93 | }
|
---|
94 | else {
|
---|
95 | Storage::disk('uploads')->move($file->location, $newLocation);
|
---|
96 |
|
---|
97 | $file->location = $newLocation;
|
---|
98 | $file->updated_at = Carbon::now();
|
---|
99 | $file->save();
|
---|
100 |
|
---|
101 | Alert::flash($file->name . " updated successfully");
|
---|
102 | return redirect()->back();
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|