Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support For Vanity Urls #507

Merged
merged 11 commits into from
Aug 14, 2023
44 changes: 44 additions & 0 deletions app/Controllers/MediaController.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,50 @@ public function download(Request $request, Response $response, string $userCode,
return $this->streamMedia($request, $response, $this->storage, $media, 'attachment');
}


/**
* @param Request $request
* @param Response $response
* @param string $vanity
* @param string $id
*
* @return Response
* @throws HttpNotFoundException
* @throws HttpBadRequestException
*/
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();
}

$data = $request->getParsedBody();
SrS2225a marked this conversation as resolved.
Show resolved Hide resolved
$vanity = $data['vanity'];
SrS2225a marked this conversation as resolved.
Show resolved Hide resolved
$vanity = strtolower(preg_replace('/[^a-z0-9]+/', '-', $vanity));

if (!$media) {
throw new HttpNotFoundException($request);
}

if ($vanity === '' || $media->code === $vanity) {
throw new HttpBadRequestException($request);
}

$this->database->query(
'UPDATE `uploads` SET `code` = ? WHERE `id` = ?',
SrS2225a marked this conversation as resolved.
Show resolved Hide resolved
[$vanity, $media->id]
);

$this->logger->info('User '.$this->session->get('username').' created a vanity link for media '.$media->id);

return $response;
SrS2225a marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param Request $request
* @param Response $response
Expand Down
1 change: 1 addition & 0 deletions app/routes.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

$group->post('/upload/{id}/publish', [MediaController::class, 'togglePublish'])->setName('upload.publish');
$group->post('/upload/{id}/unpublish', [MediaController::class, 'togglePublish'])->setName('upload.unpublish');
$group->post('/upload/{id}/vanity', [MediaController::class, 'createVanity'])->setName('upload.vanity');
$group->get('/upload/{id}/raw', [MediaController::class, 'getRawById'])->add(AdminMiddleware::class)->setName('upload.raw');
$group->map(['GET', 'POST'], '/upload/{id}/delete', [MediaController::class, 'delete'])->setName('upload.delete');

Expand Down
3 changes: 2 additions & 1 deletion resources/lang/en.lang.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
'download' => 'Download',
'upload' => 'Upload',
'delete' => 'Delete',
'confirm' => 'Confirm',
'vanity_url' => 'Custom URL',
'publish' => 'Publish',
'hide' => 'Hide',
'files' => 'Files',
Expand Down Expand Up @@ -58,7 +60,6 @@
'reg_date' => 'Registration Date',
'none' => 'None',
'open' => 'Open',
'confirm' => 'Confirmation',
'confirm_string' => 'Are you sure?',
'installed' => 'Installation completed successfully!',
'bad_login' => 'Wrong credentials.',
Expand Down
19 changes: 19 additions & 0 deletions resources/templates/comp/modal_vanity.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div class="modal fade" id="modalVanity" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">{{ lang('vanity_url') }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<input type="text" class="form-control" id="modalVanity-input" >
SrS2225a marked this conversation as resolved.
Show resolved Hide resolved
</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>
<button type="button" class="btn btn-secondary" data-dismiss="modal">{{ lang('no') }}</button>
</div>
</div>
</div>
</div>
2 changes: 2 additions & 0 deletions resources/templates/dashboard/grid.twig
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +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 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>
Expand Down Expand Up @@ -68,4 +69,5 @@
<div class="text-center text-muted"><i>{{ lang('no_media') }}</i></div>
{% endif %}
</div>
{% include 'comp/modal_vanity.twig' %}
{% endblock %}
2 changes: 2 additions & 0 deletions resources/templates/dashboard/list.twig
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{% include 'comp/navbar.twig' %}
<div class="container">
{% include 'comp/alert.twig' %}
{% include 'comp/modal_vanity.twig' %}

<div class="card shadow-sm">
<div class="card-body">
Expand Down Expand Up @@ -77,6 +78,7 @@
{% else %}
<a href="javascript:void(0)" class="btn btn-sm btn-outline-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 %}
<a href="javascript:void(0)" class="btn btn-sm btn-outline-info public-vanity" data-id="{{ media.id }}" data-toggle="tooltip" title="{{ lang('vanity') }}"><i class="fas fa-star"></i></a>
<a href="javascript:void(0)" class="btn btn-sm btn-outline-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></a>
</div>
</td>
Expand Down
2 changes: 2 additions & 0 deletions resources/templates/upload/public.twig
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +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-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>
Expand Down Expand Up @@ -162,4 +163,5 @@
</div>
</div>
{% include 'comp/modal_delete.twig' %}
{% include 'comp/modal_vanity.twig' %}
{% endblock %}
14 changes: 14 additions & 0 deletions src/js/app.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ var app = {

$('.user-delete').click(app.modalDelete);
$('.public-delete').click(app.modalDelete);
$('.public-vanity').click(app.modalVanity);
$('.media-delete').click(app.mediaDelete);
$('.publish-toggle').click(app.publishToggle);

$('.refresh-token').click(app.refreshToken);
$('#themes').mousedown(app.loadThemes);
$('.checkForUpdatesButton').click(app.checkForUpdates);
Expand All @@ -57,6 +59,18 @@ var app = {
$('#modalDelete-link').attr('href', $(this).data('link'));
$('#modalDelete').modal('show');
},
modalVanity: function () {
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 () {
$callerButton.tooltip('dispose');
window.location.href = window.AppConfig.base_url + '/home';
SrS2225a marked this conversation as resolved.
Show resolved Hide resolved
});
})
},
publishToggle: function () {
var id = $(this).data('id');
var $callerButton = $(this);
Expand Down