frontend/backend update

This commit is contained in:
carodej
2020-06-05 16:48:11 +02:00
parent ca14838212
commit cffdc3ced9
67 changed files with 3611 additions and 1136 deletions

View File

@@ -63,10 +63,11 @@ class UpgradeApp extends Command
// Migrate new tables and changes
$this->call('migrate');
$this->call('rinvex:migrate:subscriptions');
$this->call('db:seed', [
/*$this->call('db:seed', [
'--class' => 'PaymentGatewaysSeeder'
]);
]);*/
}
/**

View File

@@ -2,8 +2,8 @@
namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
class Handler extends ExceptionHandler
{
@@ -29,12 +29,12 @@ class Handler extends ExceptionHandler
/**
* Report or log an exception.
*
* @param \Exception $exception
* @param \Throwable $exception
* @return void
*
* @throws \Exception
*/
public function report(Exception $exception)
public function report(Throwable $exception)
{
parent::report($exception);
}
@@ -43,13 +43,13 @@ class Handler extends ExceptionHandler
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Exception
* @throws \Throwable
*/
public function render($request, Exception $exception)
public function render($request, Throwable $exception)
{
return parent::render($request, $exception);
}
}
}

View File

@@ -5,8 +5,12 @@ namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Resources\PlanCollection;
use App\Http\Resources\PlanResource;
use App\Http\Resources\UserResource;
use App\Http\Resources\UsersCollection;
use App\Plan;
use App\User;
use Illuminate\Http\Request;
use Rinvex\Subscriptions\Models\PlanFeature;
class PlanController extends Controller
{
@@ -15,9 +19,11 @@ class PlanController extends Controller
*
* @return PlanCollection
*/
public function index() {
return new PlanCollection(Plan::all());
public function index()
{
return new PlanCollection(
app('rinvex.subscriptions.plan')->all()
);
}
/**
@@ -28,9 +34,9 @@ class PlanController extends Controller
*/
public function show($id)
{
$plan = Plan::findOrFail($id);
return new PlanResource($plan);
return new PlanResource(
app('rinvex.subscriptions.plan')->find($id)
);
}
/**
@@ -41,9 +47,24 @@ class PlanController extends Controller
*/
public function store(Request $request)
{
// TODO: validation request
$plan = app('rinvex.subscriptions.plan')->create([
'description' => $request->input('attributes.description'),
'price' => $request->input('attributes.price'),
'name' => $request->input('attributes.name'),
'currency' => 'USD',
'invoice_period' => 1,
'sort_order' => 1,
'signup_fee' => 0,
]);
$plan = Plan::create($request->input('attributes'));
// Create multiple plan features at once
$plan->features()->saveMany([
new PlanFeature([
'name' => 'Storage capacity',
'value' => $request->input('attributes.capacity'),
'sort_order' => 1
]),
]);
return new PlanResource($plan);
}
@@ -58,11 +79,32 @@ class PlanController extends Controller
public function update(Request $request, $id)
{
// TODO: validation request
$plan = Plan::findOrFail($id);
$plan = app('rinvex.subscriptions.plan')->find($id);
// Update text data
$plan->update(make_single_input($request));
if ($request->name === 'capacity') {
$plan->getFeatureBySlug('storage-capacity')->update(['value' => $request->value]);
} else {
$plan->update(make_single_input($request));
}
return response('Saved!', 204);
}
/**
* Get subscriptions
*
* @param $id
* @return mixed
*/
public function subscribers($id)
{
$subscribers = app('rinvex.subscriptions.plan')
->find($id)
->subscriptions
->pluck('user_id');
return new UsersCollection(
User::findMany($subscribers)
);
}
}

View File

@@ -214,7 +214,7 @@ class UserController extends Controller
// Delete thumbnail if exist
if (!is_null($file->thumbnail)) {
Storage::delete('/file-manager/' . $file->getOriginal('thumbnail'));
Storage::delete('/file-manager/' . $file->getRawOriginal('thumbnail'));
}
// Delete file permanently
@@ -232,7 +232,7 @@ class UserController extends Controller
// Remove favourites
$user->settings->delete();
$user->favourites()->sync([]);
$user->favourite_folders()->sync([]);
// Delete user
$user->delete();

View File

@@ -206,12 +206,12 @@ class FileAccessController extends Controller
private function thumbnail_file($file)
{
// Get file path
$path = '/file-manager/' . $file->getOriginal('thumbnail');
$path = '/file-manager/' . $file->getRawOriginal('thumbnail');
// Check if file exist
if (!Storage::exists($path)) abort(404);
// Return image thumbnail
return Storage::download($path, $file->getOriginal('thumbnail'));
return Storage::download($path, $file->getRawOriginal('thumbnail'));
}
}

View File

@@ -39,10 +39,10 @@ class FavouriteController extends Controller
if ($folder->user_id !== $user->id) abort(403);
// Add folder to user favourites
$user->favourites()->syncWithoutDetaching($request->unique_id);
$user->favourite_folders()->syncWithoutDetaching($request->unique_id);
// Return updated favourites
return $user->favourites->makeHidden(['pivot']);
return $user->favourites;
}
/**
@@ -61,9 +61,9 @@ class FavouriteController extends Controller
}
// Remove folder from user favourites
$user->favourites()->detach($unique_id);
$user->favourite_folders()->detach($unique_id);
// Return updated favourites
return $user->favourites->makeHidden(['pivot']);
return $user->favourites;
}
}

View File

@@ -42,7 +42,7 @@ class TrashController extends Controller
Storage::delete('/file-manager/' . $file->basename);
// Delete thumbnail if exist
if ($file->thumbnail) Storage::delete('/file-manager/' . $file->getOriginal('thumbnail'));
if ($file->thumbnail) Storage::delete('/file-manager/' . $file->getRawOriginal('thumbnail'));
// Delete file permanently
$file->forceDelete();

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Http\Controllers\General;
use App\Http\Controllers\Controller;
use App\Http\Resources\PricingCollection;
use Illuminate\Http\Request;
class PricingController extends Controller
{
/**
* Get all active plans
*
* @return PricingCollection
*/
public function index() {
return new PricingCollection(
app('rinvex.subscriptions.plan')->where('is_active', 1)->get()
);
}
}

View File

@@ -28,9 +28,7 @@ class AccountController extends Controller
public function user()
{
// Get User
$user = User::with(['favourites'])
->where('id', Auth::id())
->first();
$user = Auth::user();
// Get folder tree
$tree = FileManagerFolder::with(['folders.shared', 'shared:token,id,item_id,permission,protected'])
@@ -40,7 +38,7 @@ class AccountController extends Controller
return [
'user' => $user->only(['name', 'email', 'avatar', 'role']),
'favourites' => $user->favourites->makeHidden(['pivot']),
'favourites' => $user->favourite_folders->makeHidden(['pivot']),
'tree' => $tree,
'storage' => [
'used' => Metric::bytes($user->used_capacity)->format(),

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use App\Invoice;
use Auth;
use Illuminate\Http\Request;
class SubscriptionController extends Controller
{
/**
* Upgrade account to subscription
*
* @param Request $request
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
*/
public function upgrade(Request $request)
{
// TODO: validation request
// Get user
$user = Auth::user();
// Get requested plan
$plan = app('rinvex.subscriptions.plan')
->find($request->input('plan.data.id'));
// Create subscription
$user->newSubscription('main', $plan);
// Update user storage limig
$user->settings()->update([
'storage_capacity' => $plan->features->first()->value
]);
// Store invoice
Invoice::create(
get_invoice_data($user, $plan)
);
return response('Done!', 204);
}
}

View File

@@ -19,12 +19,13 @@ class PlanResource extends JsonResource
'id' => (string)$this->id,
'type' => 'plans',
'attributes' => [
'status' => $this->status,
'subscribers' => $this->subscriptions->count(),
'status' => $this->is_active,
'name' => $this->name,
'description' => $this->description,
'price' => $this->price,
'capacity_formatted' => format_gigabytes($this->capacity),
'capacity' => $this->capacity,
'capacity_formatted' => format_gigabytes($this->features->first()->value),
'capacity' => $this->features->first()->value,
'created_at_formatted' => format_date($this->created_at),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class PricingCollection extends ResourceCollection
{
public $collects = PricingResource::class;
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => $this->collection,
];
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class PricingResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => [
'id' => (string)$this->id,
'type' => 'plans',
'attributes' => [
'name' => $this->name,
'description' => $this->description,
'price' => $this->price,
'capacity_formatted' => format_gigabytes($this->features->first()->value),
'capacity' => $this->features->first()->value,
]
]
];
}
}

View File

@@ -33,7 +33,7 @@ class UserResource extends JsonResource
]
],
'relationships' => [
'settings' => [
'settings' => [
'data' => [
'id' => (string)$this->settings->id,
'type' => 'settings',
@@ -48,13 +48,14 @@ class UserResource extends JsonResource
]
]
],
'storage' => [
'storage' => [
'data' => [
'id' => '1',
'type' => 'storage',
'attributes' => $this->storage
]
],
'subscription' => $this->activeSubscriptions()->count() !== 0 ? new UserSubscription($this->subscription('main')) : null,
]
];
}

