src/AppBundle/Controller/Admin/CloudFileController.php line 44

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller\Admin;
  3. use AppBundle\Common\ArrayToolkit;
  4. use AppBundle\Common\Paginator;
  5. use Biz\CloudFile\Service\CloudFileService;
  6. use Biz\CloudPlatform\CloudAPIFactory;
  7. use Symfony\Component\HttpFoundation\Request;
  8. class CloudFileController extends BaseController
  9. {
  10.     public function indexAction()
  11.     {
  12.         try {
  13.             $api CloudAPIFactory::create('leaf');
  14.             $result $api->get('/me');
  15.         } catch (\RuntimeException $e) {
  16.             return $this->render('admin/cloud-file/api-error.html.twig', []);
  17.         }
  18.         $storageSetting $this->getSettingService()->get('storage', []);
  19.         if (isset($result['hasStorage']) && '1' == $result['hasStorage'] && isset($storageSetting['upload_mode']) && 'cloud' == $storageSetting['upload_mode']) {
  20.             return $this->redirect($this->generateUrl('admin_cloud_file_manage'));
  21.         }
  22.         return $this->render('admin/cloud-file/error.html.twig', []);
  23.     }
  24.     public function manageAction(Request $request)
  25.     {
  26.         $storageSetting $this->getSettingService()->get('storage', []);
  27.         if ('cloud' != $storageSetting['upload_mode']) {
  28.             return $this->redirect($this->generateUrl('admin_cloud_file'));
  29.         }
  30.         return $this->render('admin/cloud-file/manage.html.twig', [
  31.             'tags' => $this->getTagService()->findAllTags(0PHP_INT_MAX),
  32.         ]);
  33.     }
  34.     public function renderAction(Request $request)
  35.     {
  36.         $conditions $request->query->all();
  37.         //云资源应该只显示resType为normal的
  38.         $conditions['storage'] = 'cloud';
  39.         $conditions['syncIdGT'] = 1;
  40.         $results $this->getCloudFileService()->search(
  41.             $conditions,
  42.             ($request->query->get('page'1) - 1) * 20,
  43.             20
  44.         );
  45.         $paginator = new Paginator(
  46.             $request,
  47.             $results['count'],
  48.             20
  49.         );
  50.         $pageType = (isset($conditions['resType']) && 'attachment' == $conditions['resType']) ? 'attachment' 'file';
  51.         return $this->render('admin/cloud-file/tbody.html.twig', [
  52.             'pageType' => $pageType,
  53.             'type' => empty($conditions['type']) ? 'all' $conditions['type'],
  54.             'materials' => $results['data'],
  55.             'createdUsers' => isset($results['createdUsers']) ? $results['createdUsers'] : [],
  56.             'paginator' => $paginator,
  57.         ]);
  58.     }
  59.     public function previewAction(Request $reqeust$globalId)
  60.     {
  61.         $file $this->getCloudFileService()->getByGlobalId($globalId);
  62.         return $this->render('admin/cloud-file/preview-modal.html.twig', [
  63.             'file' => $file,
  64.         ]);
  65.     }
  66.     public function detailAction(Request $reqeust$globalId)
  67.     {
  68.         try {
  69.             if (!$globalId) {
  70.                 return $this->render('admin/cloud-file/detail-not-found.html.twig', []);
  71.             }
  72.             $cloudFile $this->getCloudFileService()->getByGlobalId($globalId);
  73.         } catch (\RuntimeException $e) {
  74.             return $this->render('admin/cloud-file/detail-not-found.html.twig', []);
  75.         }
  76.         try {
  77.             if ('video' == $cloudFile['type']) {
  78.                 $thumbnails $this->getCloudFileService()->getDefaultHumbnails($globalId);
  79.             }
  80.         } catch (\RuntimeException $e) {
  81.             $thumbnails = [];
  82.         }
  83.         return $this->render('admin/cloud-file/detail.html.twig', [
  84.             'material' => $cloudFile,
  85.             'thumbnails' => empty($thumbnails) ? '' $thumbnails,
  86.             'params' => $reqeust->query->all(),
  87.             'editUrl' => $this->generateUrl('admin_cloud_file_edit', ['globalId' => $globalId]),
  88.         ]);
  89.     }
  90.     public function editAction(Request $request$globalId$fields)
  91.     {
  92.         $fields $request->request->all();
  93.         $result $this->getCloudFileService()->edit($globalId$fields);
  94.         return $this->createJsonResponse($result);
  95.     }
  96.     public function reconvertAction(Request $request$globalId)
  97.     {
  98.         $cloudFile $this->getCloudFileService()->reconvert($globalId, [
  99.             'directives' => [],
  100.         ]);
  101.         if (isset($cloudFile['createdUserId'])) {
  102.             $createdUser $this->getUserService()->getUser($cloudFile['createdUserId']);
  103.         }
  104.         return $this->render('admin/cloud-file/table-tr.html.twig', [
  105.             'cloudFile' => $cloudFile,
  106.             'createdUser' => isset($createdUser) ? $createdUser : [],
  107.         ]);
  108.     }
  109.     public function downloadAction(Request $request$globalId)
  110.     {
  111.         $ssl $request->isSecure() ? true false;
  112.         $download $this->getCloudFileService()->download($globalId$ssl);
  113.         return $this->redirect($download['url']);
  114.     }
  115.     public function deleteAction($globalId)
  116.     {
  117.         $result $this->getCloudFileService()->delete($globalId);
  118.         return $this->createJsonResponse($result);
  119.     }
  120.     public function batchDeleteAction(Request $request)
  121.     {
  122.         $data $request->request->all();
  123.         if (isset($data['ids']) && !empty($data['ids'])) {
  124.             $this->getCloudFileService()->batchDelete($data['ids']);
  125.             return $this->createJsonResponse(true);
  126.         }
  127.         return $this->createJsonResponse(false);
  128.     }
  129.     public function deleteShowAction(Request $request)
  130.     {
  131.         $globalIds $request->request->get('ids');
  132.         $files $this->getUploadFileService()->searchFiles(
  133.             ['globalIds' => $globalIds],
  134.             ['createdTime' => 'desc'],
  135.             0PHP_INT_MAX
  136.         );
  137.         $materials = [];
  138.         if ($files) {
  139.             $files ArrayToolkit::index($files'id');
  140.             $fileIds ArrayToolkit::column($files'id');
  141.             $materials $this->getCourseMaterialService()->findUsedCourseMaterials($fileIds$courseId 0);
  142.         }
  143.         return $this->render('material-lib/web/delete-file-modal.html.twig', [
  144.             'materials' => $materials,
  145.             'files' => $files,
  146.             'ids' => $globalIds,
  147.             'deleteFormUrl' => $this->generateUrl('admin_cloud_file_batch_delete'),
  148.         ]);
  149.     }
  150.     public function batchTagShowAction(Request $request)
  151.     {
  152.         $data $request->request->all();
  153.         $fileIds preg_split('/,/'$data['fileIds']);
  154.         $this->getMaterialLibService()->batchTagEdit($fileIds$data['tags']);
  155.         return $this->redirect($this->generateUrl('admin_cloud_file_manage'));
  156.     }
  157.     protected function getSettingService()
  158.     {
  159.         return $this->createService('System:SettingService');
  160.     }
  161.     protected function getTagService()
  162.     {
  163.         return $this->createService('Taxonomy:TagService');
  164.     }
  165.     /**
  166.      * @return CloudFileService
  167.      */
  168.     protected function getCloudFileService()
  169.     {
  170.         return $this->createService('CloudFile:CloudFileService');
  171.     }
  172.     protected function getUploadFileService()
  173.     {
  174.         return $this->createService('File:UploadFileService');
  175.     }
  176.     protected function getMaterialLibService()
  177.     {
  178.         return $this->createService('MaterialLib:MaterialLibService');
  179.     }
  180.     protected function getCourseMaterialService()
  181.     {
  182.         return $this->createService('Course:MaterialService');
  183.     }
  184. }