From 3159edc26cb61e5504420d50147f40f1135276f8 Mon Sep 17 00:00:00 2001 From: Philippe G Date: Sat, 3 Apr 2021 21:27:36 -0700 Subject: [PATCH] download using LMS proxy --- plugin/SqueezeESP32.zip | Bin 12232 -> 13548 bytes plugin/SqueezeESP32/Plugin.pm | 147 +++++++++++++++++++++++++++++++- plugin/SqueezeESP32/install.xml | 2 +- plugin/repo.xml | 4 +- 4 files changed, 149 insertions(+), 4 deletions(-) diff --git a/plugin/SqueezeESP32.zip b/plugin/SqueezeESP32.zip index ec4c9a218c8c7063a1d91196f59bc6d07fb015df..f9bb8cf0d894b7dea5be104c23a807ce806479fe 100644 GIT binary patch delta 3376 zcmZvecTf{r8pRWW^j;(aLTHiR%cDbhGzlFOYNP~^4kA?~^kxG>M?^q+FH%AWr8lK1 zN^c^O7Erp2@6GPc&f7cl{m#stbMJg}{yGD9@h((GdLUvZ002M+$gilSvL^$^V!g##aa2@Y+9NzJe|Lw6Wu`LuJx3*@R`m*j_bJb2OI5QpD{@~B7pQM& z2(WsNi?lW}CT8v+Xa#SyXeVTK>Q;oE`;pQxvn0=O!V++O_APC)Pt*HCpoPY2r-2aV zCdETJKM~ErM_(~hpSg@+om8L?!)p`+>f^*-w>E{>3bK5SjiJ%@_j-%eBJhgIt0L;Y z(GLjC?U+Epj|AEAyMz}W*ICqX=()vI#MFF&!#$t$dMgNzOgf^-70oZ2oiiai{1F*> z9C?u~%;pXO$>?`-rt4xgvxv6V@=z{G4;{9g&TSzDlMM4u&K2jx{u||0NCvb{l)uAj zWCapqwo)TaGIn_$F;V~kX&e9m`BmUX?tacLo}%6!(>KmcKg%9F2haXecdTff&4}9Z z$(Aw~ARc-KbQ{94)x5A5ibwQICc}piKbm&Wct07y0zJ0B&uHUy7SBZZiuV;2&};l6 zF-oS!Q_5oCvm8I1cCPAk?$$4%=U?{=U=o=8hPB$NjvoRcFAUE;hx+;4ncAbKUwLr( z2M*zHYxR>fdM$23aPp4}?iCN%k7^dti5CR}9`b$pIo}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VHYJ&0s??8V<8F?t^4|X-9j1jmO4uM~b zIuGpoRtTMU`B{+y+5{1MT>4u?VE)~SeZ-$dDQF9n*CW|)(Z9Vy!u==F|D^(B9Ib

addLogCategory({ 'description' => 'PLUGIN_SQUEEZEESP32', }); +use constant GITHUB_ASSET_URI => "https://api.github.com/repos/sle118/squeezelite-esp32/releases/assets/"; +use constant GITHUB_DOWNLOAD_URI => "https://github.com/sle118/squeezelite-esp32/releases/download/"; +my $FW_DOWNLOAD_ID_REGEX = qr|plugins/SqueezeESP32/firmware/(-?\d+)|; +my $FW_DOWNLOAD_REGEX = qr|plugins/SqueezeESP32/firmware/([-a-z0-9-/.]+\.bin)$|i; +my $FW_FILENAME_REGEX = qr/^squeezelite-esp32-.*\.bin(\.tmp)?$/; + # migrate 'eq' pref, as that's a reserved word and could cause problems in the future $prefs->migrateClient(1, sub { my ($cprefs, $client) = @_; @@ -48,7 +57,7 @@ sub initPlugin { $class->SUPER::initPlugin(@_); # no name can be a subset of others due to a bug in addPlayerClass Slim::Networking::Slimproto::addPlayerClass($class, 100, 'squeezeesp32-basic', { client => 'Plugins::SqueezeESP32::Player', display => 'Plugins::SqueezeESP32::Graphics' }); - Slim::Networking::Slimproto::addPlayerClass($class, 101, 'squeezeesp32-graphic', { client => 'Plugins::SqueezeESP32::Player', display => 'Slim::Display::NoDisplay' }); + Slim::Networking::Slimproto::addPlayerClass($class, 101, 'squeezeesp32-graphic', { client => 'Plugins::SqueezeESP32::Player', display => 'Slim::Display::NoDisplay' }); main::INFOLOG && $log->is_info && $log->info("Added class 100 and 101 for SqueezeESP32"); # register a command to set the EQ - without saving the values! Send params as single comma separated list of values @@ -58,6 +67,9 @@ sub initPlugin { Slim::Control::Request::subscribe( sub { onNotification(@_) }, [ ['newmetadata'] ] ); Slim::Control::Request::subscribe( sub { onNotification(@_) }, [ ['playlist'], ['open', 'newsong'] ]); Slim::Control::Request::subscribe( \&onStopClear, [ ['playlist'], ['stop', 'clear'] ]); + + Slim::Web::Pages->addRawFunction($FW_DOWNLOAD_ID_REGEX, \&handleFirmwareDownload); + Slim::Web::Pages->addRawFunction($FW_DOWNLOAD_REGEX, \&handleFirmwareDownloadDirect); } sub onStopClear { @@ -99,4 +111,137 @@ sub setEQ { $client->send_equalizer(\@eqParams); } +sub handleFirmwareDownload { + my ($httpClient, $response) = @_; + + my $request = $response->request; + + my $_errorDownloading = sub { + _errorDownloading($httpClient, $response, @_); + }; + + my $id; + if (!defined $request || !(($id) = $request->uri =~ $FW_DOWNLOAD_ID_REGEX)) { + return $_errorDownloading->(undef, 'Invalid request', $request->uri, 400); + } + + # this is the magic number used on the client to figure out whether the plugin does support download proxying + if ($id == -99) { + $response->code(204); + $response->header('Access-Control-Allow-Origin' => '*'); + + $httpClient->send_response($response); + return Slim::Web::HTTP::closeHTTPSocket($httpClient); + } + + Slim::Networking::SimpleAsyncHTTP->new( + sub { + my $http = shift; + my $content = eval { from_json( $http->content ) }; + + if (!$content || !ref $content) { + $@ && $log->error("Failed to parse response: $@"); + return $_errorDownloading->($http); + } + elsif (!$content->{browser_download_url} || !$content->{name}) { + return $_errorDownloading->($http, 'No download URL found'); + } + + downloadAndStreamFirmware($httpClient, $response, $content->{browser_download_url}, $content->{name}); + }, + $_errorDownloading, + { + timeout => 10, + cache => 1, + expires => 86400 + } + )->get(GITHUB_ASSET_URI . $id); + + return; +} + +sub handleFirmwareDownloadDirect { + my ($httpClient, $response) = @_; + + my $request = $response->request; + + my $_errorDownloading = sub { + _errorDownloading($httpClient, $response, @_); + }; + + my $path; + if (!defined $request || !(($path) = $request->uri =~ $FW_DOWNLOAD_REGEX)) { + return $_errorDownloading->(undef, 'Invalid request', $request->uri, 400); + } + + main::INFOLOG && $log->is_info && $log->info("Requesting firmware from: $path"); + + downloadAndStreamFirmware($httpClient, $response, GITHUB_DOWNLOAD_URI . $path); +} + +sub downloadAndStreamFirmware { + my ($httpClient, $response, $url, $name) = @_; + + my $_errorDownloading = sub { + _errorDownloading($httpClient, $response, @_); + }; + + $name ||= basename($url); + + if ($name !~ $FW_FILENAME_REGEX) { + return $_errorDownloading->(undef, 'Unexpected firmware image name: ' . $name, $url, 400); + } + + my $updatesDir = Slim::Utils::OSDetect::dirsFor('updates'); + my $firmwareFile = catfile($updatesDir, $name); + Slim::Utils::Misc::deleteFiles($updatesDir, $FW_FILENAME_REGEX, $firmwareFile); + + if (-f $firmwareFile) { + main::INFOLOG && $log->is_info && $log->info("Found cached firmware version"); + $response->code(200); + return Slim::Web::HTTP::sendStreamingFile($httpClient, $response, 'application/octet-stream', $firmwareFile, undef, 1); + } + + Slim::Networking::SimpleAsyncHTTP->new( + sub { + my $http = shift; + + if ($http->code != 200 || !-e "$firmwareFile.tmp") { + return $_errorDownloading->($http, $http->mess); + } + + rename "$firmwareFile.tmp", $firmwareFile or return $_errorDownloading->($http, "Unable to rename temporary $firmwareFile file" ); + + $response->code(200); + Slim::Web::HTTP::sendStreamingFile($httpClient, $response, 'application/octet-stream', $firmwareFile, undef, 1); + }, + $_errorDownloading, + { + saveAs => "$firmwareFile.tmp", + } + )->get($url); + + return; +} + +sub _errorDownloading { + my ($httpClient, $response, $http, $error, $url, $code) = @_; + + $error ||= ($http && $http->error) || 'unknown error'; + $url ||= ($http && $http->url) || 'no URL'; + $code ||= ($http && $http->code) || 500; + + $log->error(sprintf("Failed to get data from Github: %s (%s)", $error || $http->error, $url)); + + $response->headers->remove_content_headers; + $response->code($code); + $response->content_type('text/plain'); + $response->header('Connection' => 'close'); + $response->content(''); + + $httpClient->send_response($response); + Slim::Web::HTTP::closeHTTPSocket($httpClient); +}; + + 1; diff --git a/plugin/SqueezeESP32/install.xml b/plugin/SqueezeESP32/install.xml index 31629203..56551748 100644 --- a/plugin/SqueezeESP32/install.xml +++ b/plugin/SqueezeESP32/install.xml @@ -10,6 +10,6 @@ PLUGIN_SQUEEZEESP32 PLUGIN_SQUEEZEESP32_DESC Plugins::SqueezeESP32::Plugin - 0.231 + 0.300 Philippe diff --git a/plugin/repo.xml b/plugin/repo.xml index ed76e0d8..3a473e19 100644 --- a/plugin/repo.xml +++ b/plugin/repo.xml @@ -1,10 +1,10 @@ - + https://github.com/sle118/squeezelite-esp32 Philippe - 1177359481198edd64e116d6d1dc4fd2bcdfa021 + 0763bf75e2c8e7d535d50959916053d865ce2c4d philippe_44@outlook.com SqueezeESP32 additional player id (100) http://github.com/sle118/squeezelite-esp32/raw/master/plugin/SqueezeESP32.zip