Skip to content

Commit

Permalink
release: 0.17.2 Characinae Build Pack 2
Browse files Browse the repository at this point in the history
release: 0.17.2 Characinae Build Pack 2
  • Loading branch information
ZsgsDesign committed Oct 21, 2021
2 parents 3dac84e + e0cc1c9 commit 29c4275
Show file tree
Hide file tree
Showing 32 changed files with 1,638 additions and 612 deletions.
26 changes: 26 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,32 @@

All notable changes to this project will be documented in this file.

## NOJ 0.17.2 Characinae Build Pack 2 - 2021-10-21
This is a build version update for `0.17.0 Characinae`.

**Important:** Rerun `npm ci` and `composer install` then `npm run production`.

### Update Logs
* **Compatibility:** This update no longer uses wkhtmltopdf.
* **New:** NOJ now greatly improves PDF generation speed and quality by using 2 new approaches: CPDF or Blink with Skia.
* **New:** NOJ now uses `nesk/puphpeteer`.
* **New:** NOJ now uses `barryvdh/laravel-dompdf`.
* **Deprecated:** NOJ no longer uses old `LatexModel` and `LatexController`.
* **Deprecated:** Helper function `latex2image` is no longer supported.
* **Fixed:** A bug causing PDF-generated Chinese character replaced by blank square.
* **Fixed:** A bug causing PDF generation exit without error.
* **Fixed:** Typo (only 1 this time).
* **Improved:** PDF generation now does not wait 20 seconds then proceed, it quits when complete.
* **Security:** `fonts-asset/dejavu` is now at `1.0.4`.
* **Security:** `fonts-asset/simsun` is now at `1.0.2`.
* **Security:** `graham-campbell/result-type` is now at `1.0.3`.
* **Security:** `laravel/framework` is now at `8.65.0`.
* **Security:** `laravel/passport` is now at `10.1.4`.
* **Security:** `league/oauth2-server` is now at `8.3.3`.
* **Security:** `mews/purifier` is now at `3.3.6`.
* **Security:** `phpdocumentor/reflection-docblock` is now at `5.3.0`.
* **Security:** `swiftmailer/swiftmailer` is now at `6.3.0`.

## NOJ 0.17.1 Characinae Build Pack 1 - 2021-10-12
This is a build version update for `0.17.0 Characinae`.

Expand Down
20 changes: 0 additions & 20 deletions app/Helpers/functions.php
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Support\Facades\DB;
use GrahamCampbell\Markdown\Facades\Markdown;
use App\Models\Eloquent\Message;
use App\Models\Latex\LatexModel;
use App\Models\Eloquent\Tool\Theme;
use App\Models\Eloquent\Tool\AppSettings;

Expand Down Expand Up @@ -190,25 +189,6 @@ function formatProblemSolvedTime($seconds)
}
}

if (!function_exists('latex2Image')) {
function latex2Image($content)
{
$callback=function($matches) use (&$patch, &$display) {
[$url, $width, $height]=LatexModel::info("$patch$matches[1]$patch");
return "<img src=\"$url\" style=\"display: $display;\" class=\"rendered-tex\" width=\"$width\" height=\"$height\">";
};
$patch='$';
$display='inline-block';
$content=preg_replace_callback('/\\$\\$\\$(.*?)\\$\\$\\$/', $callback, $content);
$content=preg_replace_callback('/\\\\\\((.*?)\\\\\\)/', $callback, $content);
$patch='$$';
$display='block';
$content=preg_replace_callback('/\\$\\$(.*?)\\$\\$/', $callback, $content);
$content=preg_replace_callback('/\\\\\\[(.*?)\\\\\\]/', $callback, $content);
return $content;
}
}

