source: app/Http/Controllers/Dashboard/FilesController.php@ 53d99ce

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

bug fixes, edited export, added fileSeeder for DB testing

  • Property mode set to 100644
File size: 3.4 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"),
[4b7e2d3]28 "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes))
[c6b84df]29 ]);
30 }
31
32 public function deleteFile($id)
33 {
34 $file = File::find($id);
35 $file->delete();
36 Storage::disk('uploads')->delete($file->location);
[4b7e2d3]37 $file->folder()->decrement('no_of_files');
[c6b84df]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);
[4b7e2d3]62 $newFile->folder()->increment('no_of_files');
[c6b84df]63 $newFile->save();
64 }
65 Notification::send($users, new NewFileCreated("New files added"));
66
67 Alert::flash("New files added successfully");
68
[4b7e2d3]69 return redirect()->back();
[c6b84df]70 }
71 else {
72 Alert::flash("No files were uploaded", "error");
73
[4b7e2d3]74 return redirect()->back();
[c6b84df]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}
Note: See TracBrowser for help on using the repository browser.