first commit
This commit is contained in:
112
app/Http/Controllers/DocumentController.php
Normal file
112
app/Http/Controllers/DocumentController.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Jobs\ProcessDocumentOCR;
|
||||
use App\Models\Document;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class DocumentController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'files.*' => 'required|file|mimes:pdf,docx,xlsx,jpg,png|max:5120',
|
||||
'project_id' => 'required|exists:projects,id',
|
||||
'folder_id' => 'nullable|exists:folders,id'
|
||||
]);
|
||||
|
||||
foreach ($request->file('files') as $file) {
|
||||
$document = Document::create([
|
||||
'name' => $file->getClientOriginalName(),
|
||||
'project_id' => $request->project_id,
|
||||
'folder_id' => $request->folder_id,
|
||||
'created_by' => auth()->id()
|
||||
]);
|
||||
|
||||
$document->addMedia($file)->toMediaCollection('documents');
|
||||
ProcessDocumentOCR::dispatch($document->currentVersion);
|
||||
}
|
||||
|
||||
return redirect()->back()->with('success', 'Files uploaded successfully');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Document $document)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(Document $document)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, Document $document)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(Document $document)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function upload(Request $request, Project $project)
|
||||
{
|
||||
$request->validate([
|
||||
'files.*' => 'required|mimes:pdf,docx,xlsx,jpeg,png|max:2048'
|
||||
]);
|
||||
|
||||
foreach ($request->file('files') as $file) {
|
||||
$document = $project->documents()->create([
|
||||
'name' => $file->getClientOriginalName(),
|
||||
'status' => 'pending'
|
||||
]);
|
||||
|
||||
$this->createVersion($document, $file);
|
||||
}
|
||||
}
|
||||
|
||||
private function createVersion(Document $document, $file)
|
||||
{
|
||||
$version = $document->versions()->create([
|
||||
'file_path' => $file->store("projects/{$document->project_id}/documents"),
|
||||
'hash' => hash_file('sha256', $file),
|
||||
'user_id' => auth()->id()
|
||||
]);
|
||||
|
||||
$document->update(['current_version_id' => $version->id]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user