public function getRequestZipDownLoad($request){
$fileIds = explode(',',$_POST['zipDownId']);
$isDownLoad = $_POST['isDownLoad'];
$zip = new \ZipArchive();
foreach ($fileIds as $value)
{
$relevance = $this->getLdAsrService()->asrGetByFields(
['fileId' => $value,'type'=>'cloud','status'=>'finished']
);
if(empty($relevance)){
return $this->createJsonResponse(['message' => '未生成字幕', 'code' => -2]);
}
if (in_array('1',$isDownLoad) && in_array('2',$isDownLoad)) {
$this->SrtDownLoadZipFormat($zip,$relevance,$value);
$this->WordDownLoadZipFormat($zip,$relevance);
if($zip->open($this->srtWordDown, ZIPARCHIVE::CREATE) !== true) {
return false;
}
if(file_exists($this->srtDown) && file_exists($this->wordDown)){
$zipArray = [$this->srtDown,$this->wordDown];
foreach($zipArray as $v){
$zip->addFile($v,basename($v));
}
}
}else if (in_array('1',$isDownLoad)) {
$this->SrtDownLoadZipFormat($zip,$relevance,$value);
}else if (in_array('2',$isDownLoad)) {
$this->WordDownLoadZipFormat($zip,$relevance);
}
}
$host = $request->getSchemeAndHttpHost();
$url = $host .'/lddev.php/course_set/manage/batch/packDownload';
return $this->createJsonResponse(['data' => $url]);
}
public function WordDownLoadZipFormat($zip,$relevance){
if($zip->open($this->wordDown, ZIPARCHIVE::CREATE) !== true) {
return false;
}
$subtitleData = json_decode($relevance['subtitleData'], true);
if ($relevance['type'] == 'cloud' && empty($relevance['fileName'])) {
$file = $this->getUploadFileService()->getFile($relevance['fileId']);
$filename = str_replace(strrchr($file['filename'], "."), "", $file['filename']) . '.docx';
}else{
$filename = str_replace(strrchr($relevance['fileName'], "."), "", $relevance['fileName']) . '.docx';
}
$subtitleText = '';
foreach ($subtitleData as $subtitle)
{
$subtitleText .= $this->processSpecialChars($subtitle['text']);
}
ob_start();
echo $subtitleText;
$data = ob_get_contents();
ob_end_clean();
$fp = fopen($filename,"wb");
fwrite($fp,$data);
fclose($fp);
$zip->addFile($filename,basename($filename));
$zip->close();
unlink(basename($filename));
}
public function SrtDownLoadZipFormat($zip,$relevance,&$value,$isFalg = null){
$file = $this->getUploadFileService()->getFile($value);
$srtName = $file['globalId'] . '.' . 'srt';
$path = $this->getBiz()['topxia.upload.public_directory'] . '/zipDownLoad/srt/';
if(!is_dir($path)){
mkdir($path,0777,true);
}
$pathName = $this->getBiz()['topxia.upload.public_directory'] . '/zipDownLoad/srt/'. $srtName;
$this->asrGeneralClient = new General($this->getBiz());
$this->asrGeneralClient->jsonToSrt(
json_decode($relevance['subtitleData'], true),
$pathName
);
$this->dirToZip([$path],$this->srtDown);
$this->deleteDirectory($path);
}
public static function addFileToZip($path,$zip)
{
$handler = opendir($path);
while (($filename = readdir($handler)) !== false) {
if ($filename != "." && $filename != "..") {
if (is_dir($path . "/" . $filename)) {
self::addFileToZip($path . "/" . $filename, $zip);
} else {
$zip->addFile($path . "/" . $filename,'/srt/'.$filename);
}
}
}
}
public function dirToZip($paths,$filename)
{
$zip = new \ZipArchive();
try {
if ($zip->open($filename, $zip::CREATE) == TRUE) {
foreach ($paths as $path){
self::addFileToZip($path, $zip);
}
$zip->close();
}
if(file_exists($filename)){
return ['code'=>200,'url'=>$filename,'msg'=>'success'];
}
return ['code'=>-2002,'msg'=>'打包失败'];
} catch (\Exception $e) {
return ['code'=>-2001,'msg'=>$e->getMessage()];
}
}
public function deleteDirectory($path){
$files = array_diff(scandir($path), array('.', '..'));
foreach ($files as $file) {
if (is_file($path.'/'.$file)) {
unlink($path.'/'.$file);
}
else {
rmdir($path.'/'.$file);
}
}
rmdir($path);
}
protected function processSpecialChars($questionText)
{
return str_replace('&', '&', $questionText);
}