Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class FileShardingCommand
- extends EConsoleCommand
- {
- public function actionIndex()
- {
- static $oldId = null;
- while (true)
- {
- $additionalCond = '';
- if ($oldId)
- {
- $additionalCond = " AND id > $oldId";
- }
- $sql = <<<SQL
- SELECT * FROM tbl_file WHERE migration_status is null or migration_status = '' $additionalCond;
- SQL;
- $file = File::model()->findBySql($sql);
- $oldId = $file->id;
- $this->_log("Took file $file->id");
- if (!$file)
- {
- $this->_log('Done.');
- exit;
- }
- $this->_log("Took file $file->id");
- $oldFilePath = $file->getPathname();
- $transaction = \Yii::app()->db->beginTransaction();
- try
- {
- $fileName = $this->getFileName($file->id);
- $filePath = $this->getFilePath($fileName);
- if ($newFilePath = $this->saveFile($file, $filePath))
- {
- $file->prepareUpdateList()
- ->setPlainField('migration_status', 'done')
- ->setPlainField('spec', $this->makeSpec($newFilePath))
- ->apply();
- $transaction->commit();
- $this->_log("Saved to: $newFilePath");
- }
- else
- {
- $this->_log('Skip file.');
- $transaction->commit();
- continue;
- }
- if ($tmp = str_replace('.' . pathinfo($oldFilePath)['extension'], '', $oldFilePath))
- {
- @unlink($tmp);
- $this->_log("Unlinked tmp file: $tmp");
- }
- unlink($oldFilePath);
- $this->_log("Unlinked old file: $oldFilePath");
- }
- catch (Throwable $e)
- {
- $transaction->rollback();
- throw $e;
- }
- }
- }
- public function makeSpec(string $newFilePath): string
- {
- return str_replace(\Yii::app()->env->get(\env\files_path), 'file:%files_path%', $newFilePath);
- }
- public function getFileName(int $id): string
- {
- $id = (string)$id;
- $idLength = strlen($id);
- $fileNameLength = 12;
- $zeroNum = $fileNameLength - $idLength;
- if (0 === $zeroNum)
- {
- return strrev($id);
- }
- return strrev(str_repeat("0", $zeroNum) . $id);
- }
- public function getFilePath(string $fileName): string
- {
- return \Yii::app()->env->get(\env\files_path) . DIRECTORY_SEPARATOR .
- substr($fileName, 0, 3) . DIRECTORY_SEPARATOR .
- substr($fileName, 3, 3) . DIRECTORY_SEPARATOR .
- substr($fileName, 6, 3) . DIRECTORY_SEPARATOR .
- substr($fileName, 9, 3);
- }
- /**
- * @param File $file
- * @param string $newFilePath
- * @return bool|string
- */
- public function saveFile(File $file, string $newFilePath)
- {
- $oldFilePath = $file->getPathname();
- if (!file_exists($oldFilePath))
- {
- $file->prepareUpdateList()
- ->setPlainField('migration_status', 'error')
- ->apply();
- return false;
- }
- $oldHash = hash_file('md5', $file->getHandler()->getPathname());
- if (!is_dir($newFilePath) && !mkdir($newFilePath, '0777', true))
- {
- throw new RuntimeException("не удалось создать создать каталог файл $newFilePath");
- }
- $newFilePath = $newFilePath . DIRECTORY_SEPARATOR . $this->getFileNameToSave($file);
- if (!copy($oldFilePath, $newFilePath))
- {
- throw new RuntimeException('не скопировалось');
- }
- if (hash_file('md5', $newFilePath) !== $oldHash)
- {
- throw new RuntimeException('хэш не совпадает у файла #' . $file->id);
- }
- return $newFilePath;
- }
- public function getFileNameToSave(File $file)
- {
- $nameArray = explode('/', $file->getPathname());
- return array_pop($nameArray);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement