This commit is contained in:
Kekskurse 2019-09-24 16:02:39 +02:00
parent 6c2cc20405
commit b539e36ab9
12 changed files with 1309 additions and 446 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@
Homestead.json Homestead.json
Homestead.yaml Homestead.yaml
.env .env
storage/icon

View file

@ -1,12 +1,17 @@
FROM php:7.3-apache FROM php:7.3-apache
RUN apt-get update
RUN apt-get install -y libpng-dev
RUN docker-php-ext-install mysqli RUN docker-php-ext-install mysqli
RUN docker-php-ext-install pdo pdo_mysql RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-install gd
RUN a2enmod rewrite RUN a2enmod rewrite
ADD ./ /var/www ADD ./ /var/www
RUN chmod uog+rwx /var/www/storage/logs RUN chmod uog+rwx /var/www/storage/logs
RUN chmod uog+rwx /var/www/storage
WORKDIR /var/www WORKDIR /var/www

View file

@ -12,8 +12,10 @@ use App\Http\Resources\API\AppForOwner;
use App\Http\Resources\API\AppUser; use App\Http\Resources\API\AppUser;
use App\Models\Setting; use App\Models\Setting;
use App\Models\User; use App\Models\User;
use Aws\S3\S3Client;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Laravel\Lumen\Routing\Controller as BaseController; use Laravel\Lumen\Routing\Controller as BaseController;
use TaGeSo\APIResponse\Response; use TaGeSo\APIResponse\Response;
@ -160,6 +162,53 @@ class AppController extends BaseController
return $response->withData(AppUser::collection($access)); return $response->withData(AppUser::collection($access));
} }
public function changeImage(Request $request, Response $response, $id) {
if(!Auth::check()) {
throw new NotLoggedInException();
}
$newTmp = tempnam("", "icon_upload");
$request->file("img")->move("/tmp", $newTmp);
$info = getimagesize($newTmp);
Log::debug("Image sitze", $info);
Log::debug("File size ".filesize($newTmp));
if($info["0"] != $info["1"]) {
throw new HTTPException(400, "Image must be a squader.");
}
if($info[0] > 1000) {
throw new HTTPException(400, "Image is to big, max 1000 px.");
}
if($info[0] < 50) {
throw new HTTPException(400, "Image is to small, min 50 px.");
}
$app = \App\Models\App::query()->where("id", "=", $id)->firstOrFail();
if($app->user_id != Auth::user()->id) {
throw new NoPermissionException(403, "Not your app (".$app->user_id."/".Auth::user()->id.")");
}
$image = imagecreatefrompng($newTmp);
imagepng($image, $newTmp."2", 2);
Log::debug("New File size ".filesize($newTmp."2"));
$s3 = app(S3Client::class);
$result = $s3->putObject([
"Bucket" => getenv("S3_Bucket"),
"Key" => "icons/icon_".$app->id.".png",
"SourceFile" => $newTmp,
'ACL' => 'public-read'
]);
$app->iconURL = $result['ObjectURL'];
$app->saveOrFail();
return $response->withData(["url" => $result['ObjectURL']]);
}
} }

View file

@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
use App\Http\Resources\oAuth\AccessToken; use App\Http\Resources\oAuth\AccessToken;
use App\Models\App; use App\Models\App;
use App\Models\User; use App\Models\User;
use Aws\S3\S3Client;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
@ -101,21 +102,44 @@ class AppController extends Controller
echo "Its not your app. <a href='/gui/apps/".$id."'>Zurück</a>";exit(); echo "Its not your app. <a href='/gui/apps/".$id."'>Zurück</a>";exit();
} }
$app->icon = file_get_contents($newTmp); $s3 = app(S3Client::class);
$result = $s3->putObject([
"Bucket" => getenv("S3_Bucket"),
"Key" => "icons/icon_".$app->id.".png",
"SourceFile" => $newTmp,
'ACL' => 'public-read'
]);
$app->iconURL = $result['ObjectURL'];
$app->saveOrFail(); $app->saveOrFail();
return redirect('/gui/apps/'.$id); return redirect('/gui/apps/'.$id);
} }
public function getAppIcon($id) { public function getAppIcon($id) {
$app = App::query()->where("id", "=", $id)->firstOrFail(); $app = App::query()->where("id", "=", $id)->firstOrFail();
if(empty($app->icon)) { if(!is_dir(storage_path("icon"))) {
$app->icon = file_get_contents(resource_path("images/app.png")); mkdir(storage_path("icon"));
} }
$r = getimagesizefromstring($app->icon); $cacheFile = storage_path("icon/".$app->id.".png");
return response($app->icon) if(file_exists($cacheFile)) {
$icon = file_get_contents($cacheFile);
} else {
if(!empty($app->iconURL)) {
$icon = file_get_contents($app->iconURL);
file_put_contents($cacheFile, $icon);
} else {
$icon = file_get_contents(resource_path("images/app.png"));
}
}
$r = getimagesizefromstring($icon);
return response($icon)
->header('Content-Type',$r["mime"]); ->header('Content-Type',$r["mime"]);
} }

