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,39 +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) {
return response()->json([
'error' => [
'message' => 'You have no credits left to export.',
],
]);
}
return response()->json([
'success' => [
'data' => [
'user_usage' => $user->user_usage,
],
],
]);
}
public function premiumExportComplete(Request $request)
{
$user = Auth::user();
$user->load('user_usage');
if ($user->user_usage->non_watermark_videos_left <= 0) {
@@ -45,12 +22,65 @@ public function premiumExportComplete(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,
],
],
]);
}
public function premiumExportComplete(Request $request)
{
$request->validate([
'export_token' => 'required|string',
]);
$user = Auth::user();
$user->load('user_usage');
// Find the token
$token = ExportToken::where('token', $request->export_token)
->where('user_id', $user->id)
->first();
if (!$token) {
return response()->json([
'error' => [
'message' => 'Invalid export token.',
],
]);
}
if (!$token->isValid()) {
return response()->json([
'error' => [
'message' => 'Export token has expired or already been used.',
],
]);
}
// Mark token as used
$token->markAsUsed();
return response()->json([
'success' => [
'data' => [

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);
}
}

View File

@@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('export_tokens', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->uuid('token')->unique();
$table->boolean('is_premium')->default(false);
$table->integer('credits_reserved')->default(1);
$table->timestamp('expires_at');
$table->timestamp('used_at')->nullable();
$table->timestamps();
$table->index(['user_id', 'expires_at']);
$table->index('expires_at');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('export_tokens');
}
};

View File

@@ -4,7 +4,7 @@ import { Progress } from '@/components/ui/progress';
import { Spinner } from '@/components/ui/spinner.js';
import { Textarea } from '@/components/ui/textarea';
import useUserStore from '@/stores/UserStore';
import { Clock10Icon, Download, Droplets } from 'lucide-react';
import { Clock10Icon, Download } from 'lucide-react';
import { useEffect, useRef, useState } from 'react';
const VideoDownloadModal = ({
@@ -24,6 +24,7 @@ const VideoDownloadModal = ({
const [estimatedTimeRemaining, setEstimatedTimeRemaining] = useState(null);
const [status, setStatus] = useState('start'); // 'start', 'processing', 'complete'
const [downloadState, setDownloadState] = useState('idle'); // 'idle', 'downloading', 'downloaded'
const [exportToken, setExportToken] = useState(null);
const exportStartTime = useRef(null);
const lastProgressTime = useRef(null);
@@ -105,6 +106,10 @@ const VideoDownloadModal = ({
}
if (response?.success) {
// Store the export token
const token = response.success.data.export_token;
console.log('Received export token:', token);
setExportToken(token);
// Continue with export if successful
setStatus('processing');
handleDownloadButton();
@@ -124,6 +129,7 @@ const VideoDownloadModal = ({
setIsPremiumExport(false);
setEstimatedTimeRemaining(null);
setStatus('start');
setExportToken(null);
exportStartTime.current = null;
lastProgressTime.current = null;
lastProgress.current = 0;
@@ -135,11 +141,14 @@ const VideoDownloadModal = ({
if (status === 'processing' && exportProgress >= 100) {
setStatus('complete');
// Call premiumExportComplete immediately when export completes
if (isPremiumExport) {
premiumExportComplete();
if (isPremiumExport && exportToken) {
console.log('Calling premiumExportComplete with token:', exportToken);
premiumExportComplete(exportToken);
} else if (isPremiumExport && !exportToken) {
console.error('Premium export completed but no token available');
}
}
}, [exportProgress, status, isPremiumExport, premiumExportComplete]);
}, [exportProgress, status, isPremiumExport, exportToken, premiumExportComplete]);
// Calculate estimated time remaining based on progress speed
useEffect(() => {
@@ -291,10 +300,10 @@ const VideoDownloadModal = ({
<div className="space-y-8 py-4">
<div className="space-y-4 text-center">
<div className="bg-muted mx-auto flex h-16 w-16 items-center justify-center rounded-full">
{isPremiumExport ? <Download className="h-8 w-8 animate-pulse" /> : <Droplets className="h-8 w-8 animate-pulse" />}
<Download className="h-8 w-8 animate-pulse" />
</div>
<div className="space-y-2">
<h3 className="text-xl font-semibold">Exporting {isPremiumExport ? 'without watermark' : ''}</h3>
<h3 className="text-xl font-semibold">Exporting...</h3>
<p className="text-muted-foreground text-sm text-wrap">
Please do not close this window while the export is in progress.
@@ -314,7 +323,7 @@ const VideoDownloadModal = ({
<span className="text-muted-foreground text-xs">{formatTimeRemaining(estimatedTimeRemaining)}</span>
</div>
) : (
exportProgress > 5 && (
exportProgress != null && (
<div className="text-center">
<span className="text-muted-foreground text-xs">Calculating time remaining...</span>
</div>
@@ -330,8 +339,8 @@ const VideoDownloadModal = ({
<Download className="h-8 w-8" />
</div>
<div className="space-y-2">
<h3 className="text-xl font-semibold">Export Complete!</h3>
<p className="text-muted-foreground text-sm">Your video has been successfully exported.</p>
<h3 className="text-xl font-semibold">Done!</h3>
<p className="text-muted-foreground text-sm">Your video is now ready.</p>
</div>
<div className="space-y-3">
<Button
@@ -342,20 +351,20 @@ const VideoDownloadModal = ({
>
{downloadState === 'downloading' && (
<>
<Spinner className="w-4 h-4 mr-2" />
Downloading
<Spinner className="mr-2 h-4 w-4" />
Saving
</>
)}
{downloadState === 'downloaded' && (
<>
<Download className="mr-2 h-5 w-5" />
Downloaded!
Saved!
</>
)}
{downloadState === 'idle' && (
<>
<Download className="mr-2 h-5 w-5" />
Download Video
Save Video
</>
)}
</Button>

View File

@@ -68,6 +68,10 @@ const useUserStore = create(
});
}
if (response?.data?.success?.message) {
toast.success(response.data.success.message);
}
if (response?.data?.error?.message) {
toast.error(response.data.error.message);
}
@@ -78,9 +82,11 @@ const useUserStore = create(
}
},
premiumExportComplete: async () => {
premiumExportComplete: async (exportToken) => {
try {
const response = await axiosInstance.post(route('api.user.premium_export.complete'));
const response = await axiosInstance.post(route('api.user.premium_export.complete'), {
export_token: exportToken,
});
if (response?.data?.success?.data?.user_usage) {
set({
@@ -88,6 +94,10 @@ const useUserStore = create(
});
}
if (response?.data?.success?.message) {
toast.success(response.data.success.message);
}
if (response?.data?.error?.message) {
toast.error(response.data.error.message);
}