From 8499d100ff3f257a846ece39e4013c81556a4ce0 Mon Sep 17 00:00:00 2001 From: KodeStar Date: Tue, 15 Mar 2022 18:19:01 +0000 Subject: [PATCH] More work on apps --- app/Application.php | 28 ++++++ app/Http/Controllers/ItemController.php | 89 +++++++++---------- app/Item.php | 3 +- app/SupportedApps.php | 20 ++--- .../2022_03_15_140911_add_appid_to_items.php | 32 +++++++ resources/views/items/form.blade.php | 2 +- resources/views/items/scripts.blade.php | 11 +-- 7 files changed, 121 insertions(+), 64 deletions(-) create mode 100644 database/migrations/2022_03_15_140911_add_appid_to_items.php diff --git a/app/Application.php b/app/Application.php index a899d37f..30aeb009 100644 --- a/app/Application.php +++ b/app/Application.php @@ -66,10 +66,38 @@ class Application extends Model return $list; } + public static function getApp($appid) + { + $localapp = Application::where('appid', $appid)->first(); + $app = self::single($appid); + + $application = ($localapp) ? $localapp : new Application; + + if(!file_exists(app_path('SupportedApps/'.className($app->name)))) { + SupportedApps::getFiles($app); + SupportedApps::saveApp($app, $application); + } else { + // check if there has been an update for this app + if($localapp) { + if($localapp->sha !== $app->sha) { + SupportedApps::getFiles($app); + $app = SupportedApps::saveApp($app, $application); + } + } else { + SupportedApps::getFiles($app); + $app = SupportedApps::saveApp($app, $application); + + } + } + return $app; + + } + public static function single($appid) { $apps = self::apps(); $app = $apps->where('appid', $appid)->first(); + if ($app === null) return null; $classname = preg_replace('/[^\p{L}\p{N}]/u', '', $app->name); $app->class = '\App\SupportedApps\\'.$classname.'\\'.$classname; return $app; diff --git a/app/Http/Controllers/ItemController.php b/app/Http/Controllers/ItemController.php index 74db8913..17a8b42e 100644 --- a/app/Http/Controllers/ItemController.php +++ b/app/Http/Controllers/ItemController.php @@ -145,15 +145,9 @@ class ItemController extends Controller } - /** - * Store a newly created resource in storage. - * - * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response - */ - public function store(Request $request) + public function storelogic($request, $id = null) { - // + $application = Application::single($request->input('appid')); $validatedData = $request->validate([ 'title' => 'required|max:255', 'url' => 'required', @@ -164,6 +158,14 @@ class ItemController extends Controller $request->merge([ 'icon' => $path ]); + } elseif(strpos($request->input('icon'), 'http') === 0) { + $icon = $application->icon; + $contents = file_get_contents($request->input('icon')); + $path = 'icons/'.$icon; + Storage::disk('public')->put($path, $contents); + $request->merge([ + 'icon' => $path + ]); } $config = Item::checkConfig($request->input('config')); @@ -180,14 +182,28 @@ class ItemController extends Controller } - //die(print_r($request->input('config'))); - - $item = Item::create($request->all()); + if($id === null) { + $item = Item::create($request->all()); + } else { + $item = Item::find($id); + $item->update($request->all()); + } - //Search::storeSearchProvider($request->input('class'), $item); $item->parents()->sync($request->tags); + } + + /** + * Store a newly created resource in storage. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\Response + */ + public function store(Request $request) + { + $this->storelogic($request); + $route = route('dash', []); return redirect($route) ->with('success', __('app.alert.success.item_created')); @@ -232,39 +248,7 @@ class ItemController extends Controller */ public function update(Request $request, $id) { - $validatedData = $request->validate([ - 'title' => 'required|max:255', - 'url' => 'required', - ]); - //die(print_r($request->all())); - if($request->hasFile('file')) { - $path = $request->file('file')->store('icons'); - $request->merge([ - 'icon' => $path - ]); - } - - $config = Item::checkConfig($request->input('config')); - $current_user = User::currentUser(); - $request->merge([ - 'description' => $config, - 'user_id' => $current_user->id - ]); - - if($request->input('class') === 'null') { - $request->merge([ - 'class' => null, - ]); - } - - - $item = Item::find($id); - $item->update($request->all()); - - //Search::storeSearchProvider($request->input('class'), $item); - - $item->parents()->sync($request->tags); - + $this->storelogic($request, $id); $route = route('dash', []); return redirect($route) ->with('success',__('app.alert.success.item_updated')); @@ -320,6 +304,8 @@ class ItemController extends Controller { $output = []; $appid = $request->input('app'); + + if($appid === "null") return null; /*$appname = $request->input('app'); //die($appname); @@ -340,9 +326,20 @@ class ItemController extends Controller } else { $output['config'] = null; }*/ + + $output['config'] = null; + $output['custom'] = null; + $app = Application::single($appid); $output = (array)$app; - $output['config'] = null; + + if((boolean)$app->enhanced === true) { + if(!isset($app->config)) { // class based config + $appdetails = Application::getApp($appid); + $output['custom'] = className($appdetails->name).'.config'; + } + } + $output['colour'] = ($app->tile_background == 'light') ? '#fafbfc' : '#161b1f'; $output['iconview'] = 'https://raw.githubusercontent.com/linuxserver/Heimdall-Apps/master/' . preg_replace('/[^\p{L}\p{N}]/u', '', $app->name) . '/' . $app->icon; diff --git a/app/Item.php b/app/Item.php index 05f5c3dd..47999204 100644 --- a/app/Item.php +++ b/app/Item.php @@ -30,7 +30,7 @@ class Item extends Model // protected $fillable = [ - 'title', 'url', 'colour', 'icon', 'description', 'pinned', 'order', 'type', 'class', 'user_id' + 'title', 'url', 'colour', 'icon', 'description', 'pinned', 'order', 'type', 'class', 'user_id', 'appid' ]; /** @@ -53,6 +53,7 @@ class Item extends Model public static function checkConfig($config) { + // die(print_r($config)); if(empty($config)) { $config = null; } else { diff --git a/app/SupportedApps.php b/app/SupportedApps.php index ba3bcd4e..3b09aef8 100644 --- a/app/SupportedApps.php +++ b/app/SupportedApps.php @@ -122,7 +122,11 @@ abstract class SupportedApps public static function getFiles($app) { - $zipurl = $app->files; + $apps = json_decode(file_get_contents('https://apps.heimdall.site/list')); + $collect = collect($apps->apps); + $collapp = $collect->where('appid', $app->appid)->first(); + $zipurl = $collapp->files; + $client = new Client(['http_errors' => false, 'timeout' => 60, 'connect_timeout' => 15]); $res = $client->request('GET', $zipurl); @@ -139,20 +143,13 @@ abstract class SupportedApps $zip->extractTo(app_path('SupportedApps')); // place in the directory with same name $zip->close(); unlink($src); //Deleting the Zipped file + } else { + var_dump($x); } } public static function saveApp($details, $app) - { - if(!file_exists(storage_path('app/public/icons'))) { - mkdir(storage_path('app/public/icons'), 0777, true); - } - - $img_src = app_path('SupportedApps/'.className($details->name).'/'.$details->icon); - $img_dest = storage_path('app/public/icons/'.$details->icon); - //die("i: ".$img_src); - @copy($img_src, $img_dest); - + { $app->appid = $details->appid; $app->name = $details->name; $app->sha = $details->sha ?? null; @@ -168,6 +165,7 @@ abstract class SupportedApps $app->enhanced = $enhanced; $app->tile_background = $details->tile_background; $app->save(); + return $app; } } \ No newline at end of file diff --git a/database/migrations/2022_03_15_140911_add_appid_to_items.php b/database/migrations/2022_03_15_140911_add_appid_to_items.php new file mode 100644 index 00000000..debc08b2 --- /dev/null +++ b/database/migrations/2022_03_15_140911_add_appid_to_items.php @@ -0,0 +1,32 @@ +string('appid')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('items', function (Blueprint $table) { + $table->dropColumn(['appid']); + }); + } +} diff --git a/resources/views/items/form.blade.php b/resources/views/items/form.blade.php index 5e1cdc74..231669bc 100644 --- a/resources/views/items/form.blade.php +++ b/resources/views/items/form.blade.php @@ -27,7 +27,7 @@
- {!! Form::select('class', App\Application::applist(), null, array('class' => 'form-control config-item', 'id' => 'apptype', 'data-config' => 'type')) !!} + {!! Form::select('appid', App\Application::applist(), null, array('class' => 'form-control config-item', 'id' => 'apptype', 'data-config' => 'type')) !!}
diff --git a/resources/views/items/scripts.blade.php b/resources/views/items/scripts.blade.php index d7f30755..33e41e29 100644 --- a/resources/views/items/scripts.blade.php +++ b/resources/views/items/scripts.blade.php @@ -49,23 +49,24 @@ $('.tags').select2(); function appload(appvalue) { - if(appvalue == 'None') { + if(appvalue == 'null') { $('#sapconfig').html('').hide(); $('#tile-preview .app-icon').attr('src', '/img/heimdall-icon-small.png'); $('#appimage').html(""); + $('#sapconfig').html('').hide(); } else { $.post('{{ route('appload') }}', { app: appvalue }, function(data) { // Main details - $('#appimage').html(""); + $('#appimage').html(""); $('input[name=colour]').val(data.colour); - $('select[name=class]').val(data.appid); + $('select[name=appid]').val(data.appid); hueb.setColor( data.colour ); $('input[name=pinned]').prop('checked', true); // Preview details $('#tile-preview .app-icon').attr('src', data.iconview); $('#tile-preview .title').html(data.name); - if(data.config != null) { - $.get(base+'view/'+data.config, function(getdata) { + if(data.custom != null) { + $.get(base+'view/'+data.custom, function(getdata) { $('#sapconfig').html(getdata).show(); }); } else {