Kaydet (Commit) 1c7be4be authored tarafından mertcelen's avatar mertcelen

Extension auto download/install supported added (WIP)

üst 6775f98a
......@@ -13,6 +13,8 @@ use Illuminate\Http\Response;
use Illuminate\Support\Str;
use Illuminate\View\View;
use Illuminate\Support\Facades\Validator;
use App\Jobs\ExtensionUpdaterJob;
use Illuminate\Contracts\Bus\Dispatcher;
/**
* Class MainController
......@@ -144,6 +146,21 @@ class MainController extends Controller
);
}
}
list($error,$new) = self::setupNewExtension($zipFile);
if($error){
return $error;
}
system_log(3, "EXTENSION_UPLOAD_SUCCESS", [
"extension_id" => $new->id,
]);
return respond("Eklenti Başarıyla yüklendi.", 200);
}
public function setupNewExtension($zipFile)
{
// Initialize Zip Archive Object to use it later.
$zip = new ZipArchive();
......@@ -245,12 +262,7 @@ class MainController extends Controller
"db.json;
"
);
system_log(3, "EXTENSION_UPLOAD_SUCCESS", [
"extension_id" => $new->id,
]);
return respond("Eklenti Başarıyla yüklendi.", 200);
return [null,$new];
}
public function newExtension()
......@@ -376,4 +388,27 @@ class MainController extends Controller
}
return respond('Sıralamalar güncellendi', 200);
}
public function autoUpdateExtension()
{
$json = json_decode(file_get_contents(storage_path('extension_updates')),true);
$collection = collect($json);
$obj = $collection->where('extension_id',request("extension_id"))->first();
if(!$obj){
return respond("Eklenti Bulunamadı",201);
}
$job = (new ExtensionUpdaterJob(
request("extension_id"),
$obj["versionCode"],
$obj["downloadLink"],
true
))->onQueue('system_updater');
// Dispatch job right away.
$job_id = app(Dispatcher::class)->dispatch($job);
return respond("Talebiniz başarıyla alındı, eklenti güncellendiğinde bildirim alacaksınız.");
}
}
......@@ -47,6 +47,9 @@ class MainController extends Controller
public function API()
{
if(extension()->status == "0"){
return respond("Eklenti şu an güncelleniyor, lütfen birazdan tekrar deneyin.",201);
}
$page = request('target_function')
? request('target_function')
: 'index';
......
......@@ -607,4 +607,9 @@ class SettingsController extends Controller
return respond("Fonksiyon Silindi.", 200);
}
public function getExtensionUpdates()
{
return respond(json_decode(file_get_contents(storage_path('extension_updates'))));
}
}
......@@ -53,6 +53,20 @@ Route::get(
->name('extension_server_settings_page')
->middleware(['server', 'extension']);
Route::post(
'/ayarlar/eklentiGuncellemeleri',
'Extension\SettingsController@getExtensionUpdates'
)
->name('get_extension_updates')
->middleware('admin');
Route::post(
'/ayarlar/eklentiGuncelle',
'Extension\MainController@autoUpdateExtension'
)
->name('update_extension_auto')
->middleware('admin');
// Extension Server Settings
Route::post(
'/ayarlar/{extension_id}/{server_id}',
......
......@@ -80,6 +80,7 @@ class MarketController extends Controller
}
$json = json_decode((string) $response->getBody());
$collection = collect($json);
$fileToWrite = [];
for($i=0; $i < count($params); $i++){
$obj = $collection->where('packageName',$params[$i]["packageName"])->first();
if(!$obj){
......@@ -96,9 +97,22 @@ class MarketController extends Controller
// Dispatch job right away.
$job_id = app(Dispatcher::class)->dispatch($job);
array_push($fileToWrite,[
"name" => substr($params[$i]["packageName"],6),
"currentVersion" => $params[$i]["currentVersion"],
"newVersion" => $obj["version"]["versionName"],
"downloadLink" => $obj["platforms"][0]["downloadLink"],
"versionCode" => $obj["version"]["versionCode"],
"changeLog" => $obj["version"]["versionDescription"],
"extension_id" => $params[$i]["extension_id"]
]);
}
}
}
if(count($fileToWrite)){
file_put_contents(storage_path("extension_updates"),json_encode($fileToWrite),JSON_PRETTY_PRINT);
}
return respond($params);
}
......
......@@ -11,6 +11,7 @@ use App\AdminNotification;
use Illuminate\Queue\SerializesModels;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use App\Http\Controllers\Extension\MainController;
class ExtensionUpdaterJob implements ShouldQueue
{
......@@ -18,17 +19,19 @@ class ExtensionUpdaterJob implements ShouldQueue
private $extension;
private $download;
private $version_code;
private $forceUpdate;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($extension_id,$version_code, $download)
public function __construct($extension_id,$version_code, $download,$forceUpdate = false)
{
$this->extension = Extension::find($extension_id);
$this->version_code = $version_code;
$this->download = $download;
$this->forceUpdate = $forceUpdate;
}
/**
......@@ -40,9 +43,30 @@ class ExtensionUpdaterJob implements ShouldQueue
{
$downloadPath = "/tmp/" . $this->extension->id . "-" . $this->version_code;
$exists = trim(shell_exec("[ -e '$downloadPath' ] && echo 1 || echo 0"));
if($exists == "1"){
return true;
$flag = true;
if($exists != "1"){
$flag = self::downloadFile($downloadPath);
}
if($this->forceUpdate){
$controller = new MainController();
list($flag,$extension) = $controller->setupNewExtension($downloadPath);
AdminNotification::create([
"title" => $this->extension->display_name . " eklentisi güncellendi!",
"type" => "extension_update",
"message" =>
$this->extension->display_name . " eklentisinin yeni bir sürümü indirildi ve yüklendi. İncelemek için için <a href='" . route('settings') . "#extensions" . "'>tıklayınız.</a>",
"level" => 3,
]);
self::updateUpdatesFile();
}
return $flag;
}
private function downloadFile($downloadPath)
{
$client = new Client([
"headers" => [
"Authorization" => "Bearer " . env("MARKET_ACCESS_TOKEN")
......@@ -52,16 +76,24 @@ class ExtensionUpdaterJob implements ShouldQueue
$resource = fopen($downloadPath, 'w');
$client->request('GET', $this->download, ['sink' => $resource]);
if(is_file($downloadPath)){
AdminNotification::create([
"title" => $this->extension->display_name . " Güncellemesi İndirildi!",
"type" => "extension_update",
"message" =>
$this->extension->display_name . " eklentisinin yeni bir sürümü indirildi, yüklemek için <a href='" . route('extensions_settings') . "?showUpdates" . "'>tıklayınız.</a>",
"level" => 3,
]);
return true;
}else{
return false;
}
}
private function updateUpdatesFile()
{
$json = json_decode(file_get_contents(storage_path('extension_updates')),true);
for($i = 0 ; $i < count($json); $i++){
if($json[$i]["extension_id"] = $this->extension->id){
unset($json[$i]);
}
}
if(count($json)){
file_put_contents(storage_path('extension_updates'),json_encode($json));
}else{
unlink(storage_path('extension_updates'));
}
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddStatusToExtensions extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('extensions', function (Blueprint $table) {
$table->string('status')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('extensions', function (Blueprint $table) {
//
});
}
}
......@@ -14,6 +14,9 @@
"target_id" => "newExtension",
"text" => "Yeni"
])
@endif
@if(is_file(storage_path("extension_updates")))
<button class="btn btn-warning" onclick=showExtensionUpdates()>{{__("Güncellemeleri Yükle")}}</button>
@endif
<div class="float-sm-right">
<button data-toggle="tooltip" title="Ayarlar" class="btn btn-primary" onclick="openSettingsModal()"><i class="fa fa-cogs"></i></button>
......@@ -55,6 +58,36 @@
"submit_text" => "Kaydet"
])
@component('modal-component',[
"id" => "extensionUpdatesModal",
"title" => "Eklenti Güncellemeleri"
])
<div id="extensionUpdatesWrapper">
<div class="row">
<div class="col-md-3">
<ul class="list-group" id="extensionUpdatesList">
</ul>
</div>
<div class="col-md-9">
<div class="row">
<div class="col-md-9">
<h3 id="extensionUpdateName"></h3>
</div>
<div class="col-md-3">
<h2 id="extensionNewVersion"></h2>
</div>
</div>
<div class="row">
<div class="col-md-12" id="extensionChangeLogWrapper">
</div>
</div><br>
<button class="btn btn-success" onclick="requestExtensionUpdate()">{{__("Şimdi Güncelle")}}</button>
</div>
</div>
</div>
@endcomponent
@include('modal',[
"id"=>"extensionUpload",
"title" => "Eklenti Yükle",
......@@ -119,6 +152,49 @@
<script>
$('input[name=ext_count]').val('{{getExtensionViewCount()}}');
var extensionUpdates = [];
function showExtensionUpdates(){
showSwal('{{__("Okunuyor...")}}','info');
request('{{route('get_extension_updates')}}',new FormData(),function(success){
let json = JSON.parse(success);
let element = $("#extensionUpdatesList");
element.html("");
$.each(json.message,function (index,current){
element.append("<li id='extension_" + current["name"] + "_button' onclick='setExtensionUpdateData(\"" + current["name"] + "\")' class='list-group-item'>" + current["name"] + "</li>");
extensionUpdates[current["name"]] = current;
});
if(json.message.length == 1){
setExtensionUpdateData(json.message[0]["name"]);
}
$("#extensionUpdatesModal").modal('show');
Swal.close();
}, function(error){
let json = JSON.parse(error);
showSwal(json.message,'error',2000);
});
}
function setExtensionUpdateData(target){
$("#extensionNewVersion").text(extensionUpdates[target]["newVersion"]);
$("#extensionUpdateName").text(target);
$("#extensionUpdatesList li").removeClass("active");
$("#extensionChangeLogWrapper").html(extensionUpdates[target]["changeLog"]);
$("#extension_" + target + "_button").addClass('active');
}
function requestExtensionUpdate(){
let extension_id = extensionUpdates[$("#extensionUpdateName").text()]["extension_id"];
let form = new FormData();
form.append('extension_id', extension_id);
request("{{route('update_extension_auto')}}",form, function (success){
let json = JSON.parse(success);
showSwal(json.message,'success',2000);
},function(error){
let json = JSON.parse(error);
showSwal(json.message,'error',2000);
});
}
function downloadFile(form){
window.location.assign('/indir/eklenti/' + form.getElementsByTagName('select')[0].value);
setTimeout(function(){
......
......@@ -312,6 +312,7 @@
</div>
</div>
<script>
customRequestData["extension_id"] = "{{extension()->id}}";
function editPage(element){
var page = $(element).find("#name").text();
partialPageRequest(location.protocol+'//'+location.host+location.pathname + "/" + page);
......
......@@ -7,7 +7,7 @@ if(!$item){
if(auth()->user()->isAdmin() && \App\AdminNotification::find(request('notification_id'))->exists()){
header("Location: " . route('system_notification',[
"notification_id" => request('notification_id')
]), true);
]), true);
exit();
}else{
return redirect()->back();
......
......@@ -14,7 +14,7 @@
<a class="nav-link active" data-toggle="tab" href="#users" aria-selected="true">{{__("Kullanıcı Ayarları")}}</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#extensions" aria-selected="true">{{__("Eklentiler")}}</a>
<a id="extensionNavLink" class="nav-link" data-toggle="tab" href="#extensions" aria-selected="true">{{__("Eklentiler")}} @if(is_file(storage_path("extension_updates"))) <span style="color:green" class="blinking">*</span> @endif</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#roles" onclick="getRoleList()" aria-selected="true">{{__("Rol Grupları")}}</a>
......@@ -389,6 +389,15 @@
</div>
</div>
</div>
<style>
.blinking {
animation: blinker 1s linear infinite;
}
@keyframes blinker {
50% { opacity: 0; }
}
</style>
@include('modal',[
"id"=>"add_user",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment