Skip to content
This repository was archived by the owner on Jan 18, 2018. It is now read-only.

Commit 2164cbb

Browse files
Added API
0 parents  commit 2164cbb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+5021
-0
lines changed

.env.example

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
APP_ENV=production
2+
APP_DEBUG=false
3+
APP_KEY=SomeRandomKey
4+
5+
CACHE_DRIVER=array
6+
SESSION_DRIVER=array
7+
QUEUE_DRIVER=sync
8+
9+
CAT_GEN=path
10+
DOGE_GEN=path

.gitattributes

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
* text=auto
2+
3+
/tests export-ignore
4+
/.gitattributes export-ignore
5+
/.gitignore export-ignore
6+
/.travis.yml export-ignore
7+
/phpunit.xml.dist export-ignore
8+
/README.md export-ignore

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.env
2+
phpunit.xml
3+
vendor

.travis.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: php
2+
3+
php:
4+
- 7.0
5+
6+
sudo: false
7+
8+
install: travis_retry composer install --no-interaction --prefer-source
9+
10+
script: vendor/bin/phpunit

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Graham Campbell <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# SERVER

app/AppServiceProvider.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App;
6+
7+
use App\Generators\CatGenerator;
8+
use App\Generators\DogeGenerator;
9+
use App\Generators\ProcessRunner;
10+
use Illuminate\Contracts\Container\Container;
11+
use Illuminate\Support\ServiceProvider;
12+
13+
/**
14+
* This is the app service provider.
15+
*
16+
* @author Graham Campbell <[email protected]>
17+
*/
18+
class AppServiceProvider extends ServiceProvider
19+
{
20+
/**
21+
* Register the service provider.
22+
*
23+
* @return void
24+
*/
25+
public function register()
26+
{
27+
$this->app->singleton(ProcessRunner::class, function () {
28+
return new ProcessRunner();
29+
});
30+
31+
$this->app->singleton(CatGenerator::class, function (Container $app) {
32+
return new CatGenerator(
33+
$app->make(ProcessRunner::class),
34+
$app->config->get('services.meme.cat'),
35+
$app->basePath('resources/img'),
36+
$app->basePath('public/result')
37+
);
38+
});
39+
40+
$this->app->singleton(DogeGenerator::class, function (Container $app) {
41+
return new DogeGenerator(
42+
$app->make(ProcessRunner::class),
43+
$app->config->get('services.meme.doge'),
44+
$app->basePath('public/result')
45+
);
46+
});
47+
48+
$this->app->get('/', 'App\Controllers\MainController@show');
49+
50+
$this->app->post('cat', 'App\Controllers\MainController@cat');
51+
52+
$this->app->post('dodge', 'App\Controllers\MainController@doge');
53+
}
54+
}

app/Controllers/MainController.php

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Controllers;
6+
7+
use App\Generators\CatGenerator;
8+
use App\Generators\DogeGenerator;
9+
use App\Generators\GeneratorInterface;
10+
use App\Generators\MultiGenerator;
11+
use App\Generators\ValidatingGenerator;
12+
use Illuminate\Contracts\Container\Container;
13+
use Illuminate\Contracts\View\Factory;
14+
use Illuminate\Http\JsonResponse;
15+
use Illuminate\Http\Request;
16+
use Illuminate\Http\Response;
17+
use Laravel\Lumen\Routing\Controller;
18+
19+
/**
20+
* This is the main controller class.
21+
*
22+
* @author Graham Campbell <[email protected]>
23+
*/
24+
class MainController extends Controller
25+
{
26+
/**
27+
* Show the welcome message.
28+
*
29+
* @return \Illuminate\Http\JsonResponse
30+
*/
31+
public function show()
32+
{
33+
return new JsonResponse([
34+
'meta' => ['message' => 'Very image. Much server.'],
35+
]);
36+
}
37+
38+
/**
39+
* Generate cat memes.
40+
*
41+
* @param \Illuminate\Contracts\Container\Container $container
42+
* @param \Illuminate\Http\Request $request
43+
*
44+
* @return \Illuminate\Http\JsonResponse
45+
*/
46+
public function cat(Container $container, Request $request)
47+
{
48+
$inner = $container->make(CatGenerator::class);
49+
$text = $request->get('text');
50+
$quantity = $request->get('quantity', 1);
51+
52+
return $this->generate($inner, (string) $text, (int) $quantity);
53+
}
54+
55+
/**
56+
* Generate dodge memes.
57+
*
58+
* @param \Illuminate\Contracts\Container\Container $container
59+
* @param \Illuminate\Http\Request $request
60+
*
61+
* @return \Illuminate\Http\JsonResponse
62+
*/
63+
public function dodge(Container $container, Request $request)
64+
{
65+
$inner = $container->make(DogeGenerator::class);
66+
$text = $request->get('text');
67+
$quantity = $request->get('quantity', 1);
68+
69+
return $this->generate($inner, (string) $text, (int) $quantity);
70+
}
71+
72+
/**
73+
* Generate the memes.
74+
*
75+
* @param \App\Generators\GeneratorInterface $inner
76+
* @param string $text
77+
* @param int $quantity
78+
*
79+
* @return \Illuminate\Http\JsonResponse
80+
*/
81+
protected function generate(GeneratorInterface $inner, string $text, int $quantity = 1)
82+
{
83+
$generator = new ValidatingGenerator(new MultiGenerator($inner, $quantity));
84+
85+
$images = [];
86+
87+
foreach ($generator->start($text)->wait() as $image) {
88+
$images[] = "https://api.memetrash.co.uk/result/{$image}.jpg";
89+
}
90+
91+
return new JsonResponse([
92+
'success' => ['message' => 'Here are your memes!'],
93+
'data' => ['images' => $images],
94+
]);
95+
}
96+
}

