You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.6 KiB
89 lines
2.6 KiB
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Auth;
|
|
use App\Models\File;
|
|
use App\Models\Task;
|
|
use App\Models\Project;
|
|
use App\Models\Business;
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Resources\FileResource;
|
|
use mysql_xdevapi\Exception;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class TaskFileController extends Controller
|
|
{
|
|
public function checkBelonging(int $business, int $project, int $task)
|
|
{
|
|
$business = Business::findOrFail($business);
|
|
$project = Project::findOrFail($project);
|
|
$task = Task::find($task);
|
|
|
|
if (
|
|
$business->id !== $project->business_id
|
|
|| $project->id !== $task['project_id']
|
|
// || $task['user_id']!== Auth::id()
|
|
) {
|
|
\abort(Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
|
|
return [$business, $project, $task];
|
|
}
|
|
|
|
public function index(int $business, int $project, int $task)
|
|
{
|
|
// check permissions
|
|
// owner project
|
|
// admin project
|
|
// colleague project
|
|
// guest or de active
|
|
// return files as file resource
|
|
[$business, $project, $task] = $this->checkBelonging($business, $project, $task);
|
|
return FileResource::collection($task->files);
|
|
}
|
|
|
|
public function sync(Request $request,int $business, int $project, int $task)
|
|
{
|
|
// different size and different validation
|
|
// validate
|
|
// validate the wallet is not so much in debt
|
|
// create record in the db
|
|
// put file in s3
|
|
// return file resource
|
|
[$business, $project, $task] = $this->checkBelonging($business,$project,$task);
|
|
|
|
$this->validate($request, [
|
|
'files' => 'required|array',
|
|
'files.*' => 'int',
|
|
]);
|
|
|
|
$files = File::find($request->files)->each(function (File $file) {
|
|
if ($file->user_id !== Auth::id()) {
|
|
abort(Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
});
|
|
|
|
// sync
|
|
|
|
return FileResource::collection($files);
|
|
}
|
|
|
|
public function download(int $business, int $project, int $task, int $file)
|
|
{
|
|
// requested file belongs to this project and this business
|
|
// check permisson
|
|
// create perma link or temp link
|
|
// return the file resource or stream it
|
|
[$business, $project, $task] = $this->checkBelonging($business, $project, $task);
|
|
|
|
$file = File::find($file);
|
|
if ($file->user_id !== Auth::id()) {
|
|
abort(Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
|
|
return $file->getTemporaryLink();
|
|
}
|
|
}
|