partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

FileMigrationController.php (3465B)


      1 <?php
      2 
      3 namespace PartKeepr\SetupBundle\Controller;
      4 
      5 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
      6 use Symfony\Component\HttpFoundation\JsonResponse;
      7 use Symfony\Component\HttpFoundation\Request;
      8 use Symfony\Component\HttpFoundation\Response;
      9 
     10 class FileMigrationController extends SetupBaseController
     11 {
     12     /**
     13      * Checks if there are existing userds in the database.
     14      *
     15      * @Route("/setup/migrateFiles")
     16      *
     17      * @param Request $request
     18      *
     19      * @return Response
     20      */
     21     public function migrateFilesAction(Request $request)
     22     {
     23         $this->dumpConfig($request);
     24 
     25         $response = $this->handleRequest($request, '/setup/_int_migrate_files_action');
     26 
     27         return new Response($response->getContent());
     28     }
     29 
     30     /**
     31      * @Route("/setup/_int_migrate_files_action")
     32      */
     33     public function intMigrateFilesAction(Request $request)
     34     {
     35         if (!$this->ensureAuthKey($request)) {
     36             return $this->getAuthKeyErrorResponse();
     37         }
     38 
     39         $response = [
     40             'success' => true,
     41             'errors'  => [],
     42             'message' => 'No files to migrate',
     43         ];
     44 
     45         $legacyConfig = $this->get('partkeepr.setup.config_service')->legacyConfigParser();
     46 
     47         $legacyFilePath = $this->get('kernel')->getRootDir().'/../data/';
     48         $legacyImagePath = $this->get('kernel')->getRootDir().'/../data/images/';
     49 
     50         $legacyFileDirectories = ['FootprintAttachment', 'PartAttachment', 'ProjectAttachment'];
     51 
     52         if (array_key_exists('partkeepr.files.path', $legacyConfig)) {
     53             $legacyFilePath = $legacyConfig['partkeepr.files.path'];
     54         }
     55 
     56         if (array_key_exists('partkeepr.images.path', $legacyConfig)) {
     57             $legacyImagePath = $legacyConfig['partkeepr.images.path'];
     58         }
     59 
     60         $newFilesPath = $legacyFilePath.'/files/';
     61         $newImagesPath = $legacyFilePath.'/images/';
     62 
     63         if (!is_dir($newFilesPath)) {
     64             mkdir($newFilesPath, 0777, true);
     65         }
     66 
     67         if (!is_dir($newImagesPath)) {
     68             mkdir($newImagesPath, 0777, true);
     69         }
     70 
     71         foreach ($legacyFileDirectories as $legacyFileDirectory) {
     72             $legacyMovePath = $legacyFilePath.'/'.$legacyFileDirectory.'/';
     73             $newMovePath = $legacyFilePath.'/files/'.$legacyFileDirectory.'/';
     74 
     75             if (is_dir($legacyMovePath)) {
     76                 $this->moveFiles($legacyMovePath, $newMovePath);
     77                 $response['message'] = 'Old image directories moved';
     78             }
     79         }
     80 
     81         $legacyImageDirectories = ['footprint', 'iclogo', 'part', 'storagelocation'];
     82 
     83         foreach ($legacyImageDirectories as $legacyImageDirectory) {
     84             $legacyMovePath = $legacyImagePath.'/'.$legacyImageDirectory.'/';
     85             $newMovePath = $legacyFilePath.'/images/'.$legacyImageDirectory.'/';
     86 
     87             if (is_dir($legacyMovePath)) {
     88                 $this->moveFiles($legacyMovePath, $newMovePath);
     89                 $response['message'] = 'Old image directories moved';
     90             }
     91         }
     92 
     93         return new JsonResponse($response);
     94     }
     95 
     96     public function moveFiles($source, $target)
     97     {
     98         $iterator = new \DirectoryIterator($source);
     99 
    100         if (!is_dir($target)) {
    101             mkdir($target, 0777, true);
    102         }
    103 
    104         foreach ($iterator as $file) {
    105             if (!$file->isDot()) {
    106                 rename($file->getPathName(), $target.'/'.$file->getFilename());
    107             }
    108         }
    109     }
    110 }