2018-07-24 19:35:26 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Users;
|
|
|
|
|
2023-09-07 12:22:14 -07:00
|
|
|
use App\Helpers\StorageHelper;
|
2018-07-24 19:35:26 -07:00
|
|
|
use App\Http\Controllers\Controller;
|
2024-02-07 07:21:48 -08:00
|
|
|
use App\Http\Requests\UploadFileRequest;
|
2018-07-24 19:35:26 -07:00
|
|
|
use App\Models\Actionlog;
|
|
|
|
use App\Models\User;
|
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2021-10-06 12:26:45 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
class UserFilesController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Return JSON response with a list of user details for the getIndex() view.
|
|
|
|
*
|
2024-02-07 07:21:48 -08:00
|
|
|
* @param UploadFileRequest $request
|
2018-07-24 19:35:26 -07:00
|
|
|
* @param int $userId
|
|
|
|
* @return string JSON
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2024-02-07 07:21:48 -08:00
|
|
|
*@author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.6]
|
2018-07-24 19:35:26 -07:00
|
|
|
*/
|
2024-02-07 07:21:48 -08:00
|
|
|
public function store(UploadFileRequest $request, $userId = null)
|
2018-07-24 19:35:26 -07:00
|
|
|
{
|
|
|
|
$user = User::find($userId);
|
2021-06-10 13:15:52 -07:00
|
|
|
$destinationPath = config('app.private_uploads').'/users';
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
if (isset($user->id)) {
|
|
|
|
$this->authorize('update', $user);
|
|
|
|
|
|
|
|
$logActions = [];
|
|
|
|
$files = $request->file('file');
|
2021-05-27 13:48:13 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (is_null($files)) {
|
2021-05-27 13:48:13 -07:00
|
|
|
return redirect()->back()->with('error', trans('admin/users/message.upload.nofiles'));
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
foreach ($files as $file) {
|
2024-02-07 07:21:48 -08:00
|
|
|
$file_name = $request->handleFile('private_uploads/users/', 'user-'.$user->id, $file);
|
2021-10-06 12:26:45 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
//Log the uploaded file to the log
|
|
|
|
$logAction = new Actionlog();
|
|
|
|
$logAction->item_id = $user->id;
|
|
|
|
$logAction->item_type = User::class;
|
2024-09-25 07:46:57 -07:00
|
|
|
$logAction->created_by = auth()->id();
|
2019-12-11 11:09:54 -08:00
|
|
|
$logAction->note = $request->input('notes');
|
2018-07-24 19:35:26 -07:00
|
|
|
$logAction->target_id = null;
|
|
|
|
$logAction->created_at = date("Y-m-d H:i:s");
|
2021-10-06 12:26:45 -07:00
|
|
|
$logAction->filename = $file_name;
|
2018-07-24 19:35:26 -07:00
|
|
|
$logAction->action_type = 'uploaded';
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $logAction->save()) {
|
|
|
|
return JsonResponse::create(['error' => 'Failed validation: '.print_r($logAction->getErrors(), true)], 500);
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
$logActions[] = $logAction;
|
|
|
|
}
|
2021-01-27 12:01:42 -08:00
|
|
|
// dd($logActions);
|
|
|
|
return redirect()->back()->with('success', trans('admin/users/message.upload.success'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
return redirect()->back()->with('error', trans('admin/users/message.upload.nofiles'));
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete file
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.6]
|
|
|
|
* @param int $userId
|
|
|
|
* @param int $fileId
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function destroy($userId = null, $fileId = null)
|
|
|
|
{
|
2024-05-22 09:20:03 -07:00
|
|
|
if ($user = User::find($userId)) {
|
2018-07-24 19:35:26 -07:00
|
|
|
|
2024-05-22 09:20:03 -07:00
|
|
|
$this->authorize('delete', $user);
|
|
|
|
$rel_path = 'private_uploads/users';
|
|
|
|
|
|
|
|
|
2024-05-22 09:44:49 -07:00
|
|
|
if ($log = Actionlog::find($fileId)) {
|
|
|
|
$filename = $log->filename;
|
|
|
|
$log->delete();
|
|
|
|
|
|
|
|
if (Storage::exists($rel_path.'/'.$filename)) {
|
|
|
|
Storage::delete($rel_path.'/'.$filename);
|
2024-05-22 09:20:03 -07:00
|
|
|
return redirect()->back()->with('success', trans('admin/users/message.deletefile.success'));
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2024-05-22 09:44:49 -07:00
|
|
|
}
|
2024-05-22 09:20:03 -07:00
|
|
|
|
2024-05-22 09:44:49 -07:00
|
|
|
// The log record doesn't exist somehow
|
|
|
|
return redirect()->back()->with('success', trans('admin/users/message.deletefile.success'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
2024-05-22 09:20:03 -07:00
|
|
|
|
|
|
|
return redirect()->route('users.index')->with('error', trans('admin/users/message.user_not_found', ['id' => $userId]));
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display/download the uploaded file
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.6]
|
|
|
|
* @param int $userId
|
|
|
|
* @param int $fileId
|
|
|
|
* @return mixed
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function show($userId = null, $fileId = null)
|
|
|
|
{
|
2023-10-09 08:43:14 -07:00
|
|
|
|
2024-10-16 16:08:38 -07:00
|
|
|
|
2023-10-09 08:43:14 -07:00
|
|
|
if (empty($fileId)) {
|
|
|
|
return redirect()->route('users.show')->with('error', 'Invalid file request');
|
|
|
|
}
|
|
|
|
|
2024-10-21 08:34:54 -07:00
|
|
|
if ($user = User::find($userId)) {
|
2023-09-05 10:28:01 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
$this->authorize('view', $user);
|
|
|
|
|
2023-10-09 08:43:14 -07:00
|
|
|
if ($log = Actionlog::whereNotNull('filename')->where('item_id', $user->id)->find($fileId)) {
|
2024-10-16 16:08:38 -07:00
|
|
|
$file = 'private_uploads/users/'.$log->filename;
|
|
|
|
|
2024-10-21 08:34:54 -07:00
|
|
|
try {
|
|
|
|
return StorageHelper::showOrDownloadFile($file, $log->filename);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return redirect()->route('users.show', ['user' => $user])->with('error', trans('general.file_not_found'));
|
2023-10-09 07:17:03 -07:00
|
|
|
}
|
2023-09-05 10:28:01 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2024-10-21 08:34:54 -07:00
|
|
|
// todo
|
|
|
|
|
|
|
|
return redirect()->back()->with('error', trans('general.file_not_found'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
|
2023-09-05 10:28:01 -07:00
|
|
|
// Redirect to the user management page if the user doesn't exist
|
|
|
|
return redirect()->route('users.index')->with('error', trans('admin/users/message.user_not_found', ['id' => $userId]));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|