Use Requests library to get help from Github.

This commit is contained in:
James Cole 2016-02-23 16:12:59 +01:00
parent f2d06bcea1
commit 28a4f724d5
5 changed files with 45 additions and 37 deletions

View File

@ -5,6 +5,7 @@ namespace FireflyIII\Helpers\Csv;
use Crypt;
use League\Csv\Reader;
use Session;
use Storage;
/**
* Class Data
@ -253,7 +254,8 @@ class Data
protected function loadCsvFile()
{
$file = $this->getCsvFileLocation();
$content = file_get_contents($file);
$disk = Storage::disk('upload');
$content = $disk->get($file);
$contentDecrypted = Crypt::decrypt($content);
$this->setCsvFileContent($contentDecrypted);
}

View File

@ -3,9 +3,9 @@ declare(strict_types = 1);
namespace FireflyIII\Helpers\Help;
use Cache;
use ErrorException;
use League\CommonMark\CommonMarkConverter;
use Log;
use Requests;
use Route;
/**
@ -23,34 +23,42 @@ class Help implements HelpInterface
*
* @return string
*/
public function getFromCache(string $key)
public function getFromCache(string $key): string
{
return Cache::get($key);
}
/**
* @codeCoverageIgnore
*
* @param string $language
* @param string $route
*
* @return array
*/
public function getFromGithub(string $route)
public function getFromGithub(string $language, string $route): array
{
$uri = 'https://raw.githubusercontent.com/JC5/firefly-iii-help/master/en/' . e($route) . '.md';
$uri = sprintf('https://raw.githubusercontent.com/JC5/firefly-iii-help/master/%s/%s.md', $language, $route);
$routeIndex = str_replace('.', '-', $route);
$title = trans('help.' . $routeIndex);
$content = [
'text' => '<p>There is no help for this route!</p>',
'text' => '<p>There is no help for this route, or there is no help available in your language.</p>',
'title' => $title,
];
try {
$content['text'] = file_get_contents($uri);
} catch (ErrorException $e) {
Log::error(trim($e->getMessage()));
Log::debug('Going to get from Github: ' . $uri);
$result = Requests::get($uri);
Log::debug('Status code was ' . $result->status_code . '.');
if ($result->status_code === 200) {
$content['text'] = $result->body;
}
if (strlen(trim($content['text'])) == 0) {
$content['text'] = '<p>There is no help for this route.</p>';
Log::debug('No actual help text for this route (even though a page was found).');
$content['text'] = '<p>There is no help for this route, or there is no help available in your language.</p>';
}
$converter = new CommonMarkConverter();
$content['text'] = $converter->convertToHtml($content['text']);
@ -66,7 +74,7 @@ class Help implements HelpInterface
*
* @return bool
*/
public function hasRoute(string $route)
public function hasRoute(string $route):bool
{
return Route::has($route);
}
@ -78,7 +86,7 @@ class Help implements HelpInterface
*
* @return bool
*/
public function inCache(string $route)
public function inCache(string $route):bool
{
return Cache::has('help.' . $route . '.title') && Cache::has('help.' . $route . '.text');
}

View File

@ -15,34 +15,32 @@ interface HelpInterface
*
* @return string
*/
public function getFromCache(string $key);
public function getFromCache(string $key): string;
/**
* @param string $route
*
* @return array
*/
public function getFromGithub(string $route);
public function getFromGithub(string $language, string $route):array;
/**
* @param string $route
*
* @return bool
*/
public function hasRoute(string $route);
public function hasRoute(string $route): bool;
/**
* @param string $route
*
* @return bool
*/
public function inCache(string $route);
public function inCache(string $route): bool;
/**
* @param string $route
* @param array $content
*
* @return void
*/
public function putInCache(string $route, array $content);
}

View File

@ -14,7 +14,6 @@ use Illuminate\Http\Request;
use Input;
use Log;
use Preferences;
use Request as RequestFacade;
use Session;
use View;
@ -127,16 +126,15 @@ class CsvController extends Controller
$result = json_encode($data, JSON_PRETTY_PRINT);
$name = sprintf('"%s"', addcslashes('csv-configuration-' . date('Y-m-d') . '.json', '"\\'));
RequestFacade::header('Content-disposition: attachment; filename=' . $name);
RequestFacade::header('Content-Type: application/json');
RequestFacade::header('Content-Description: File Transfer');
RequestFacade::header('Connection: Keep-Alive');
RequestFacade::header('Expires: 0');
RequestFacade::header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
RequestFacade::header('Pragma: public');
RequestFacade::header('Content-Length: ' . strlen($result));
return $result;
return response($result, 200)
->header('Content-disposition', 'attachment; filename=' . $name)
->header('Content-Type', 'application/json')
->header('Content-Description', 'File Transfer')
->header('Connection', 'Keep-Alive')
->header('Expires', '0')
->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
->header('Pragma', 'public')
->header('Content-Length', strlen($result));
}
/**
@ -220,8 +218,8 @@ class CsvController extends Controller
// process given roles and mapping:
$inputMap = Input::get('map') ?? [];
$roles = $this->wizard->processSelectedRoles(Input::get('role'));
$maps = $this->wizard->processSelectedMapping($roles, $inputMap);
$roles = $this->wizard->processSelectedRoles(Input::get('role'));
$maps = $this->wizard->processSelectedMapping($roles, $inputMap);
Session::put('csv-map', $maps);
Session::put('csv-roles', $roles);
@ -398,7 +396,7 @@ class CsvController extends Controller
return redirect(route('csv.index'));
}
$fullPath = $this->wizard->storeCsvFile($request->file('csv')->getRealPath());
$path = $this->wizard->storeCsvFile($request->file('csv')->getRealPath());
$settings = [];
$settings['date-format'] = Input::get('date_format');
$settings['has-headers'] = intval(Input::get('has_headers')) === 1;
@ -424,7 +422,7 @@ class CsvController extends Controller
}
}
$this->data->setCsvFileLocation($fullPath);
$this->data->setCsvFileLocation($path);
$this->data->setDateFormat($settings['date-format']);
$this->data->setHasHeaders($settings['has-headers']);
$this->data->setMap($settings['map']);

View File

@ -2,6 +2,7 @@
use FireflyIII\Helpers\Help\HelpInterface;
use Log;
use Preferences;
use Response;
/**
@ -46,7 +47,8 @@ class HelpController extends Controller
return Response::json($content);
}
$content = $help->getFromGithub($route);
$language = Preferences::get('language', env('DEFAULT_LANGUAGE', 'en_US'))->data;
$content = $help->getFromGithub($language, $route);
$help->putInCache($route, $content);