aws_polly-1.0.x-dev/src/Service/TextToSpeech.php
src/Service/TextToSpeech.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | <?php namespace Drupal\aws_polly\Service; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Session\AccountProxy; use Drupal\file\Entity\File; use Aws\Polly\PollyClient; use Aws\Credentials\Credentials; use Aws\Sts\StsClient; use Aws\Sts\Exception\StsException; /** * Contains the TextToSpeech. */ class TextToSpeech { /** * The aws polly configuration object. * * @var Drupal\Core\Config\ImmutableConfig */ public $config ; /** * The aws polly client object. * * @var Aws\Polly\PollyClient */ protected $client ; /** * Drupal\Core\Session\AccountProxy definition. * * @var \Drupal\Core\Session\AccountProxy */ protected $currentUser ; /** * Aws\Credentials\Credentials definition. * * @var \Aws\Credentials\Credentials */ protected $credentials ; /** * Creates a config of aws_polly.settings. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. * @param \Drupal\Core\Session\AccountProxy $current_user * The current user object. */ public function __construct(ConfigFactoryInterface $config_factory , AccountProxy $current_user ) { $this ->config = $config_factory ->get( 'aws_polly.settings' ); $this ->currentUser = $current_user ; $this ->credentials = new Credentials( $this ->config->get( 'aws_access_key' ), $this ->config->get( 'aws_secret_key' )); $this ->client = new PollyClient([ 'version' => '2016-06-10' , 'credentials' => $this ->credentials, 'region' => $this ->config->get( 'aws_region' ), ]); } /** * This function calls aws polly service and return audio file. * * @param array $parameters * Parameters to pass aws polly service. * * @return blob * Audio file object for given parameter. */ public function getAudio( array $parameters = []) { $format = $parameters [ 'format' ] ?? 'mp3' ; $result = $this ->client->synthesizeSpeech([ 'OutputFormat' => $format , 'Text' => $parameters [ 'text' ], 'TextType' => 'text' , 'VoiceId' => $this ->config->get( 'voice_id' ), ]); $file = $result ->get( 'AudioStream' )->getContents(); return $file ; } /** * Function generates the audio file and returns file object. * * @param array $parameters * Parameters pass to generate file. * * @return Drupal\file\Entity\File * Returns drupal file object. */ public function generateFile( array $parameters = []) { $content = $this ->getAudio( $parameters ); $file = $this ->getFile( $parameters ); $this ->uploadFile( $file , $content ); return $file ; } /** * This function gets file object from given parameters. * * @param array $parameters * Paramters pass to get file object. * @param string $uri_schema * Uri scheme to get file. * * @return Drupal\file\Entity\File * Return drupal file object. * * @todo give to filename more info about content (ie: node title). */ $format = $parameters [ 'format' ] ?? 'mp3' ; $fileExt = $format ; $month = date ( 'm' ); $year = date ( 'Y' ); $folder = $year . "-" . $month ; $random = rand(100, 999); $user_id = $this ->currentUser->id(); $path = 'polly/' . $folder . "/polly_" . $random . "_" . $user_id . "_" . time() . "." . $fileExt ; $file = File::create([ 'uid' => $user_id , 'filename' => basename ( $path ), 'uri' => $uri_schema . $path , 'status' => 1, ]); $file ->save(); return $file ; } /** * Function uploads the file from given content. * * @param Drupal\file\Entity\File $file * The drupal File object. * @param string $content * The audio file content from aws polly service. */ public function uploadFile(File $file , string $content ) { $dir = dirname( $file ->getFileUri()); if (! file_exists ( $dir )) { mkdir ( $dir , 0770, TRUE); } file_put_contents ( $file ->getFileUri(), $content ); } /** * Check if the current credentials are valid. */ public function checkAwsCredentials() { $valid = TRUE; $client = new StsClient([ 'credentials' => $this ->credentials, 'region' => ( $this ->config->get( 'aws_region' ) ?: 'us-east-1' ), 'version' => '2011-06-15' , ]); try { $result = $client ->getCallerIdentity(); } catch (StsException $e ) { $valid = FALSE; } return $valid ; } } |