2018-07-24 19:35:26 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Users;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Http\Requests\AssetFileRequest;
|
2018-07-24 19:35:26 -07:00
|
|
|
use App\Models\Actionlog;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Input;
|
|
|
|
use Illuminate\Support\Facades\Response;
|
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2021-10-06 12:26:45 -07:00
|
|
|
use enshrined\svgSanitize\Sanitizer;
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.6]
|
|
|
|
* @param AssetFileRequest $request
|
|
|
|
* @param int $userId
|
|
|
|
* @return string JSON
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function store(AssetFileRequest $request, $userId = null)
|
|
|
|
{
|
|
|
|
$user = User::find($userId);
|
|
|
|
$destinationPath = config('app.private_uploads') . '/users';
|
|
|
|
|
|
|
|
if (isset($user->id)) {
|
|
|
|
$this->authorize('update', $user);
|
|
|
|
|
|
|
|
$logActions = [];
|
|
|
|
$files = $request->file('file');
|
2021-05-27 13:48:13 -07:00
|
|
|
|
|
|
|
if (is_null($files)){
|
|
|
|
return redirect()->back()->with('error', trans('admin/users/message.upload.nofiles'));
|
|
|
|
}
|
2018-07-24 19:35:26 -07:00
|
|
|
foreach($files as $file) {
|
2021-10-06 12:26:45 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
$extension = $file->getClientOriginalExtension();
|
2021-10-06 12:26:45 -07:00
|
|
|
$file_name = 'user-'.$user->id.'-'.str_random(8).'-'.str_slug(basename($file->getClientOriginalName(), '.'.$extension)).'.'.$extension;
|
|
|
|
|
|
|
|
|
|
|
|
// Check for SVG and sanitize it
|
|
|
|
if ($extension == 'svg') {
|
|
|
|
\Log::error('This is an SVG');
|
|
|
|
\Log::error($file_name);
|
|
|
|
|
|
|
|
$sanitizer = new Sanitizer();
|
|
|
|
$dirtySVG = file_get_contents($file->getRealPath());
|
|
|
|
$cleanSVG = $sanitizer->sanitize($dirtySVG);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Storage::put('private_uploads/users/'.$file_name, $cleanSVG);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::error('Upload no workie :( ');
|
|
|
|
\Log::debug($e);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Storage::put('private_uploads/users/'.$file_name, file_get_contents($file));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
$logAction->user_id = 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';
|
|
|
|
|
|
|
|
if (!$logAction->save()) {
|
|
|
|
return JsonResponse::create(["error" => "Failed validation: " . print_r($logAction->getErrors(), true)], 500);
|
|
|
|
|
|
|
|
}
|
|
|
|
$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-01-27 12:01:42 -08: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)
|
|
|
|
{
|
|
|
|
$user = User::find($userId);
|
|
|
|
$destinationPath = config('app.private_uploads').'/users';
|
|
|
|
|
|
|
|
if (isset($user->id)) {
|
|
|
|
$this->authorize('update', $user);
|
|
|
|
$log = Actionlog::find($fileId);
|
|
|
|
$full_filename = $destinationPath . '/' . $log->filename;
|
|
|
|
if (file_exists($full_filename)) {
|
|
|
|
unlink($destinationPath . '/' . $log->filename);
|
|
|
|
}
|
|
|
|
$log->delete();
|
|
|
|
return redirect()->back()->with('success', trans('admin/users/message.deletefile.success'));
|
|
|
|
}
|
|
|
|
// Prepare the error message
|
|
|
|
$error = trans('admin/users/message.user_not_found', ['id' => $userId]);
|
|
|
|
// Redirect to the licence management page
|
|
|
|
return redirect()->route('users.index')->with('error', $error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
$user = User::find($userId);
|
|
|
|
|
|
|
|
// the license is valid
|
|
|
|
if (isset($user->id)) {
|
|
|
|
$this->authorize('view', $user);
|
|
|
|
|
|
|
|
$log = Actionlog::find($fileId);
|
|
|
|
$file = $log->get_src('users');
|
2021-03-15 12:26:39 -07:00
|
|
|
return Response::download($file); //FIXME this doesn't use the new StorageHelper yet, but it's complicated...
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
// Prepare the error message
|
|
|
|
$error = trans('admin/users/message.user_not_found', ['id' => $userId]);
|
|
|
|
|
|
|
|
// Redirect to the licence management page
|
|
|
|
return redirect()->route('users.index')->with('error', $error);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|