public function install(Context $context) { $response = new JsonResponse(); try { // file upload handling $ext = 'zip'; $fu = new FileUpload('uploadfile'); $result = $fu->handleUpload(NOVOSGA_CACHE, [$ext]); if (!$result) { throw new Exception($fu->getErrorMsg()); } // install module $service = new ModuloService($this->em()); $service->extractAndInstall($fu->getSavedFile(), $fu->getExtension()); FileUtils::rm($fu->getSavedFile()); // response $response->success = true; $response->message = _('Módulo instalado com sucesso'); } catch (Exception $e) { $response->message = $e->getMessage(); } return $response; }
public function moduleResource($moduleKey, $resource) { $filename = implode(DS, [MODULES_PATH, implode(DS, explode('.', $moduleKey)), 'public', $resource]); if (file_exists($filename)) { $mime = \Novosga\Util\FileUtils::mime($filename); header("Content-type: {$mime}"); echo file_get_contents($filename); exit; } else { throw new Exception(sprintf('Resource not found: %s', $filename)); } }
/** * @param string $zipname * * @throws Exception * * @return string The module dir */ public function extract($zipname) { $name = str_replace('.zip', '', basename($zipname)); $path = $this->verifyKey($name); $dir = MODULES_PATH . DS . $path[0]; $moduleDir = $dir . DS . $path[1]; if (!file_exists($zipname)) { throw new Exception(sprintf(_('Arquivo não encontrado: %s'), $zipname)); } if (file_exists($moduleDir)) { throw new Exception(_('Já possui um módulo com o mesmo nome')); } $cacheDir = NOVOSGA_CACHE . DS . $name; // zip extract $zip = new ZipArchive(); $zip->open($zipname); $zip->extractTo($cacheDir); $zip->close(); // vendor dir if (!is_dir($dir)) { if (!@mkdir($dir)) { FileUtils::rm(NOVOSGA_CACHE . DS . $name); throw new Exception(_('Não foi possível criar o diretório do módulo')); } chmod($dir, 0777); } if (!@rename($cacheDir, $moduleDir)) { FileUtils::rm(NOVOSGA_CACHE . DS . $name); throw new Exception(_('Não foi possível mover os arquivos para o diretório dos módulos')); } FileUtils::rm(NOVOSGA_CACHE . DS . $name); return $moduleDir; }