Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. <?php
  2.  
  3. class FileShardingCommand
  4. extends EConsoleCommand
  5. {
  6. public function actionIndex()
  7. {
  8. $transaction = \Yii::app()->db->beginTransaction();
  9. try
  10. {
  11. while (true)
  12. {
  13. $sql = <<<SQL
  14. SELECT * FROM tbl_file WHERE migration_status is null or migration_status = '';
  15. SQL;
  16.  
  17. $file = File::model()->findBySql($sql);
  18.  
  19. if (!$file)
  20. {
  21. exit;
  22. }
  23. $file->prepareUpdateList()
  24. ->setPlainField('migration_status', 'in_progress')
  25. ->apply();
  26.  
  27.  
  28. $fileName = $this->getFileName($file->id);
  29. $filePath = $this->getFilePath($fileName);
  30.  
  31. if ($newFilePath = $this->saveFile($file, $filePath))
  32. {
  33. $file->prepareUpdateList()
  34. ->setPlainField('migration_status', 'done')
  35. ->setPlainField('spec', $this->makeSpec($newFilePath))
  36. ->apply();
  37.  
  38. echo "ok";
  39. $transaction->commit();
  40. }
  41. else
  42. {
  43. continue;
  44. }
  45.  
  46. }
  47.  
  48. }
  49. catch (Throwable $e)
  50. {
  51. $transaction->rollback();
  52. throw $e;
  53. }
  54. }
  55.  
  56. public function makeSpec(string $newFilePath):string
  57. {
  58. return str_replace(\Yii::app()->env->get(\env\files_path), 'file:%files_path%', $newFilePath);
  59. }
  60.  
  61. public function getFileName(int $id): string
  62. {
  63. $id = (string)$id;
  64.  
  65. $idLength = strlen($id);
  66. $fileNameLength = 12;
  67.  
  68. $zeroNum = $fileNameLength - $idLength;
  69.  
  70. if (0 === $zeroNum)
  71. {
  72. return strrev($id);
  73. }
  74.  
  75. return strrev(str_repeat("0", $zeroNum) . $id);
  76. }
  77.  
  78. public function getFilePath(string $fileName): string
  79. {
  80. return \Yii::app()->env->get(\env\files_path) . DIRECTORY_SEPARATOR .
  81. substr($fileName, 0, 3) . DIRECTORY_SEPARATOR .
  82. substr($fileName, 3, 3) . DIRECTORY_SEPARATOR .
  83. substr($fileName, 6, 3) . DIRECTORY_SEPARATOR .
  84. substr($fileName, 9, 3);
  85. }
  86.  
  87.  
  88. public function saveFile(File $file, string $newFilePath)
  89. {
  90. $oldFilePath = $file->getHandler()->getPathname();
  91.  
  92. if (!file_exists($oldFilePath))
  93. {
  94. $file->prepareUpdateList()
  95. ->setPlainField('migration_status', 'error')
  96. ->apply();
  97. return;
  98. }
  99.  
  100. $oldHash = hash_file('md5', $file->getHandler()->getPathname());
  101.  
  102.  
  103. if (is_dir($newFilePath))
  104. {
  105. $newFilePath = $newFilePath . DIRECTORY_SEPARATOR . $this->getFileNameToSave($file);
  106.  
  107. if (file_exists($newFilePath))
  108. {
  109. throw new RuntimeException('уже есть такой файл');
  110.  
  111. }
  112. if (copy($oldFilePath, $newFilePath) && hash_file('md5', $newFilePath) === $oldHash)
  113. {
  114. return $newFilePath;
  115. }
  116. }
  117. else
  118. {
  119. if (mkdir($newFilePath, '0777', true))
  120. {
  121. $newFilePath = $newFilePath . DIRECTORY_SEPARATOR . $this->getFileNameToSave($file);
  122.  
  123. if (copy($oldFilePath, $newFilePath) && hash_file('md5', $newFilePath) === $oldHash)
  124. {
  125. unlink($oldFilePath);
  126. @unlink($file->getSourcePathName());
  127. return $newFilePath;
  128. }
  129. }
  130. }
  131.  
  132. return false;
  133. }
  134.  
  135. public function getFileNameToSave(File $file)
  136. {
  137. $nameArray = explode('/', $file->getPathname());
  138. return array_pop($nameArray);
  139. }
  140.  
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement