- Timestamp:
- 10/19/21 17:46:21 (3 years ago)
- Branches:
- develop, master
- Children:
- 6b95845
- Parents:
- b9c4a92
- Location:
- app
- Files:
-
- 1 added
- 1 deleted
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
app/Http/Controllers/Dashboard/DocumentsController.php
rb9c4a92 rea7b12a 5 5 use App\Helpers\Alert; 6 6 use App\Http\Requests\Dashboard\DocumentRequest; 7 use App\Http\Requests\Dashboard\FileRequest; 8 use App\Http\Requests\Dashboard\PasswordSettingsRequest; 7 9 use App\Models\Department; 8 10 use App\Models\Document; 9 11 use App\Models\File; 10 12 use App\Services\UploadService; 13 use Carbon\Carbon; 11 14 use Illuminate\Http\Request; 15 use Illuminate\Http\Response; 12 16 use Illuminate\Support\Facades\Storage; 13 17 use App\Http\Controllers\Controller; … … 115 119 $document->description = $request->description; 116 120 117 $location = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $request->name;121 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $request->name; 118 122 119 123 if (!Storage::disk('local')->has($location)) { … … 121 125 } 122 126 123 foreach ($request->file_item as $file) {124 $file->storeAs($location . DIRECTORY_SEPARATOR, $file->getClientOriginalName());125 $newFile = new File();126 $newFile->link = $location . DIRECTORY_SEPARATOR . $file->getClientOriginalName();127 $newFile->document()->associate($document);128 $newFile->save();129 }130 131 127 $document->save(); 128 129 if ($request->has('file_item')) { 130 foreach ($request->file_item as $file) { 131 $file->storeAs($location . DIRECTORY_SEPARATOR, $file->getClientOriginalName()); 132 $newFile = new File(); 133 $newFile->name = $file->getClientOriginalName(); 134 $newFile->location = $location . DIRECTORY_SEPARATOR . $file->getClientOriginalName(); 135 $newFile->document()->associate($document); 136 $newFile->save(); 137 } 138 } 132 139 133 140 Alert::flash("New document created successfully"); … … 140 147 return view("dashboard.documents.edit")->with([ 141 148 "document" => Document::findOrFail($id), 142 "departments" => Department::all() 149 "departments" => Department::all(), 150 "files" => File::where('document_id', $id)->get() 143 151 ]); 144 152 } … … 147 155 { 148 156 $document = Document::findOrFail($id); 157 $files = File::where('document_id', $id)->get(); 149 158 150 159 $department = Department::find($request->department); 151 160 152 161 $document->department()->associate($department); 162 163 $oldLocation = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name; 153 164 154 165 $document->name = $request->name; … … 156 167 $document->description = $request->description; 157 168 158 $oldLocation = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $document->name; 159 $location = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $request->name; 169 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $request->name; 160 170 161 171 if ($document->isDirty('name')) { 162 172 if (!Storage::disk('local')->has($location)) { 163 173 Storage::disk('local')->move($oldLocation, $location); 164 } 165 } 166 167 $hasFileError = false; 174 foreach($files as $file){ 175 $file->location = "test"; 176 } 177 } 178 } 179 168 180 if ($request->has('file_item')) { 169 181 foreach ($request->file_item as $file) { 170 182 $fileName = $file->getClientOriginalName(); 171 if(Storage::disk('local')->has($location . DIRECTORY_SEPARATOR . $fileName)) { 172 $hasFileError = true; 173 break; 183 if (Storage::disk('local')->has($location . DIRECTORY_SEPARATOR . $fileName)) { 184 // $hasFileError = true; 185 // break; 186 $NewFileName = time() . $fileName; 187 $file->storeAs($location . DIRECTORY_SEPARATOR, $NewFileName); 188 $newFile = new File(); 189 $newFile->name = rand() . $fileName; 190 $newFile->location = $location . DIRECTORY_SEPARATOR . $NewFileName; 191 $newFile->document()->associate($document); 192 $newFile->save(); 193 } else { 194 $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName); 195 $newFile = new File(); 196 $newFile->name = $fileName; 197 $newFile->location = $location . DIRECTORY_SEPARATOR . $fileName; 198 $newFile->document()->associate($document); 199 $newFile->save(); 174 200 } 175 $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName); 176 $newFile = new File(); 177 $newFile->link = $location . DIRECTORY_SEPARATOR . $fileName; 178 $newFile->document()->associate($document); 179 $newFile->save(); 180 } 181 } 182 183 if($hasFileError) { 184 Alert::flash('Document with the same name exists', 'error'); 185 return redirect()->route("dashboard.documents.edit", ['id' => $document->id]); 201 } 186 202 } 187 203 188 204 $document->save(); 189 205 206 // if($hasFileError) { 207 // Alert::flash('Document with the same name exists', 'error'); 208 // return redirect()->route("dashboard.documents.edit", ['id' => $document->id]); 209 // } 210 211 212 190 213 Alert::flash("Document edited successfully"); 191 214 192 return redirect()-> route("dashboard.documents.index");215 return redirect()->back(); 193 216 } 194 217 … … 230 253 { 231 254 $document = Document::find($id); 255 $files = File::where('document_id', $id)->get(); 232 256 if (auth()->user()->hasPermission("delete_all_documents")) { 257 // @dd($files); 258 foreach ($files as $file) { 259 $file->delete(); 260 } 233 261 $document->delete(); 262 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name; 263 Storage::disk('local')->deleteDirectory($location); 234 264 Alert::flash($document->name . " deleted successfully"); 235 265 } 236 266 return redirect()->route("dashboard.documents.index"); 237 267 } 268 269 public function deleteFile($id) 270 { 271 $file = File::find($id); 272 $file->delete(); 273 Storage::disk('local')->delete($file->location); 274 275 Alert::flash($file->name . " deleted successfully"); 276 277 return redirect()->back(); 278 } 279 280 public function downloadFile($id) 281 { 282 $file = File::find($id); 283 return Storage::download($file->location); 284 } 285 286 public function renameFile(FileRequest $request, $id) 287 { 288 $file = File::find($id); 289 $fileExtension = explode('.', $file->name)[1]; 290 291 $file->name = $request->name . '.' . $fileExtension; 292 $newLocation = 'Departments' . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[1] . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[2] . DIRECTORY_SEPARATOR . $file->name; 293 294 if(Storage::disk('local')->has($newLocation)) { 295 Alert::flash("A file with the same name already exists", "error"); 296 return redirect()->back(); 297 } 298 else { 299 Storage::disk('local')->copy($file->location, $newLocation); 300 Storage::disk('local')->delete($file->location); 301 302 $file->location = $newLocation; 303 $file->save(); 304 305 Alert::flash($file->name . " updated successfully"); 306 return redirect()->back(); 307 } 308 } 238 309 } -
app/Http/Requests/Dashboard/DocumentRequest.php
rb9c4a92 rea7b12a 5 5 use App\Models\Department; 6 6 use App\Models\Document; 7 use App\Models\FileType;8 7 use Illuminate\Foundation\Http\FormRequest; 9 8 … … 32 31 public function rules() 33 32 { 34 35 33 $rules = [ 36 34 "arch_id" => [ … … 57 55 if ($this->isMethod("patch")) { 58 56 $fileRules = [ 59 "file_item.*" => "mimes:jpg,jpeg,png |max:4096"57 "file_item.*" => "mimes:jpg,jpeg,png,pdf|max:4096" 60 58 ]; 61 59 } else { 62 60 $fileRules = [ 63 "file_item.*" => "mimes:jpg,jpeg,png |max:4096"61 "file_item.*" => "mimes:jpg,jpeg,png,pdf|max:4096" 64 62 ]; 65 63 } -
app/Models/Document.php
rb9c4a92 rea7b12a 22 22 } 23 23 24 public function ago() {25 return Carbon::parse($this->created_at)->diffForHumans();26 }27 28 24 public function files() 29 25 { -
app/Models/File.php
rb9c4a92 rea7b12a 4 4 5 5 use Illuminate\Database\Eloquent\Model; 6 use Illuminate\Support\Facades\Storage; 6 7 7 8 class File extends Model 8 9 { 9 10 protected $table = "files"; 10 protected $fillable = [" link", "document_id"];11 protected $fillable = ["name", "location", "document_id"]; 11 12 12 13 public function document() … … 14 15 return $this->belongsTo(Document::class); 15 16 } 17 18 public function getSize($location) 19 { 20 $fileSize = Storage::disk('local')->size($location) / 1024 / 1024; 21 $fileSize = round($fileSize, 2); 22 return $fileSize; 23 } 16 24 }
Note:
See TracChangeset
for help on using the changeset viewer.