[c6b84df] | 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"),
|
---|
[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();
|
---|
[8fdb18e] | 36 | Storage::disk('local')->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) {
|
---|
[e78295c] | 56 | $fileName = $file->getClientOriginalName();
|
---|
| 57 |
|
---|
| 58 | if(File::where(['folder_id' => $folder->id, 'name' => $fileName])->count() > 0) {
|
---|
| 59 |
|
---|
[0a1fb54] | 60 | Alert::flash("The uploaded file already exists", "error");
|
---|
[e78295c] | 61 | return redirect()->back();
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[c6b84df] | 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);
|
---|
[4b7e2d3] | 69 | $newFile->folder()->increment('no_of_files');
|
---|
[c6b84df] | 70 | $newFile->save();
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | Alert::flash("New files added successfully");
|
---|
| 74 |
|
---|
[4b7e2d3] | 75 | return redirect()->back();
|
---|
[c6b84df] | 76 | }
|
---|
| 77 | else {
|
---|
| 78 | Alert::flash("No files were uploaded", "error");
|
---|
| 79 |
|
---|
[4b7e2d3] | 80 | return redirect()->back();
|
---|
[c6b84df] | 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | public function downloadFile($id)
|
---|
| 85 | {
|
---|
| 86 | $file = File::find($id);
|
---|
| 87 | return Storage::download($file->location);
|
---|
| 88 | }
|
---|
| 89 | }
|
---|