Loading .gitignore +1 −0 Original line number Diff line number Diff line /composer.lock /vendor .idea .php_cs.cache No newline at end of file src/Helpers/Build.php +63 −0 Original line number Diff line number Diff line Loading @@ -41,6 +41,7 @@ private $uid = null; private $size = ''; private $logger = null; private $migrationFilePath = ''; /** * Constructor of the Build class. Loading Loading @@ -74,6 +75,7 @@ $tokens = $this->removeTrailingDashes($tokens); $this->filePath = $physicalPath . '/' . $fileName; $this->migrationFilePath = $physicalPath . '/migration_paths.json'; $this->channel = $this->_getChannel(str_replace(range(0, 9), '', $tokens[4]), $tokens[1], $tokens[2]); $this->filename = $fileName; Loading Loading @@ -170,6 +172,24 @@ return $ret; } /** * Check version of current build against given build(returns false if lesser version) * @param object An object representing current build * @return boolean */ public function checkVersion($currentBuild) { if (empty($currentBuild)) { return true; } // Valid build if no current build specified if ($currentBuild->getTimestamp() > $this->getTimestamp()) { return false; // Invalid build if timestamp older } return(version_compare($currentBuild->getVersion(), $this->getVersion(), "<")); } /** * Return the MD5 value of the current build * @param string $path The path of the file Loading Loading @@ -207,6 +227,36 @@ return $this->size; } /** * Get isUpgradeSupported parameter * @return boolean */ /* In case android version same(even if migration path not explicitly specified), or migration rules invalid, return $isSameVersion Otherwise check if build version is contained in migration paths for given version Typical migration_paths.json file would look like: { "9": [9,10] // i.e. upgrades from 9 to 9 and 9 to 10 are permitted } */ public function getIsUpgradeSupported($compareVersion) { $isSameVersion = boolval(!strcmp($this->androidVersion, $compareVersion)); // Check if Android version is same $migrationContents = file_get_contents($this->migrationFilePath); if (!empty($migrationContents)) { $migrations = json_decode($migrationContents, true); // Contains migration rules if (!empty($migrations[$compareVersion]) && is_array($migrations[$compareVersion])) { return in_array($this->androidVersion, $migrations[$compareVersion]); } } return $isSameVersion; } /** * Get a unique id of the current build * @return string A unique id Loading Loading @@ -318,6 +368,19 @@ $this->filePath.'.prop' : 'zip://'.$this->filePath.'#system/build.prop'; } /* Utility / Internal */ /** * Parse version string(semantic versioning) and return array with major and minor version numbers * @param string A string representing version to parse * @return array */ private function parseSemVer($versionString) { $versionArray = explode(".", $versionString); return array_map("intval", $versionArray); } /* Utility / Internal */ /** Loading src/Helpers/Builds.php +70 −54 Original line number Diff line number Diff line Loading @@ -27,7 +27,8 @@ use \Flight; use \JX\CmOta\Helpers\Build; class Builds { class Builds { // This will contain the build list based on the current request private $builds = array(); Loading @@ -41,7 +42,8 @@ /** * Constructor of the Builds class. */ public function __construct($logger) { public function __construct($logger) { $this->logger = $logger; // Set required paths for properly builds Urls later Flight::cfg()->set('buildsPath', Flight::cfg()->get('basePath') . '/builds/full'); Loading @@ -58,14 +60,25 @@ * Return a valid response list of builds available based on the current request * @return array An array preformatted with builds */ public function get() { public function get() { $ret = array(); foreach ($this->builds as $build) { if (!is_null($this->currentBuild) && strcmp($this->currentBuild->getAndroidVersion(), $build->getAndroidVersion()) != 0) { $this->logger->info($build->getIncremental().' ignored as Android version is not the same'); if (!$build->checkVersion($this->currentBuild)) { $this->logger->info($build->getIncremental().' ignored as build version is older than current version'); continue; } $isUpgradeSupported = false; if (!is_null($this->currentBuild)) { $currentBuildVersion = $this->currentBuild->getAndroidVersion(); $isUpgradeSupported = $build->getIsUpgradeSupported($currentBuildVersion); if (!$isUpgradeSupported) { $this->logger->info($build->getIncremental().' ignored as upgrade path is not permitted'); continue; } } if (preg_match("/disabled/i", $build->getFilename())) { continue; Loading @@ -91,18 +104,19 @@ 'android_version' => $build->getAndroidVersion(), 'id' => $build->getUid(), 'size' => $build->getSize(), 'is_upgrade_supported' => $isUpgradeSupported )); } return $ret; } /** * Set a custom set of POST data. Useful to hack the flow in case the data doesn't come within the body of the HTTP request * @param array An array structured as POST data * @return void */ public function setPostData( $customData ){ public function setPostData($customData) { $this->postData = $customData; $this->builds = array(); $this->getBuilds(); Loading @@ -112,7 +126,8 @@ * Return a valid response of the delta build (if available) based on the current request * @return array An array preformatted with the delta build */ public function getDelta() { public function getDelta() { $ret = false; $source = $this->postData['source_incremental']; Loading Loading @@ -141,7 +156,8 @@ /* Utility / Internal */ private function getBuilds() { private function getBuilds() { // Get physical paths of where the files resides $path = Flight::cfg()->get('realBasePath') . '/builds/full'; // Get subdirs Loading @@ -158,7 +174,6 @@ $files = scandir($dir); if (count($files) > 0) { foreach ($files as $file) { $filename = pathinfo($file, PATHINFO_FILENAME); if (preg_match("/IMG/i", $filename)) { Loading @@ -179,13 +194,14 @@ $build = apcu_fetch($file); // If not found there, we have to find it with the old fashion method... if ( $build === FALSE ) { if ($build === false) { $build = new Build($file, $dir, $this->logger); // ...and then save it for 72h until it expires again apcu_store($file, $build, 72*60*60); } } else } else { $build = new Build($file, $dir, $this->logger); } if ($build->isValid($this->postData['params'])) { array_push($this->builds, $build); Loading Loading
.gitignore +1 −0 Original line number Diff line number Diff line /composer.lock /vendor .idea .php_cs.cache No newline at end of file
src/Helpers/Build.php +63 −0 Original line number Diff line number Diff line Loading @@ -41,6 +41,7 @@ private $uid = null; private $size = ''; private $logger = null; private $migrationFilePath = ''; /** * Constructor of the Build class. Loading Loading @@ -74,6 +75,7 @@ $tokens = $this->removeTrailingDashes($tokens); $this->filePath = $physicalPath . '/' . $fileName; $this->migrationFilePath = $physicalPath . '/migration_paths.json'; $this->channel = $this->_getChannel(str_replace(range(0, 9), '', $tokens[4]), $tokens[1], $tokens[2]); $this->filename = $fileName; Loading Loading @@ -170,6 +172,24 @@ return $ret; } /** * Check version of current build against given build(returns false if lesser version) * @param object An object representing current build * @return boolean */ public function checkVersion($currentBuild) { if (empty($currentBuild)) { return true; } // Valid build if no current build specified if ($currentBuild->getTimestamp() > $this->getTimestamp()) { return false; // Invalid build if timestamp older } return(version_compare($currentBuild->getVersion(), $this->getVersion(), "<")); } /** * Return the MD5 value of the current build * @param string $path The path of the file Loading Loading @@ -207,6 +227,36 @@ return $this->size; } /** * Get isUpgradeSupported parameter * @return boolean */ /* In case android version same(even if migration path not explicitly specified), or migration rules invalid, return $isSameVersion Otherwise check if build version is contained in migration paths for given version Typical migration_paths.json file would look like: { "9": [9,10] // i.e. upgrades from 9 to 9 and 9 to 10 are permitted } */ public function getIsUpgradeSupported($compareVersion) { $isSameVersion = boolval(!strcmp($this->androidVersion, $compareVersion)); // Check if Android version is same $migrationContents = file_get_contents($this->migrationFilePath); if (!empty($migrationContents)) { $migrations = json_decode($migrationContents, true); // Contains migration rules if (!empty($migrations[$compareVersion]) && is_array($migrations[$compareVersion])) { return in_array($this->androidVersion, $migrations[$compareVersion]); } } return $isSameVersion; } /** * Get a unique id of the current build * @return string A unique id Loading Loading @@ -318,6 +368,19 @@ $this->filePath.'.prop' : 'zip://'.$this->filePath.'#system/build.prop'; } /* Utility / Internal */ /** * Parse version string(semantic versioning) and return array with major and minor version numbers * @param string A string representing version to parse * @return array */ private function parseSemVer($versionString) { $versionArray = explode(".", $versionString); return array_map("intval", $versionArray); } /* Utility / Internal */ /** Loading
src/Helpers/Builds.php +70 −54 Original line number Diff line number Diff line Loading @@ -27,7 +27,8 @@ use \Flight; use \JX\CmOta\Helpers\Build; class Builds { class Builds { // This will contain the build list based on the current request private $builds = array(); Loading @@ -41,7 +42,8 @@ /** * Constructor of the Builds class. */ public function __construct($logger) { public function __construct($logger) { $this->logger = $logger; // Set required paths for properly builds Urls later Flight::cfg()->set('buildsPath', Flight::cfg()->get('basePath') . '/builds/full'); Loading @@ -58,14 +60,25 @@ * Return a valid response list of builds available based on the current request * @return array An array preformatted with builds */ public function get() { public function get() { $ret = array(); foreach ($this->builds as $build) { if (!is_null($this->currentBuild) && strcmp($this->currentBuild->getAndroidVersion(), $build->getAndroidVersion()) != 0) { $this->logger->info($build->getIncremental().' ignored as Android version is not the same'); if (!$build->checkVersion($this->currentBuild)) { $this->logger->info($build->getIncremental().' ignored as build version is older than current version'); continue; } $isUpgradeSupported = false; if (!is_null($this->currentBuild)) { $currentBuildVersion = $this->currentBuild->getAndroidVersion(); $isUpgradeSupported = $build->getIsUpgradeSupported($currentBuildVersion); if (!$isUpgradeSupported) { $this->logger->info($build->getIncremental().' ignored as upgrade path is not permitted'); continue; } } if (preg_match("/disabled/i", $build->getFilename())) { continue; Loading @@ -91,18 +104,19 @@ 'android_version' => $build->getAndroidVersion(), 'id' => $build->getUid(), 'size' => $build->getSize(), 'is_upgrade_supported' => $isUpgradeSupported )); } return $ret; } /** * Set a custom set of POST data. Useful to hack the flow in case the data doesn't come within the body of the HTTP request * @param array An array structured as POST data * @return void */ public function setPostData( $customData ){ public function setPostData($customData) { $this->postData = $customData; $this->builds = array(); $this->getBuilds(); Loading @@ -112,7 +126,8 @@ * Return a valid response of the delta build (if available) based on the current request * @return array An array preformatted with the delta build */ public function getDelta() { public function getDelta() { $ret = false; $source = $this->postData['source_incremental']; Loading Loading @@ -141,7 +156,8 @@ /* Utility / Internal */ private function getBuilds() { private function getBuilds() { // Get physical paths of where the files resides $path = Flight::cfg()->get('realBasePath') . '/builds/full'; // Get subdirs Loading @@ -158,7 +174,6 @@ $files = scandir($dir); if (count($files) > 0) { foreach ($files as $file) { $filename = pathinfo($file, PATHINFO_FILENAME); if (preg_match("/IMG/i", $filename)) { Loading @@ -179,13 +194,14 @@ $build = apcu_fetch($file); // If not found there, we have to find it with the old fashion method... if ( $build === FALSE ) { if ($build === false) { $build = new Build($file, $dir, $this->logger); // ...and then save it for 72h until it expires again apcu_store($file, $build, 72*60*60); } } else } else { $build = new Build($file, $dir, $this->logger); } if ($build->isValid($this->postData['params'])) { array_push($this->builds, $build); Loading