if (!function_exists('vscodeLocale')) {
function vscodeLocale()
{
Expand Down
19 changes: 18 additions & 1 deletion app/Http/Controllers/Ajax/ContestAdminController.php
Expand Up @@ -338,11 +338,28 @@ public function generatePDF(Request $request)
"cid"=>"required|integer",
"config.cover"=>"required",
"config.advice"=>"required",
"config.renderer"=>"required|string",
"config.formula"=>"required|string",
]);
$cid=$request->input('cid');
$renderer = $request->input('config.renderer');
$formula = $request->input('config.formula');
if($renderer == 'blink') {
if($formula != 'tex') {
return ResponseModel::err(4011, 'Illegal Formula Rendering Option.');
}
} else if ($renderer == 'cpdf') {
if($formula != 'svg' && $formula != 'png') {
return ResponseModel::err(4011, 'Illegal Formula Rendering Option.');
}
} else {
return ResponseModel::err(4011, 'Unknown Render Engine.');
}
$config=[
'cover'=>$request->input('config.cover')=='true',
'advice'=>$request->input('config.advice')=='true'
'advice'=>$request->input('config.advice')=='true',
'renderer'=>$renderer,
'formula'=>$formula,
];
$contestModel=new ContestModel();
if ($contestModel->judgeClearance($cid, Auth::user()->id)!=3) {
Expand Down
15 changes: 15 additions & 0 deletions app/Http/Controllers/Contest/AdminController.php
Expand Up @@ -131,4 +131,19 @@ public function scrollBoard($cid) {
'basic_info' => $basicInfo,
]);
}

public function pdfView($cid) {
$record = Contest::find($cid);
$accessConfig = request()->accessConfig;
return view('pdf.contest.main', [
'conf' => $accessConfig,
'contest' => [
'cid' => $cid,
'name' => $record->name,
'shortName' => $record->name,
'date' => date("F j, Y", strtotime($record->begin_time)),
],
'problemset' => $record->getProblemSet(),
]);
}
}
71 changes: 0 additions & 71 deletions app/Http/Controllers/LatexController.php

This file was deleted.

1 change: 1 addition & 0 deletions app/Http/Kernel.php
Expand Up @@ -67,6 +67,7 @@ class Kernel extends HttpKernel
'group.banned' => \App\Http\Middleware\Group\Banned::class,

'contest.desktop' => \App\Http\Middleware\Contest\IsDesktop::class,
'contest.board.admin.pdfview.clearance' => \App\Http\Middleware\Contest\Board\Admin\PDFView\Clearance::class,

'user.banned' => \App\Http\Middleware\User\Banned::class,

Expand Down
34 changes: 34 additions & 0 deletions app/Http/Middleware/Contest/Board/Admin/PDFView/Clearance.php
@@ -0,0 +1,34 @@
<?php

namespace App\Http\Middleware\Contest\Board\Admin\PDFView;

use Closure;
use Cache;
use App\Models\Eloquent\Contest;

class Clearance
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$contest=Contest::find($request->cid);
if(blank($contest)) {
return abort(403, 'Contest Does Not Exist');
}
$accessConfig = Cache::tags(['contest', 'pdfViewAccess', $request->cid])->get($request->accessToken);
if(blank($accessConfig)) {
return abort(403, 'Access Token Expired');
}
Cache::tags(['contest', 'pdfViewAccess', $request->cid])->forget($request->accessToken);
$request->merge([
'accessConfig' => $accessConfig
]);
return $next($request);
}
}
121 changes: 69 additions & 52 deletions app/Jobs/GeneratePDF.php
Expand Up @@ -9,9 +9,12 @@
use Illuminate\Foundation\Bus\Dispatchable;
use App\Models\Eloquent\Contest;
use Imtigger\LaravelJobStatus\Trackable;
use PDF;
use Nesk\Puphpeteer\Puppeteer;
use Nesk\Rialto\Data\JsFunction;
use Cache;
use Exception;
use Str;
use Storage;
use PDF;

class GeneratePDF implements ShouldQueue
{
Expand Down Expand Up @@ -47,74 +50,88 @@ public function handle()
{
$cid = $this->cid;
$config = $this->config;
$accessToken = Str::random(32);

Cache::tags(['contest', 'pdfViewAccess', $cid])->put($accessToken, $config);

if (!is_dir(storage_path("app/contest/pdf/"))) {
mkdir(storage_path("app/contest/pdf/"), 0777, true);
}

$record = Contest::find($cid);

// $headerPath = 'headers/'.Str::random(32).".html";

// Storage::disk("temp")->put('headers/'.Str::random(32).".html", "Generated by NOJ - https://github.com/ZsgsDesign/NOJ");

$pdfContest = PDF::setOptions([
// 'footer-html' => storage_path("temp/$headerPath"),
'header-left' => "Generated by NOJ - https://github.com/ZsgsDesign/NOJ",
'header-line' => true,
'footer-left' => $record->name,
'footer-right' => "Page [page]",
'footer-font-size' => 10,
'header-font-size' => 10,
'margin-bottom' => 24,
'margin-left' => 12,
'margin-right' => 12,
'margin-top' => 24,
'header-spacing' => 12,
'footer-spacing' => 12,
'encoding' => 'utf-8',
'javascript-delay' => 20000,
'no-stop-slow-scripts' => true,
'window-status' => "finished",
'disable-smart-shrinking' => true,
'dpi' => 400,
// 'zoom' => 0.75
])->loadView('pdf.contest.main', [
'conf' => $config,
'contest' => [
'cid' => $cid,
'name' => $record->name,
'shortName' => $record->name,
'date' => date("F j, Y", strtotime($record->begin_time)),
],
'problemset' => $record->getProblemSet(false),
])->setPaper('a4')->save(storage_path("app/contest/pdf/$cid.pdf"), true);

// dump(Contest::find($cid)->getProblemSet(false)[0]['title']);
$puppeteer = new Puppeteer;
$browser = $puppeteer->launch([
'args' => ['--no-sandbox', '--disable-setuid-sandbox'],
]);

$page = $browser->newPage();

$response = $page->goto(route('contest.board.admin.pdf.view', [
'cid' => $cid,
'accessToken' => $accessToken,
]), [
'waitUntil' => 'networkidle0'
]);

if($response->status() != '200') {
throw new Exception('Cannot Access PDF Generated View Stream');
}

$page->waitForSelector('body.rendered', [
'timeout' => 120000
]);

if($config['renderer'] == 'blink') {
$page->pdf([
'format' => 'A4',
'path' => storage_path("app/contest/pdf/$cid.pdf"),
'printBackground' => true
]);

$browser->close();
return;
}

$parsedHTML = $page->content();

$browser->close();

// $pdf=PDF::setOptions([
// 'dpi' => 150,
// 'isPhpEnabled' => true,
// 'isHtml5ParserEnabled' => true,
// 'isRemoteEnabled' => true
// ])->setWarnings(true)->loadView('pdf.contest.main', [
// 'conf'=>$config,
// 'conf' => $config,
// 'contest' => [
// 'cid'=>$cid,
// 'name'=>$record->name,
// 'shortName'=>$record->name,
// 'date'=>date("F j, Y", strtotime($record->begin_time)),
// 'cid' => $cid,
// 'name' => $record->name,
// 'shortName' => $record->name,
// 'date' => date("F j, Y", strtotime($record->begin_time)),
// ],
// 'problemset'=>$record->getProblemSet(false),
// 'problemset' => $record->getProblemSet(),
// ]);

// $pdf->getDomPDF()->add_info('Subject', "$record->name ProblemSet");
// $pdf->getDomPDF()->add_info('Producer', config('app.displayName'));
// $pdf->getDomPDF()->add_info('Creator', config('app.name').' Contest PDF Auto-Generater');
// $pdf->getDomPDF()->add_info('CreatorTool', config('app.url'));
// $pdf->getDomPDF()->add_info('BaseURL', route('contest.detail',['cid'=>$cid]));

// $pdf->save(storage_path("app/contest/pdf/$cid.pdf"));
// file_put_contents(__DIR__."/lalala.html", $parsedHTML);
// return;

$pdf=PDF::setOptions([
'dpi' => 96,
'isPhpEnabled' => true,
'isHtml5ParserEnabled' => true,
'isRemoteEnabled' => true
])->setWarnings(false)->loadHTML($parsedHTML);

$pdf->output();

$pdf->addInfo([
'Subject' => "$record->name ProblemSet",
'Producer' => config('app.displayName'),
'Creator' => config('app.name').' Contest PDF Auto-Generater',
'CreatorTool' => config('app.url'),
'BaseURL' => route('contest.detail', ['cid' => $cid]),
])->save(storage_path("app/contest/pdf/$cid.pdf"));

$record->pdf = 1;
$record->save();
Expand Down

0 comments on commit 29c4275

Please sign in to comment.