app/Generators/CatGenerator.php

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Generators;
6+
7+
/**
8+
* This is the cat meme generator class.
9+
*
10+
* @author Graham Campbell <[email protected]>
11+
*/
12+
class CatGenerator implements GeneratorInterface
13+
{
14+
/**
15+
* The runner instance.
16+
*
17+
* @var \App\Generator\ProcessRunner
18+
*/
19+
protected $runner;
20+
21+
/**
22+
* The generator path.
23+
*
24+
* @var string
25+
*/
26+
protected $generator;
27+
28+
/**
29+
* The resources path.
30+
*
31+
* @var string
32+
*/
33+
protected $resources;
34+
35+
/**
36+
* The output path.
37+
*
38+
* @var string
39+
*/
40+
protected $output;
41+
42+
/**
43+
* Create a new cat meme generator instance.
44+
*
45+
* @param \App\Generator\ProcessRunner $runner
46+
* @param string $generator
47+
* @param string $resources
48+
* @param string $output
49+
*
50+
* @return void
51+
*/
52+
public function __construct(ProcessRunner $runner, string $generator, string $resources, string $output)
53+
{
54+
$this->runner = $runner;
55+
$this->generator = $generator;
56+
$this->resources = $resources;
57+
$this->output = $output;
58+
}
59+
60+
/**
61+
* Start the meme generation.
62+
*
63+
* @param string $text
64+
*
65+
* @throws \App\Generators\ExceptionInterface
66+
*
67+
* @return \App\Generators\Promise
68+
*/
69+
public function start(string $text)
70+
{
71+
$name = str_random(16);
72+
$image = random_int(1, 70);
73+
74+
$command = "python {$this->generator}/run.py \"{$this->resources}/{$image}.jpg\" \"{$this->output}/{$name}.jpg\" \"{$this->generator}/resources\" \"{$text}\"";
75+
76+
$process = $this->runner->start($command);
77+
78+
return new Promise(function () use ($process, $name) {
79+
$process->wait();
80+
81+
return [$name];
82+
});
83+
}
84+
}

app/Generators/DogeGenerator.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Generators;
6+
7+
/**
8+
* This is the doge meme generator class.
9+
*
10+
* @author Graham Campbell <[email protected]>
11+
*/
12+
class DogeGenerator implements GeneratorInterface
13+
{
14+
/**
15+
* The runner instance.
16+
*
17+
* @var \App\Generator\ProcessRunner
18+
*/
19+
protected $runner;
20+
21+
/**
22+
* The generator path.
23+
*
24+
* @var string
25+
*/
26+
protected $generator;
27+
28+
/**
29+
* The output path.
30+
*
31+
* @var string
32+
*/
33+
protected $output;
34+
35+
/**
36+
* Create a new doge meme generator instance.
37+
*
38+
* @param \App\Generator\ProcessRunner $runner
39+
* @param string $generator
40+
* @param string $output
41+
*
42+
* @return void
43+
*/
44+
public function __construct(ProcessRunner $runner, string $generator, string $output)
45+
{
46+
$this->runner = $runner;
47+
$this->generator = $generator;
48+
$this->output = $output;
49+
}
50+
51+
/**
52+
* Start the meme generation.
53+
*
54+
* @param string $text
55+
*
56+
* @throws \App\Generators\ExceptionInterface
57+
*
58+
* @return \App\Generators\Promise
59+
*/
60+
public function start(string $text)
61+
{
62+
$name = str_random(16);
63+
64+
$command = "python {$this->generator}/run.py \"{$text}\" \"{$this->output}/{$name}.jpg\" \"{$this->generator}/resources\" 6";
65+
66+
$process = $this->runner->start($command);
67+
68+
return new Promise(function () use ($process, $name) {
69+
$process->wait();
70+
71+
return [$name];
72+
});
73+
}
74+
}

app/Generators/ExceptionInterface.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Generators;
6+
7+
/**
8+
* This is the generator exception interface.
9+
*
10+
* @author Graham Campbell <[email protected]>
11+
*/
12+
interface ExceptionInterface
13+
{
14+
//
15+
}

0 commit comments

Comments
 (0)