View File

@@ -23,25 +23,25 @@ class UserStorageResource extends JsonResource
// Get all images
$images = FileManagerFile::where('user_id', $this->id)
->where('type', 'image')->get()->map(function ($item) {
return (int)$item->getOriginal('filesize');
return (int)$item->getRawOriginal('filesize');
})->sum();
// Get all audios
$audios = FileManagerFile::where('user_id', $this->id)
->where('type', 'audio')->get()->map(function ($item) {
return (int)$item->getOriginal('filesize');
return (int)$item->getRawOriginal('filesize');
})->sum();
// Get all videos
$videos = FileManagerFile::where('user_id', $this->id)
->where('type', 'video')->get()->map(function ($item) {
return (int)$item->getOriginal('filesize');
return (int)$item->getRawOriginal('filesize');
})->sum();
// Get all documents
$documents = FileManagerFile::where('user_id', $this->id)
->whereIn('mimetype', $document_mimetypes)->get()->map(function ($item) {
return (int)$item->getOriginal('filesize');
return (int)$item->getRawOriginal('filesize');
})->sum();
// Get all other files
@@ -49,7 +49,7 @@ class UserStorageResource extends JsonResource
->whereNotIn('mimetype', $document_mimetypes)
->whereNotIn('type', ['audio', 'video', 'image'])
->get()->map(function ($item) {
return (int)$item->getOriginal('filesize');
return (int)$item->getRawOriginal('filesize');
})->sum();
return [

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class UserSubscription extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => [
'id' => $this->id,
'type' => 'subscription',
'attributes' => [
'name' => $this->name,
'slug' => $this->slug,
'starts_at' => format_date($this->starts_at, '%d. %B. %Y'),
'ends_at' => format_date($this->ends_at, '%d. %B. %Y'),
]
]
];
}
}

View File

