Skip to content

Commit

Permalink
Merge pull request #44 from Kovah/dev
Browse files Browse the repository at this point in the history
v0.0.12
  • Loading branch information
Kovah authored Mar 24, 2019
2 parents 4428ab1 + 9013bd8 commit abf2fb5
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 86 deletions.
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
branches:
only:
- master

language: php
php:
- '7.1.3'
- '7.2'
- '7.3'

install:
- cp .env.example .env
- composer install
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<p align="center">
<a href="https://hub.docker.com/r/linkace/linkace"><img src="https://img.shields.io/badge/Docker-linkace%2Flinkace-2596EC.svg" alt="Docker Repository"></a>
<a href="https://github.com/Kovah/LinkAce/releases"><img src="https://img.shields.io/github/release/kovah/linkace.svg" alt="Latest Release"></a>
<a href="https://travis-ci.org/Kovah/LinkAce"><img src="https://img.shields.io/travis/kovah/linkace.svg" alt="Build Status"></a>
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/github/license/kovah/linkace.svg" alt="License"></a>
</p>

Expand Down
63 changes: 63 additions & 0 deletions app/Helper/WaybackMachine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Helper;

use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;

/**
* Class WaybackMachine
*
* @package App\Helper
*/
class WaybackMachine
{
/** @var string */
public static $base_url = 'https://web.archive.org';

/**
* Save an URL to the Wayback Machine
*
* @param string $url
* @return bool
*/
public static function saveToArchive(string $url): bool
{
if (!filter_var($url, FILTER_VALIDATE_URL)) {
// Abort if provided string is not an URL
return false;
}

$archive_url = self::$base_url . '/save/' . $url;

try {

$client = new Client();
$client->request('GET', $archive_url, [
'http_errors' => false,
]);

} catch (\GuzzleHttp\Exception\GuzzleException $e) {
Log::warning($e);
return false;
}

return true;
}

/**
* Get the link to the Wayback Machine archive for a specific URL
*
* @param string $url
* @return null|string
*/
public static function getArchiveLink(string $url): ?string
{
if (!filter_var($url, FILTER_VALIDATE_URL)) {
// Abort if provided string is not an URL
return null;
}

return self::$base_url . '/web/*/' . $url;
}
}
24 changes: 20 additions & 4 deletions app/Helper/functions.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

use App\Helper\WaybackMachine;
use App\Models\Link;
use App\Models\Setting;
use Illuminate\Support\Facades\Cache;

/**
* Shorthand for the current user settings
Expand Down Expand Up @@ -53,7 +56,7 @@ function systemsettings(string $key = '')
* @param bool $use_relational
* @return string
*/
function formatDateTime(\Carbon\Carbon $date, bool $use_relational = false)
function formatDateTime(\Carbon\Carbon $date, bool $use_relational = false): string
{
$timezone = config('app.timezone');

Expand All @@ -77,7 +80,7 @@ function formatDateTime(\Carbon\Carbon $date, bool $use_relational = false)
/**
* Get the correct pagination limit
*
* @return \Illuminate\Config\Repository|mixed
* @return mixed
*/
function getPaginationLimit()
{
Expand All @@ -96,7 +99,7 @@ function getPaginationLimit()
* @param \App\Models\Link $link
* @return string
*/
function getShareLinks(\App\Models\Link $link)
function getShareLinks(\App\Models\Link $link): string
{
$cache_key = 'sharelinks_link_' . $link->id . (auth()->guest() ? '_guest' : '');
$cache_duration = config('linkace.default.cache_duration');
Expand Down Expand Up @@ -148,7 +151,7 @@ function displaySVG($path, $width = null, $height = null)
* @param string $order_dir
* @return string
*/
function tableSorter($label, $route, $type, $order_by, $order_dir)
function tableSorter($label, $route, $type, $order_by, $order_dir): string
{
$out = '<div class="d-flex">';
$out .= '<span class="mr-1">' . e($label) . '</span>';
Expand Down Expand Up @@ -177,3 +180,16 @@ function tableSorter($label, $route, $type, $order_by, $order_dir)

return $out;
}

/**
* Get the Wayback Machine link for an URL
*
* @param string|Link $link
* @return null|string
*/
function waybackLink($link): ?string
{
$link = $link->url ?? $link;

return WaybackMachine::getArchiveLink($link);
}
5 changes: 5 additions & 0 deletions app/Http/Controllers/Models/LinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Helper\LinkAce;
use App\Helper\LinkIconMapper;
use App\Helper\WaybackMachine;
use App\Http\Controllers\Controller;
use App\Http\Requests\LinkDeleteRequest;
use App\Http\Requests\LinkStoreRequest;
Expand Down Expand Up @@ -100,6 +101,10 @@ public function store(LinkStoreRequest $request)

alert(trans('link.added_successfully'), 'success');

// Notify the Wayback Machine about the link
WaybackMachine::saveToArchive($link->url);

// Redirect to the corresponding page based on bookmarklet usage
$is_bookmarklet = session('bookmarklet.create');

if ($request->get('reload_view')) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "linkace",
"version": "0.0.11",
"version": "0.0.12",
"description": "A small, selfhosted bookmark manager with advanced features, built with Laravel and Docker",
"homepage": "https://github.com/Kovah/LinkAce",
"repository": {
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/link.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
'author' => 'by :user',

'external_link' => 'External Link',
'wayback' => 'Link archive @ Wayback Machine',

'added_successfully' => 'Link added successfully.',
'updated_successfully' => 'Link updated successfully.',
Expand Down
8 changes: 8 additions & 0 deletions resources/views/models/links/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ class="btn btn-sm btn-outline-danger" aria-label="@lang('link.delete')">
<input type="hidden" name="link_id" value="{{ $link->id }}">
</form>

@if($link->status !== 1)
<div class="mb-3">
<a href="{{ waybackLink($link) }}" class="btn btn-sm btn-block btn-outline-warning" target="_blank">
@lang('link.wayback')
</a>
</div>
@endif

<div class="card mb-3">
<div class="card-header">
@lang('category.category')
Expand Down
80 changes: 0 additions & 80 deletions tests/Feature/CategoryCreationTest.php

This file was deleted.

64 changes: 64 additions & 0 deletions tests/Unit/Helper/HelperFunctionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Tests\Unit\Helper;

use App\Models\Link;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;

/**
* Class HelperFunctionsTest
*
* @package Tests\Unit\Helper
*/
class HelperFunctionsTest extends TestCase
{
use DatabaseMigrations;
use DatabaseTransactions;

/** @var User */
private $user;

/** @var Link */
private $link;

public function setUp()
{
parent::setUp();

$this->user = factory(User::class)->create();
$this->link = factory(Link::class)->create();
}

/**
* Test the saveToArchive() helper funtion with a valid URL.
* Should return true.
*
* @return void
*/
public function testValidWaybackLink(): void
{
$expected = 'https://web.archive.org/web/*/' . $this->link->url;

$link = waybackLink($this->link);

$this->assertEquals($expected, $link);
}

/**
* Test the saveToArchive() helper funtion with an invalid URL.
* Will return false.
*
* @return void
*/
public function testInvalidWaybackLink(): void
{
$url = 'not an URL';

$link = waybackLink($url);

$this->assertNull($link);
}
}
File renamed without changes.
Loading

0 comments on commit abf2fb5

Please sign in to comment.