From 257296cee0dde6b4faf8f0317659c5d35b8364f5 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 26 Sep 2023 17:41:05 +0200 Subject: [PATCH] wait #6306 @0.5 --- app/Fluidbook/Compiler/Accessibility.php | 103 ++++++++++++++++++++++ app/Fluidbook/Compiler/Compiler.php | 107 +---------------------- app/Models/FluidbookPublication.php | 3 + 3 files changed, 107 insertions(+), 106 deletions(-) create mode 100644 app/Fluidbook/Compiler/Accessibility.php diff --git a/app/Fluidbook/Compiler/Accessibility.php b/app/Fluidbook/Compiler/Accessibility.php new file mode 100644 index 000000000..964cdee88 --- /dev/null +++ b/app/Fluidbook/Compiler/Accessibility.php @@ -0,0 +1,103 @@ +fluidbookSettings->audiodescriptionTexts) { + + $file = $this->wdir . '/' . $this->fluidbookSettings->audiodescriptionTexts; + if (file_exists($file)) { + $reader = new Xlsx(); + $phpexcel = $reader->load($file); + + $sheet = $phpexcel->getActiveSheet(); + $maxRow = $sheet->getHighestRow('A'); + + for ($i = 0; $i <= $maxRow; $i++) { + $page = trim($sheet->getCell('A' . $i)->getValue()); + $text = trim($sheet->getCell('B' . $i)->getValue()); + $voice = trim($sheet->getCell('C' . $i)->getValue()); + if ($page == '' || $text == '') { + continue; + } + $data = ['text' => $text]; + if ($voice) { + $data['voice'] = $voice; + } + $this->audioDescriptionTextsList[$page] = $data; + } + } + } + + foreach ($this->audioDescriptionTextsList as $page => $data) { + $replace = [ + '`' => "'", + '“' => '"', + '”' => '"', + '’' => "'", + '—' => " - ", + '‘' => "'", + "…" => "...", + ]; + + $text = trim($data['text']); + $text = str_replace(array_keys($replace), array_values($replace), $text); + $text = Text::cleanUTF8($text, ''); + + $voiceInfos = $data['voice'] ?? $this->fluidbookSettings->audiodescriptionVoice; + + if ($voiceInfos) { + $e = explode(':', $voiceInfos); + + if (count($e) === 1) { + $engine = 'azuretts'; + $voice = $voiceInfos; + } else { + $engine = $e[0]; + $voice = $e[1]; + } + + $hash = hash('sha256', $engine . ':' . $voice . '_^_' . $text); + $fname = $hash . '.mp3'; + $dir = Files::mkdir(protected_path('audiodescription')); + + $file = $dir . $fname; + + if (!file_exists($file) || filesize($file) === 0) { + if ($engine == 'azuretts') { + $e = explode('/', $voice); + $this->_azureTTS($text, $e[0], $e[1], $e[2], $file); + } + } + + $this->config->set('audiodescription.' . $page, $fname); + $this->vdir->copy($file, 'data/audiodescription/' . $fname); + } + $this->accessibleTexts[$page] = $text; + } + + if (count($this->accessibleTexts) > 0) { + $this->config->accessibleTexts = $this->accessibleTexts; + } + } + + + protected function _azureTTS($text, $locale, $gender, $voiceName, $output) + { + try { + $api = new \Cubist\Azure\TTS\Api(env('AZURE_API_KEY')); + $api->textToSpeech($text, $locale, $gender, $voiceName, $output); + if (!file_exists($output)) { + throw new Exception('Error while making tts (' . $output . ') : ' . $text . ', ' . $locale . ', ' . $gender . ', ' . $voiceName); + } + } catch (Exception $e) { + } + } +} diff --git a/app/Fluidbook/Compiler/Compiler.php b/app/Fluidbook/Compiler/Compiler.php index 0d81f145f..5922d57ac 100644 --- a/app/Fluidbook/Compiler/Compiler.php +++ b/app/Fluidbook/Compiler/Compiler.php @@ -3,10 +3,7 @@ namespace App\Fluidbook\Compiler; use App\Fields\FluidbookFont; -use App\Fluidbook\Link\Link; use App\Fluidbook\PDF; -use App\Fluidbook\SearchIndex; -use App\Fluidbook\SEO\Document; use App\Fluidbook\SEO\Page; use App\Fluidbook\SocialImage; use App\Http\Controllers\Admin\Operations\Tools\Favicon; @@ -14,14 +11,12 @@ use App\Jobs\Base; use App\Jobs\FluidbookImagesPreprocess; use App\Models\FluidbookPublication; use App\Models\FluidbookTheme; -use App\Models\FluidbookTranslate; use App\Models\Signature; use App\Models\Traits\FluidbookPlayerBranches; use Cubist\Backpack\Magic\Fields\Checkbox; use Cubist\Excel\ExcelToArray; use Cubist\Locale\Country; use Cubist\Locale\Locale; -use Cubist\PDF\CommandLine\FWSTK; use Cubist\Util\ArrayUtil; use Cubist\Util\CommandLine; use Cubist\Util\Data; @@ -31,19 +26,13 @@ use Cubist\Util\Graphics\Color; use Cubist\Util\Graphics\Image; use Cubist\Util\PHP; use Cubist\Util\Text; -use Cubist\Util\WebVideo; use Cubist\Util\Zip; use DOMDocument; use DOMElement; use DOMXPath; -use Exception; use Fluidbook\Tools\Compiler\CompilerInterface; use Fluidbook\Tools\SVG\SVGTools; use Illuminate\Console\Command; -use Illuminate\Support\Facades\Log; -use PhpOffice\PhpSpreadsheet\Reader\Xlsx; -use PhpOffice\PhpSpreadsheet\Spreadsheet; -use SimpleXMLElement; use SplFileInfo; class Compiler extends Base implements CompilerInterface @@ -57,6 +46,7 @@ class Compiler extends Base implements CompilerInterface use Articles; use Cache; use Search; + use Accessibility; protected static $uaPrefixes = array('-moz-', '-webkit-', '-o-', '-ms-', ''); @@ -760,7 +750,6 @@ class Compiler extends Base implements CompilerInterface } } - protected function _extranetToToolboxPath($path) { if (str_starts_with($path, '/application/fluidbook/books/working/')) { @@ -882,100 +871,6 @@ class Compiler extends Base implements CompilerInterface } - protected function writeAccessibility() - { - if ($this->fluidbookSettings->audiodescriptionTexts) { - - $file = $this->wdir . '/' . $this->fluidbookSettings->audiodescriptionTexts; - if (file_exists($file)) { - $reader = new Xlsx(); - $phpexcel = $reader->load($file); - - $sheet = $phpexcel->getActiveSheet(); - $maxRow = $sheet->getHighestRow('A'); - - for ($i = 0; $i <= $maxRow; $i++) { - $page = trim($sheet->getCell('A' . $i)->getValue()); - $text = trim($sheet->getCell('B' . $i)->getValue()); - $voice = trim($sheet->getCell('C' . $i)->getValue()); - if ($page == '' || $text == '') { - continue; - } - $data = ['text' => $text]; - if ($voice) { - $data['voice'] = $voice; - } - $this->audioDescriptionTextsList[$page] = $data; - } - } - } - - foreach ($this->audioDescriptionTextsList as $page => $data) { - $replace = [ - '`' => "'", - '“' => '"', - '”' => '"', - '’' => "'", - '—' => " - ", - '‘' => "'", - "…" => "...", - ]; - - $text = trim($data['text']); - $text = str_replace(array_keys($replace), array_values($replace), $text); - $text = Text::cleanUTF8($text, ''); - - $voiceInfos = $data['voice'] ?? $this->fluidbookSettings->audiodescriptionVoice; - - if ($voiceInfos) { - $e = explode(':', $voiceInfos); - - if (count($e) === 1) { - $engine = 'azuretts'; - $voice = $voiceInfos; - } else { - $engine = $e[0]; - $voice = $e[1]; - } - - $hash = hash('sha256', $engine . ':' . $voice . '_^_' . $text); - $fname = $hash . '.mp3'; - $dir = Files::mkdir(protected_path('audiodescription')); - - $file = $dir . $fname; - - if (!file_exists($file) || filesize($file) === 0) { - if ($engine == 'azuretts') { - $e = explode('/', $voice); - $this->_azureTTS($text, $e[0], $e[1], $e[2], $file); - } - } - - $this->config->set('audiodescription.' . $page, $fname); - $this->vdir->copy($file, 'data/audiodescription/' . $fname); - } - $this->accessibleTexts[$page] = $text; - } - - if (count($this->accessibleTexts) > 0) { - $this->config->accessibleTexts = $this->accessibleTexts; - } - } - - - protected function _azureTTS($text, $locale, $gender, $voiceName, $output) - { - try { - $api = new \Cubist\Azure\TTS\Api('28fdfcdcc7f141b29cd9db4afc5779c5'); - $api->textToSpeech($text, $locale, $gender, $voiceName, $output); - if (!file_exists($output)) { - throw new Exception('Error while making tts (' . $output . ') : ' . $text . ', ' . $locale . ', ' . $gender . ', ' . $voiceName); - } - } catch (Exception $e) { - dd($e); - } - } - protected function _writeIndex($page) { if (!isset($this->seo->pages[$page])) { diff --git a/app/Models/FluidbookPublication.php b/app/Models/FluidbookPublication.php index 3da16f38d..249990bb5 100644 --- a/app/Models/FluidbookPublication.php +++ b/app/Models/FluidbookPublication.php @@ -883,6 +883,9 @@ class FluidbookPublication extends ToolboxSettingsModel public function getAssetDirId() { + if ($this->composition_fluidbook && $this->composition_fluidbook_links && $this->composition_fluidbook_id && !$this->assetsDir) { + $this->assetsDir = $this->composition_fluidbook_id; + } return !isset($this->assetsDir) || !$this->assetsDir ? $this->id : trim($this->assetsDir); } -- 2.39.5