This commit is contained in:
ct
2025-07-03 20:05:53 +08:00
parent 35c7c0bebd
commit 3d7b3c428b
6 changed files with 233 additions and 24 deletions

View File

@@ -0,0 +1,61 @@
<?php
namespace App\Console\Commands;
use App\Models\ExportToken;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class CleanupExpiredExportTokens extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cleanup:expired-export-tokens';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Cleanup expired export tokens and restore credits to users';
/**
* Execute the console command.
*/
public function handle()
{
$this->info('Starting cleanup of expired export tokens...');
// Get all expired and unused tokens
$expiredTokens = ExportToken::expiredAndUnused()->get();
if ($expiredTokens->isEmpty()) {
$this->info('No expired tokens found.');
return;
}
$this->info('Found ' . $expiredTokens->count() . ' expired tokens to process.');
$restoredCredits = 0;
DB::transaction(function () use ($expiredTokens, &$restoredCredits) {
foreach ($expiredTokens as $token) {
if ($token->is_premium && $token->credits_reserved > 0) {
// Restore credits to user
$token->user->user_usage()->increment('non_watermark_videos_left', $token->credits_reserved);
$restoredCredits += $token->credits_reserved;
$this->info("Restored {$token->credits_reserved} credits to user {$token->user_id}");
}
// Delete the expired token
$token->delete();
}
});
$this->info("Cleanup completed. Restored {$restoredCredits} credits and deleted {$expiredTokens->count()} expired tokens.");
}
}

View File

@@ -2,16 +2,16 @@
namespace App\Http\Controllers;
use App\Models\ExportToken;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
class UserExportController extends Controller
{
public function premiumExportRequest(Request $request)
{
$user = Auth::user();
$user->load('user_usage');
if ($user->user_usage->non_watermark_videos_left <= 0) {
@@ -22,10 +22,27 @@ public function premiumExportRequest(Request $request)
]);
}
// Immediately consume the credit and create token
$user->user_usage->update([
'non_watermark_videos_left' => $user->user_usage->non_watermark_videos_left - 1,
]);
// Create export token (expires in 30 minutes)
$token = ExportToken::create([
'user_id' => $user->id,
'token' => Str::uuid()->toString(),
'is_premium' => true,
'credits_reserved' => 1,
'expires_at' => now()->addMinutes(30),
]);
$user->user_usage->refresh();
return response()->json([
'success' => [
'data' => [
'user_usage' => $user->user_usage,
'export_token' => $token->token,
],
],
]);
@@ -33,23 +50,36 @@ public function premiumExportRequest(Request $request)
public function premiumExportComplete(Request $request)
{
$user = Auth::user();
$request->validate([
'export_token' => 'required|string',
]);
$user = Auth::user();
$user->load('user_usage');
if ($user->user_usage->non_watermark_videos_left <= 0) {
// Find the token
$token = ExportToken::where('token', $request->export_token)
->where('user_id', $user->id)
->first();
if (!$token) {
return response()->json([
'error' => [
'message' => 'You have no credits left to export.',
'message' => 'Invalid export token.',
],
]);
}
$user->user_usage->update([
'non_watermark_videos_left' => $user->user_usage->non_watermark_videos_left - 1,
]);
if (!$token->isValid()) {
return response()->json([
'error' => [
'message' => 'Export token has expired or already been used.',
],
]);
}
$user->user_usage->refresh();
// Mark token as used
$token->markAsUsed();
return response()->json([
'success' => [

View File

@@ -0,0 +1,63 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ExportToken extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'token',
'is_premium',
'credits_reserved',
'expires_at',
'used_at',
];
protected $casts = [
'is_premium' => 'boolean',
'expires_at' => 'datetime',
'used_at' => 'datetime',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function isExpired(): bool
{
return $this->expires_at->isPast();
}
public function isUsed(): bool
{
return !is_null($this->used_at);
}
public function isValid(): bool
{
return !$this->isExpired() && !$this->isUsed();
}
public function markAsUsed(): void
{
$this->update(['used_at' => now()]);
}
public function scopeExpiredAndUnused($query)
{
return $query->where('expires_at', '<', now())
->whereNull('used_at');
}
public function scopeForUser($query, $userId)
{
return $query->where('user_id', $userId);
}
}