added fixes per request by author

This commit is contained in:
SrS2225a 2023-01-17 21:17:52 -08:00
parent 58744bd092
commit acaceea3f1
5 changed files with 31 additions and 23 deletions

View file

@ -4,6 +4,7 @@ namespace App\Controllers;
use App\Database\Repositories\UserRepository;
use App\Web\UA;
use App\helpers;
use GuzzleHttp\Psr7\Stream;
use Intervention\Image\Constraint;
use Intervention\Image\ImageManagerStatic as Image;
@ -201,34 +202,28 @@ class MediaController extends Controller
*/
public function createVanity(Request $request, Response $response, int $id): Response
{
if (!$this->session->get('admin')) {
$media = $this->database->query('SELECT * FROM `uploads` WHERE `id` = ? LIMIT 1', $id)->fetch();
} else {
$media = $this->database->query(
'SELECT * FROM `uploads` WHERE `id` = ? AND `user_id` = ? LIMIT 1',
[$id, $this->session->get('user_id')]
)->fetch();
}
$media = $this->database->query('SELECT * FROM `uploads` WHERE `id` = ? LIMIT 1', $id)->fetch();
$data = $request->getParsedBody();
$vanity = $data['vanity'];
$vanity = strtolower(preg_replace('/[^a-z0-9]+/', '-', $vanity));
$vanity = param($request, 'vanity');
$vanity = preg_replace('/[^a-z0-9]+/', '-', strtolower($vanity));
//handle collisions
$collision = $this->database->query('SELECT * FROM `uploads` WHERE `code` = ? AND `id` != ? LIMIT 1',[$vanity, $id])->fetch();
if (!$media) {
throw new HttpNotFoundException($request);
}
if ($vanity === '' || $media->code === $vanity) {
if ($vanity === '' || $collision) {
throw new HttpBadRequestException($request);
}
$this->database->query(
'UPDATE `uploads` SET `code` = ? WHERE `id` = ?',
[$vanity, $media->id]
);
$this->database->query('UPDATE `uploads` SET `code` = ? WHERE `id` = ?',[$vanity, $media->id]);
$media->code = $vanity;
$response->getBody()->write(json_encode($media));
$this->logger->info('User '.$this->session->get('username').' created a vanity link for media '.$media->id);
return $response;
}

View file

@ -8,7 +8,7 @@
</button>
</div>
<div class="modal-body">
<input type="text" class="form-control" id="modalVanity-input" >
<input type="text" class="form-control" id="modalVanity-input" maxlength="64">
</div>
<div class="modal-footer">
<button class="btn btn-primary media-vanity" id="modalVanity-link"><i class="fas fa-check fa-fw"></i> {{ lang('confirm') }}</button>

View file

@ -28,7 +28,7 @@
{% else %}
<a class="btn btn-sm btn-info publish-toggle" data-toggle="tooltip" title="{{ lang('publish') }}" data-id="{{ media.id }}" data-published="{{ media.published }}"><i class="fas fa-check-circle"></i></a>
{% endif %}
<button class="btn btn-primary btn-sm public-vanity" data-link="{{ route('upload.delete', {'id': media.id}) }}" data-id="{{ media.id }}" data-toggle="tooltip" title="{{ lang('vanity') }}"><i class="fas fa-star"></i></button>
<button class="btn btn-info btn-sm public-vanity" data-link="{{ route('upload.delete', {'id': media.id}) }}" data-id="{{ media.id }}" data-toggle="tooltip" title="{{ lang('vanity') }}"><i class="fas fa-star"></i></button>
<button type="button" class="btn btn-sm btn-danger media-delete" data-link="{{ route('upload.delete', {'id': media.id}) }}" data-id="{{ media.id }}" data-toggle="tooltip" title="{{ lang('delete') }}">
<i class="fas fa-trash"></i>
</button>

View file

@ -49,7 +49,7 @@
<a href="{{ url }}/raw" class="btn btn-secondary my-2 my-sm-0" data-toggle="tooltip" title="{{ lang('raw') }}"><i class="fas fa-file-alt fa-lg fa-fw"></i></a>
<a href="{{ url }}/download" class="btn btn-warning my-2 my-sm-0" data-toggle="tooltip" title="{{ lang('download') }}"><i class="fas fa-cloud-download-alt fa-lg fa-fw"></i></a>
{% if session.get('logged') %}
<a href="javascript:void(0)" class="btn btn-primary my-2 my-sm-0 public-vanity" data-link="{{ route('upload.vanity', {'id': media.mediaId}) }}" data-id="{{ media.mediaId }}" data-toggle="tooltip" title="{{ lang('vanity') }}"><i class="fas fa-star fa-lg fa-fw"></i></a>
<a href="javascript:void(0)" class="btn btn-info my-2 my-sm-0 public-vanity" data-link="{{ route('upload.vanity', {'id': media.mediaId}) }}" data-id="{{ media.mediaId }}" data-toggle="tooltip" title="{{ lang('vanity') }}"><i class="fas fa-star fa-lg fa-fw"></i></a>
<a href="javascript:void(0)" class="btn btn-danger my-2 my-sm-0 public-delete" data-link="{{ route('upload.delete', {'id': media.mediaId}) }}" data-toggle="tooltip" title="{{ lang('delete') }}"><i class="fas fa-trash fa-lg fa-fw"></i></a>
{% endif %}
</div>

View file

@ -63,11 +63,24 @@ var app = {
var id = $(this).data('id');
$('#modalVanity').modal('show');
$('#modalVanity-link').click(function () {
var vanity = $('#modalVanity-input').val();
var $callerButton = $(this);
$.post(window.AppConfig.base_url + '/upload/' + id + '/vanity', {vanity: vanity}, function () {
$.post(window.AppConfig.base_url + '/upload/' + id + '/vanity', {vanity: $('#modalVanity-input').val()}, function (data, status) {
$callerButton.tooltip('dispose');
window.location.href = window.AppConfig.base_url + '/home';
$('#modalVanity').modal('hide');
$('#modalVanity-input').val('');
var data = JSON.parse(data);
if ($('#media_' + id).find('.btn-group').length >0) {
$('#media_' + id).find('.btn-group').find('a').each(function (item) {
var oldUrl = $(this).attr('href');
var newUrl = oldUrl.replace(oldUrl.substr(oldUrl.lastIndexOf('/') + 1), data.code);
$(this).attr('href', newUrl);
})
} else {
var oldUrl = window.location.href;
var newUrl = oldUrl.replace(oldUrl.substr(oldUrl.lastIndexOf('/') + 1), data.code);
window.location.href = newUrl;
}
});
})
},