source: app/Http/Controllers/Dashboard/FilesController.php@ c6b84df

develop
Last change on this file since c6b84df was c6b84df, checked in by beratkjufliju <kufliju@…>, 3 years ago

added fileTypes controller, notifications, excel export, edited views

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[c6b84df]1<?php
2
3namespace App\Http\Controllers\Dashboard;
4
5use App\Helpers\Alert;
6use App\Http\Controllers\Controller;
7use App\Http\Requests\Dashboard\FileNameRequest;
8use App\Http\Requests\Dashboard\FileRequest;
9use App\Models\FileType;
10use App\Models\Folder;
11use App\Models\File;
12use App\Models\User;
13use App\Notifications\NewFileCreated;
14use Carbon\Carbon;
15use Illuminate\Support\Facades\Notification;
16use Illuminate\Support\Facades\Storage;
17
18class 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}
Note: See TracBrowser for help on using the repository browser.