diff --git a/src/commands/PackageController.php b/src/commands/PackageController.php index 64d5e39..6c0ebbb 100644 --- a/src/commands/PackageController.php +++ b/src/commands/PackageController.php @@ -89,8 +89,6 @@ class PackageController extends Controller if (!$package) { $package = new Package(); $package->name = $name; - } else { - $package->checkUrl = false; } if (!$package->url) $package->url = $url; diff --git a/src/components/Git.php b/src/components/Git.php index c0df5a7..64e1b32 100644 --- a/src/components/Git.php +++ b/src/components/Git.php @@ -34,6 +34,7 @@ class Git 'https://' => $file ? 'https://raw.' : 'https://', 'http://' => $file ? 'https://raw.' : 'https://', 'git://' => $file ? 'https://raw.' : 'https://', + 'github.com' => $file ? 'githubusercontent.com' : 'github.com', '.git' => $file ? '/master/' . $file : '', ]); } diff --git a/src/controllers/ModController.php b/src/controllers/ModController.php index 87e0f04..102e092 100644 --- a/src/controllers/ModController.php +++ b/src/controllers/ModController.php @@ -85,8 +85,11 @@ class ModController extends Controller $model = $this->findModel($name); $model->harvestModInfo(); if ($model->getDirtyAttributes()) { - $model->save(); - Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Mod has been updated from remote repository.')); + if ($model->save()) { + Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Mod has been updated from remote repository.')); + } else { + Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Error while saving mod updates.')); + } } else { Yii::$app->getSession()->setFlash('info', Yii::t('app', 'Mod has been updated from remote repository, however no change was detected.')); } diff --git a/src/models/Package.php b/src/models/Package.php index e8ebce3..0af6679 100644 --- a/src/models/Package.php +++ b/src/models/Package.php @@ -46,11 +46,6 @@ class Package extends ActiveRecord */ public $serialized = false; - /** - * @var bool - */ - public $checkUrl = true; - /** * @inheritdoc */ @@ -73,7 +68,7 @@ class Package extends ActiveRecord [['name'], 'match', 'pattern' => '/^[-a-z0-9_]+$/', 'message' => '{attribute} can only contain lowercase letters, numbers, "_" and "-"'], [['url'], 'match', 'pattern' => '%(git|http(s)?)(:(//)?)([\w./\-~]+)(\.git)%', 'message' => '{attribute} must be a valid git endpoint.'], [['url'], function ($attribute) { - if ($this->checkUrl && Git::getFile($this->$attribute) === false) { + if ($this->isNewRecord && Git::getFile($this->$attribute) === false) { $this->addError($attribute, 'Could not fetch remote repository.'); } }], @@ -232,7 +227,7 @@ class Package extends ActiveRecord if ($this->bower) { // set fields from bower if (isset($this->bower['description'])) { - $this->description = substr($this->bower['description'], 0, 140); + $this->description = $this->bower['description']; } if (isset($this->bower['homepage'])) { $this->homepage = $this->bower['homepage']; @@ -268,7 +263,7 @@ class Package extends ActiveRecord } if ($repo) { if (isset($repo['description'])) { - $this->description = substr($repo['description'], 0, 140); + $this->description = $repo['description']; } if (isset($repo['homepage'])) { $this->homepage = $repo['homepage']; @@ -282,7 +277,25 @@ class Package extends ActiveRecord } } - // update authors + // fetch screenshot + if (!$this->screenshots) { + $file = Git::getUrl($this->url, 'screenshot.png'); + if (strpos(@get_headers($file)[0], '200')) { + $this->screenshots = [$file]; + } + } + + // fetch description + if (!$this->description) { + $this->description = Git::getFile($this->url, 'description.txt'); + } + + // trim description + if (strlen($this->description) > 140) { + $this->description = substr($this->description, 0, 137) . '...'; + } + + // fetch authors if (!$this->authors && !strpos($this->url, 'repo.or.cz')) { $url = parse_url(Git::getUrl($this->url)); $path = explode('/', trim($url['path'], '/'));