1
0
mirror of https://github.com/devfake/flox.git synced 2024-11-14 22:22:39 +01:00

import and export alternative titles (#35)

* create helper functions

* import/export alternative titles

* add test for import and export
This commit is contained in:
Viktor Geringer 2017-02-01 13:27:50 +01:00 committed by Tim Meier
parent d74c8cbe05
commit 06b41cb3a9
10 changed files with 127 additions and 51 deletions

View File

@ -2,8 +2,8 @@
namespace App\Http\Controllers;
use App\AlternativeTitle;
use App\Episode;
use App\Http\Requests\ImportRequest;
use App\Item;
use App\Services\FileParser;
use App\Services\Storage;
@ -13,6 +13,7 @@
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Symfony\Component\HttpFoundation\Response;
class SettingController {
@ -21,11 +22,13 @@
private $storage;
private $version;
private $setting;
private $alternativeTitles;
public function __construct(Item $item, Episode $episodes, Storage $storage, Setting $setting)
public function __construct(Item $item, Episode $episodes, AlternativeTitle $alternativeTitles, Storage $storage, Setting $setting)
{
$this->item = $item;
$this->episodes = $episodes;
$this->alternativeTitles = $alternativeTitles;
$this->storage = $storage;
$this->setting = $setting;
$this->version = config('app.version');
@ -40,42 +43,54 @@
{
$data['items'] = $this->item->all();
$data['episodes'] = $this->episodes->all();
$data['alternative_titles'] = $this->alternativeTitles->all();
$file = 'flox--' . date('Y-m-d---H-i') . '.json';
$filename = $this->storage->createExportFilename();
$this->storage->saveExport($file, json_encode($data));
$this->storage->saveExport($filename, json_encode($data));
return response()->download(base_path('../public/exports/' . $file));
return response()->download(base_path('../public/exports/' . $filename));
}
/**
* Reset item table and restore backup. Download every poster image new.
*
* @param ImportRequest $request
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
*/
public function import(ImportRequest $request)
public function import()
{
set_time_limit(300);
increaseTimeLimit();
$file = Input::file('import');
$file = $request->file('import');
$extension = $file->getClientOriginalExtension();
if($extension !== 'json') {
return response('Wrong File', 422);
return response('This is not a flox backup file.', Response::HTTP_UNPROCESSABLE_ENTITY);
}
$data = json_decode(file_get_contents($file));
$this->item->truncate();
foreach($data->items as $item) {
$this->item->create((array) $item);
$this->storage->downloadPoster($item->poster);
if(isset($data->items)) {
$this->item->truncate();
foreach($data->items as $item) {
$this->item->create((array) $item);
$this->storage->downloadPoster($item->poster);
}
}
$this->episodes->truncate();
foreach($data->episodes as $episode) {
$this->episodes->create((array) $episode);
if(isset($data->episodes)) {
$this->episodes->truncate();
foreach($data->episodes as $episode) {
$this->episodes->create((array) $episode);
}
}
if(isset($data->alternative_titles)) {
$this->alternativeTitles->truncate();
foreach($data->alternative_titles as $title) {
$this->alternativeTitles->create((array) $title);
}
}
}
@ -101,7 +116,7 @@
*/
public function updateGenre(TMDB $tmdb)
{
set_time_limit(3000);
increaseTimeLimit();
$items = $this->item->all();
@ -178,6 +193,8 @@
*/
public function fetchFiles(FileParser $parser)
{
increaseTimeLimit();
$files = $parser->fetch();
return $parser->updateDatabase($files);

View File

View File

@ -1,31 +0,0 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
class ImportRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return Auth::check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'import' => 'file'
];
}
}

View File

@ -17,6 +17,16 @@
LaravelStorage::disk('export')->put($file, $items);
}
/**
* Create the export filename.
*
* @return string
*/
public function createExportFilename()
{
return 'flox--' . date('Y-m-d---H-i') . '.json';
}
/**
* Download the poster image file.
*

8
backend/app/helpers.php Normal file
View File

@ -0,0 +1,8 @@
<?php
const FIVE_MINUTES = 300;
function increaseTimeLimit()
{
set_time_limit(FIVE_MINUTES);
}

View File

@ -25,7 +25,10 @@
],
"psr-4": {
"App\\": "app/"
}
},
"files": [
"app/helpers.php"
]
},
"autoload-dev": {
"classmap": [

View File

@ -1,12 +1,16 @@
<?php
use App\AlternativeTitle;
use App\Episode;
use App\Item;
use App\Services\Storage;
use App\Setting;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Http\UploadedFile;
class SettingTest extends TestCase {
@ -101,6 +105,68 @@
$this->assertNotEmpty($withGenre->genre);
}
/** @test */
public function it_should_export_a_backup_file()
{
$filename = 'flox-export-test.json';
$path = base_path('../public/exports/' . $filename);
$this->removeExportFile($path);
$this->createTv();
$this->createMovie();
$storage = Mockery::mock(app(Storage::class))->makePartial();
$storage->shouldReceive('createExportFilename')->once()->andReturn($filename);
$this->app->instance(Storage::class, $storage);
$this->actingAs($this->user)->json('GET', 'api/export')->assertResponseStatus(200);
$file = (array) json_decode(file_get_contents($path));
$this->assertArrayHasKey('items', $file);
$this->assertArrayHasKey('episodes', $file);
$this->assertArrayHasKey('alternative_titles', $file);
$this->assertCount(2, $file['items']);
$this->assertCount(4, $file['episodes']);
$this->assertCount(0, $file['alternative_titles']);
$this->removeExportFile($path);
}
/** @test */
public function it_should_import_a_backup_file()
{
$this->callImport('export.json');
$this->assertCount(5, Item::all());
$this->assertCount(143, Episode::all());
$this->assertCount(25, AlternativeTitle::all());
}
/** @test */
public function it_should_abort_import_if_not_json()
{
$this->callImport('wrong-file.txt');
$this->seeStatusCode(422);
}
private function callImport($filename)
{
$path = __DIR__ . '/../fixtures/flox/' . $filename;
$file = new UploadedFile($path, $filename);
$this->actingAs($this->user)->json('POST', 'api/import', ['import' => $file]);
}
private function removeExportFile($path)
{
if(file_exists($path)) {
unlink($path);
}
}
private function createGuzzleMock($fixture)
{
$mock = new MockHandler([

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1 @@
test

View File

@ -55,12 +55,13 @@
if(confirm) {
this.SET_LOADING(true);
http.post(`${config.api}/import`, this.uploadedFile).then(() => {
this.SET_LOADING(false);
this.uploadSuccess = true;
}, error => {
this.SET_LOADING(false);
alert('Error: ' + error.data);
alert('Error: ' + error.response.data);
});
}
}