@@ -124,7 +124,7 @@ class Editor
Storage::delete('/file-manager/' . $file->basename);
// Delete thumbnail if exist
if (!is_null($file->thumbnail)) Storage::delete('/file-manager/' . $file->getOriginal('thumbnail'));
if (!is_null($file->thumbnail)) Storage::delete('/file-manager/' . $file->getRawOriginal('thumbnail'));
// Delete file permanently
$file->forceDelete();
@@ -138,7 +138,7 @@ class Editor
if (!$request->force_delete) {
// Remove folder from user favourites
$user->favourites()->detach($unique_id);
$user->favourite_folders()->detach($unique_id);
// Soft delete folder record
$folder->delete();
@@ -172,7 +172,7 @@ class Editor
Storage::delete('/file-manager/' . $file->basename);
// Delete thumbnail if exist
if ($file->thumbnail) Storage::delete('/file-manager/' . $file->getOriginal('thumbnail'));
if ($file->thumbnail) Storage::delete('/file-manager/' . $file->getRawOriginal('thumbnail'));
// Delete file permanently
$file->forceDelete();

View File

@@ -11,12 +11,71 @@ use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\ImageManagerStatic as Image;
/**
* Get invoice number
*
* @return string
*/
function get_invoice_number()
{
$invoices = \App\Invoice::all();
if ($invoices->isEmpty()) {
return Carbon::now()->year . '00001';
} else {
return (int)$invoices->last()->order + 1;
}
}
function get_invoice_data($user, $plan)
{
$subscription = $user->subscription('main');
$order_number = get_invoice_number();
$token = \Illuminate\Support\Str::random(22);
return [
'token' => $token,
'order' => $order_number,
'user_id' => $user->id,
'plan_id' => $plan->id,
'total' => $plan->price,
'currency' => 'USD',
'bag' => [
[
'description' => 'Subscription - ' . $plan->name,
'date' => format_date($subscription->starts_at, '%d. %B. %Y') . ' - ' . format_date($subscription->ends_at, '%d. %B. %Y'),
'amount' => $plan->price,
]
],
'seller' => [
'billing_name' => 'VueFileManager',
'billing_address' => 'Somewhere 32',
'billing_state' => 'Washington',
'billing_city' => 'Manchester',
'billing_postal_code' => '04001',
'billing_country' => 'The USA',
'billing_phone_number' => '490321-6354774',
'billing_vat_number' => '7354724626246',
],
'client' => [
'billing_name' => $user->settings->billing_name,
'billing_address' => $user->settings->billing_address,
'billing_state' => $user->settings->billing_state,
'billing_city' => $user->settings->billing_city,
'billing_postal_code' => $user->settings->billing_postal_code,
'billing_country' => $user->settings->billing_country,
'billing_phone_number' => $user->settings->billing_phone_number,
],
];
}
/**
* Get app version from config
*
* @return \Illuminate\Config\Repository|mixed
*/
function get_storage() {
function get_storage()
{
return env('FILESYSTEM_DRIVER');
}
@@ -25,7 +84,8 @@ function get_storage() {
*
* @return bool
*/
function is_storage_driver($driver) {
function is_storage_driver($driver)
{
if (is_array($driver)) {
return in_array(env('FILESYSTEM_DRIVER'), $driver);
@@ -39,7 +99,8 @@ function is_storage_driver($driver) {
*
* @return \Illuminate\Config\Repository|mixed
*/
function get_version() {
function get_version()
{
return config('vuefilemanager.version');
}
@@ -48,7 +109,8 @@ function get_version() {
*
* @return mixed
*/
function is_demo($user_id) {
function is_demo($user_id)
{
return env('APP_DEMO', false) && $user_id === 1;
}
@@ -61,7 +123,8 @@ function is_demo($user_id) {
* @param $user_id
* @return \Illuminate\Database\Eloquent\Builder|Model
*/
function get_item($type, $unique_id, $user_id) {
function get_item($type, $unique_id, $user_id)
{
if ($type === 'folder') {
@@ -83,7 +146,8 @@ function get_item($type, $unique_id, $user_id) {
* @param $token
* @return \Illuminate\Database\Eloquent\Builder|Model
*/
function get_shared($token) {
function get_shared($token)
{
return Share::where(DB::raw('BINARY `token`'), $token)
->firstOrFail();
@@ -95,7 +159,8 @@ function get_shared($token) {
* @param $shared
* @return bool
*/
function is_editor($shared) {
function is_editor($shared)
{
return $shared->permission === 'editor';
}
@@ -190,7 +255,11 @@ function make_single_input($request)
*/
function format_gigabytes($gigabytes)
{
return Metric::gigabytes($gigabytes)->format();
if ($gigabytes >= 1000) {
return Metric::gigabytes($gigabytes)->format('Tb/');
} else {
return Metric::gigabytes($gigabytes)->format('GB/');
}
}
/**

View File

@@ -6,12 +6,21 @@ use Illuminate\Database\Eloquent\Model;
class Invoice extends Model
{
protected $guarded = [
'id'
];
protected $casts = [
'seller' => 'array',
'client' => 'array',
'bag' => 'array',
];
/**
* Get user instance
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function user() {
return $this->hasOne(User::class, 'id', 'user_id');
}

View File

@@ -1,10 +0,0 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Plan extends Model
{
protected $guarded = ['id'];
}

View File

@@ -11,6 +11,7 @@ use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Notification;
use Laravel\Passport\HasApiTokens;
use Rinvex\Subscriptions\Traits\HasSubscriptions;
/**
* App\User
@@ -55,7 +56,7 @@ use Laravel\Passport\HasApiTokens;
*/
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
use HasApiTokens, Notifiable, HasSubscriptions;
/**
* The attributes that are mass assignable.
@@ -111,7 +112,7 @@ class User extends Authenticatable
public function getUsedCapacityAttribute() {
$user_capacity = $this->files_with_trashed->map(function ($item) {
return $item->getOriginal();
return $item->getRawOriginal();
})->sum('filesize');
return $user_capacity;
@@ -147,7 +148,7 @@ class User extends Authenticatable
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function favourites()
public function favourite_folders()
{
return $this->belongsToMany(FileManagerFolder::class, 'favourite_folder', 'user_id', 'folder_unique_id', 'id', 'unique_id')->with('shared:token,id,item_id,permission,protected');
}