source: app/Http/Controllers/Dashboard/FilesController.php@ 1e31937

Last change on this file since 1e31937 was 0a1fb54, checked in by beratkjufliju <kufliju@…>, 3 years ago

bug fixes

  • Property mode set to 100644
File size: 2.6 KB
Line 
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(',', FileType::find('1')->mimes))
29 ]);
30 }
31
32 public function deleteFile($id)
33 {
34 $file = File::find($id);
35 $file->delete();
36 Storage::disk('local')->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 = $file->getClientOriginalName();
57
58 if(File::where(['folder_id' => $folder->id, 'name' => $fileName])->count() > 0) {
59
60 Alert::flash("The uploaded file already exists", "error");
61 return redirect()->back();
62 }
63
64 $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName);
65 $newFile = new File();
66 $newFile->name = $fileName;
67 $newFile->location = $location . DIRECTORY_SEPARATOR . $fileName;
68 $newFile->folder()->associate($folder);
69 $newFile->folder()->increment('no_of_files');
70 $newFile->save();
71 }
72
73 Alert::flash("New files added successfully");
74
75 return redirect()->back();
76 }
77 else {
78 Alert::flash("No files were uploaded", "error");
79
80 return redirect()->back();
81 }
82 }
83
84 public function downloadFile($id)
85 {
86 $file = File::find($id);
87 return Storage::download($file->location);
88 }
89}
Note: See TracBrowser for help on using the repository browser.