View file

@ -20,6 +20,7 @@ class App extends JsonResource
'description' => $this->description, 'description' => $this->description,
'directUrl' => $this->direct_url, 'directUrl' => $this->direct_url,
'url' => $this->url, 'url' => $this->url,
'iconURL' => $this->iconURL,
'properties' => [ 'properties' => [
'testingWarning' => (bool)$this->testing_warning, 'testingWarning' => (bool)$this->testing_warning,
#'autoAccept' => (bool)$this->auto_accept, #'autoAccept' => (bool)$this->auto_accept,

View file

@ -24,6 +24,7 @@ class AppForOwner extends JsonResource
'directUrl' => $this->direct_url, 'directUrl' => $this->direct_url,
'apiKey' => $this->apiKey, 'apiKey' => $this->apiKey,
'apiSecret' => $this->apiSecret, 'apiSecret' => $this->apiSecret,
'iconURL' => $this->iconURL,
'properties' => [ 'properties' => [
'testingWarning' => (bool)$this->testing_warning, 'testingWarning' => (bool)$this->testing_warning,
'autoAccept' => (bool)$this->auto_accept, 'autoAccept' => (bool)$this->auto_accept,

View file

@ -2,6 +2,7 @@
namespace App\Providers; namespace App\Providers;
use Aws\S3\S3Client;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
@ -19,6 +20,16 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function register() public function register()
{ {
// $this->app->singleton(S3Client::class, function() {
$s3 = new S3Client([
'region' => 'eu-west-1',
'version' => 'latest',
'credentials' => [
'key' => getenv("S3_ACCESS_KEY"),
'secret' => getenv("S3_SECRET")
]
]);
return $s3;
});
} }
} }

View file

@ -10,7 +10,8 @@
"vlucas/phpdotenv": "^3.3", "vlucas/phpdotenv": "^3.3",
"phpmailer/phpmailer": "~6.0", "phpmailer/phpmailer": "~6.0",
"tageso/api-response": "*", "tageso/api-response": "*",
"google/recaptcha": "^1.2" "google/recaptcha": "^1.2",
"aws/aws-sdk-php":"^3."
}, },
"require-dev": { "require-dev": {
"fzaninotto/faker": "^1.4", "fzaninotto/faker": "^1.4",

1601
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,36 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AppIconUrl extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('apps', function (Blueprint $table) {
$table->removeColumn("icon");
$table->string("iconURL", 500)->nullable()->comment("Public link to icon");
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('apps', function (Blueprint $table) {
$table->binary("icon")->nullable()->default(null)->comment("200x200 Image as Icon");
$table->removeColumn("iconURL");
});
//
}
}

View file

@ -57,6 +57,7 @@ $router->group(['prefix' => 'api'], function () use ($router) {
$router->get("/access", ["uses" => "API\oAuthController@getAccess"]); $router->get("/access", ["uses" => "API\oAuthController@getAccess"]);
$router->post("/access/allow", ["uses" => "API\oAuthController@allowAccess"]); $router->post("/access/allow", ["uses" => "API\oAuthController@allowAccess"]);
$router->get("/user", ["uses" => "API\AppController@getUsers"]); $router->get("/user", ["uses" => "API\AppController@getUsers"]);
$router->post("/changeImage", ["uses" => "API\AppController@changeImage"]);
}); });
}); });
$router->group(['prefix' => 'account'], function () use ($router) { $router->group(['prefix' => 'account'], function () use ($router) {