Skip to content

Commit

Permalink
✨ Ability to test PDF and Media archiving in settings (issue #34)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarceauKa committed Oct 28, 2019
1 parent c8802ca commit 809b33f
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 8 deletions.
84 changes: 84 additions & 0 deletions app/Http/Controllers/Api/Manage/ArchiveController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace App\Http\Controllers\Api\Manage;

use App\Http\Controllers\Controller;
use App\Services\LinkArchive\LinkArchive;
use App\Services\LinkArchive\YoutubeDlProvider;
use App\Services\Shaarli\Shaarli;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Storage;

class ArchiveController extends Controller
{
public function __construct()
{
$this->middleware('demo');
}

public function check(Request $request, Shaarli $shaarli, string $type)
{
if (false === in_array($type, ['pdf', 'media'])) {
abort(404);
}

if ($type === 'pdf') {
if (false === $shaarli->getLinkArchivePdf()) {
return $this->sendError(__('Archive as PDF is not enabled'));
}

$exec = $shaarli->getNodeBin();
exec($exec . ' --version', $result);

if (empty($result)) {
return $this->sendError(__('Your node path is unreachable: :path', ['path' => $exec]));
}

$dir = base_path('node_modules/puppeteer/.local-chromium');

if (false === is_dir($dir)) {
return $this->sendError(__('Puppeteer dependencies not installed, run `npm install @nesk/puphpeteer --no-save`'));
}

try {
$name = LinkArchive::archive(url()->route('home'), 'pdf');
} catch (\Exception $e) {
return $this->sendError(__('Unable to create archive, error is: :message', ['message' => $e->getMessage()]));
}

Storage::disk('archives')->delete($name);
}

if ($type === 'media') {
if (false === $shaarli->getLinkArchiveMedia()) {
return $this->sendError(__('Archive as Media is not enabled'));
}

$exec = $shaarli->getYoutubeDlBin();
exec($exec . ' --version', $result);

if (empty($result)) {
return $this->sendError(__('Your youtube-dl path is unreachable: :path', ['path' => $exec]));
}

$status = YoutubeDlProvider::test('https://www.youtube.com/watch?v=oavMtUWDBTM');

if ($status === false) {
return $this->sendError(__("Unknow error. Check if python is correctly installed on your system"));
}
}

return response()->json([
'status' => 'success',
]);
}

protected function sendError(string $message): JsonResponse
{
return response()->json([
'status' => 'fail',
'message' => $message
], 422);
}
}
11 changes: 8 additions & 3 deletions app/Services/LinkArchive/YoutubeDlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ public function isEnabled(): bool

public function canArchive(): bool
{
/*try {
return true;
}

public static function test(string $url): bool
{
try {
$dl = new YoutubeDl([
'skip-download' => true,
'restrict-filenames' => true,
Expand All @@ -64,15 +69,15 @@ public function canArchive(): bool

$dl->setBinPath(app('shaarli')->getYoutubeDlBin());
$dl->setDownloadPath(storage_path('app/archives'));
$result = $dl->download($this->url);
$result = $dl->download($url);

if (false === $result instanceof Video) {
return false;
}
} catch (ExecutableNotFoundException $e) {
unset($e);
return false;
}*/
}

return true;
}
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

## Added

- Ability to test PDF and Media archiving in settings (issue #34)

## Changed

- Add `php artisan storage:link` to the update command
Expand Down
2 changes: 1 addition & 1 deletion public/js/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/js/app.js": "/js/app.js?id=2ecf9ae28ca809c7feeb",
"/js/app.js": "/js/app.js?id=8d8a863294f153396628",
"/css/app.css": "/css/app.css?id=568603584999965bb4e5",
"/js/manifest.js": "/js/manifest.js?id=3c768977c2574a34506e",
"/js/vendor.js": "/js/vendor.js?id=24c264fe7e2a39bcb572"
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ from youtube, soundcloud, vimeo and [few more sites](http://ytdl-org.github.io/y

- [Puppeteer](https://github.com/GoogleChrome/puppeteer) will be used by default to save the webpage as a PDF.

You can adjust archiving configuration in the settings section.
You can adjust archiving configuration and test it in the settings section.

## Contribute

Expand Down
65 changes: 65 additions & 0 deletions resources/js/components/CheckArchive.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<template>
<button type="button"
class="btn btn-primary"
:class="{'btn-danger': status === 'fail', 'btn-success': status === 'success'}"
@click="check"
:disabled="loading"
>
<span v-if="!loading && status === null">{{ __('Check') }}</span>
<span v-else-if="status === 'success'">{{ __('Success') }}</span>
<span v-else-if="status === 'fail'">{{ __('Fail') }}</span>
<span v-else>
<div class="spinner-grow spinner-grow-sm" role="status"></div>
</span>
</button>
</template>

<script>
import httpErrors from "../mixins/httpErrors";
export default {
mixins: [
httpErrors,
],
props: {
type: {
type: String,
required: true
},
},
data() {
return {
loading: false,
status: null,
}
},
methods: {
check() {
this.loading = true;
axios.get(`/api/manage/archive/check/${this.type}`)
.then(response => {
this.status = response.data.status;
this.clear();
})
.catch(error => {
this.status = 'fail';
this.setHttpError(error);
this.toastHttpError(this.__('Whoops!'));
this.clear();
})
},
clear() {
this.loading = false;
setTimeout(() => {
this.status = null;
}, 5000);
}
}
}
</script>
14 changes: 12 additions & 2 deletions resources/views/manage/settings.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@

<div class="form-group">
<label for="node_bin">{{ __('Node.js binary') }}</label>
<input type="text" class="form-control {{ $errors->has('node_bin') ? ' is-invalid' : '' }}" name="node_bin" id="node_bin" value="{{ $settings['node_bin'] }}">
<div class="input-group">
<input type="text" class="form-control {{ $errors->has('node_bin') ? ' is-invalid' : '' }}" name="node_bin" id="node_bin" value="{{ $settings['node_bin'] }}">
<div class="input-group-append">
<check-archive type="pdf"></check-archive>
</div>
</div>
@error('node_bin')
<span class="invalid-feedback" role="alert">{{ $message }}</span>
@enderror
Expand All @@ -150,7 +155,12 @@

<div class="form-group">
<label for="youtube_dl_bin">{{ __('Youtube-dl binary') }}</label>
<input type="text" class="form-control {{ $errors->has('youtube_dl_bin') ? ' is-invalid' : '' }}" name="youtube_dl_bin" id="youtube_dl_bin" value="{{ $settings['youtube_dl_bin'] }}">
<div class="input-group">
<input type="text" class="form-control {{ $errors->has('youtube_dl_bin') ? ' is-invalid' : '' }}" name="youtube_dl_bin" id="youtube_dl_bin" value="{{ $settings['youtube_dl_bin'] }}">
<div class="input-group-append">
<check-archive type="media"></check-archive>
</div>
</div>
@error('youtube_dl_bin')
<span class="invalid-feedback" role="alert">{{ $message }}</span>
@enderror
Expand Down
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
$router->delete('tags/{tag}', 'TagsController@delete')->name('tags.delete');
$router->post('tags/{from}/move/{to}', 'TagsController@move')->name('tags.move');

$router->get('archive/check/{type}', 'ArchiveController@check');

$router->get('users', 'UsersController@all')->name('users.all');
$router->post('users', 'UsersController@store')->name('users.store');
$router->get('users/{id}', 'UsersController@get')->name('users.get');
Expand Down

0 comments on commit 809b33f

Please sign in to comment.