Реализация экспорта данных
Экспорт данных позволяет пользователям выгружать свои данные в структурированных форматах. Требования GDPR (ст. 20 о праве на переносимость) обязывают предоставить экспорт в машиночитаемом формате.
Архитектура для больших объёмов
Для небольших данных (<10k строк) — синхронный ответ. Для больших — асинхронная генерация с уведомлением.
[User request] → [Create ExportJob record] → [Queue Worker]
↓
[Generate files]
↓
[Upload to S3 (zip)]
↓
[Email with download link]
(signed URL, 7 days TTL)
Laravel: универсальный экспорт
class DataExportService
{
public function exportUserData(User $user): Export
{
$export = Export::create([
'user_id' => $user->id,
'status' => 'pending',
'expires_at' => now()->addDays(7),
]);
ExportUserDataJob::dispatch($user, $export);
return $export;
}
}
class ExportUserDataJob implements ShouldQueue
{
public int $timeout = 600;
public function __construct(private User $user, private Export $export) {}
public function handle(): void
{
$this->export->update(['status' => 'processing']);
$tempDir = sys_get_temp_dir() . '/export_' . $this->export->id;
mkdir($tempDir, 0777, true);
try {
// Профиль
file_put_contents(
"$tempDir/profile.json",
json_encode($this->exportProfile(), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
);
// Заказы
$this->exportOrders("$tempDir/orders.csv");
// Сообщения
$this->exportMessages("$tempDir/messages.json");
// Создать ZIP
$zipPath = sys_get_temp_dir() . "/export_{$this->export->id}.zip";
$zip = new ZipArchive();
$zip->open($zipPath, ZipArchive::CREATE);
foreach (glob("$tempDir/*") as $file) {
$zip->addFile($file, basename($file));
}
$zip->close();
// Загрузить в S3
$s3Key = "exports/{$this->user->id}/{$this->export->id}/data.zip";
Storage::disk('s3')->put($s3Key, file_get_contents($zipPath));
$this->export->update([
'status' => 'ready',
's3_key' => $s3Key,
]);
$this->user->notify(new ExportReadyNotification($this->export));
} finally {
exec("rm -rf {$tempDir}");
if (file_exists($zipPath ?? '')) unlink($zipPath);
}
}
private function exportProfile(): array
{
return [
'id' => $this->user->id,
'name' => $this->user->name,
'email' => $this->user->email,
'created_at' => $this->user->created_at->toIso8601String(),
'profile' => $this->user->profile?->toArray(),
];
}
private function exportOrders(string $path): void
{
$handle = fopen($path, 'w');
fprintf($handle, chr(0xEF) . chr(0xBB) . chr(0xBF)); // UTF-8 BOM
fputcsv($handle, ['Номер', 'Дата', 'Сумма', 'Статус', 'Позиции'], ';');
$this->user->orders()->with('items')->lazy(200)->each(function (Order $order) use ($handle) {
$items = $order->items->map(fn($i) => "{$i->name} x{$i->quantity}")->join(', ');
fputcsv($handle, [
$order->number,
$order->created_at->format('d.m.Y H:i'),
number_format($order->total, 2),
$order->status,
$items,
], ';');
});
fclose($handle);
}
private function exportMessages(string $path): void
{
$messages = $this->user->messages()
->select('id', 'subject', 'body', 'created_at')
->get()
->map(fn($m) => [
'id' => $m->id,
'subject' => $m->subject,
'body' => strip_tags($m->body),
'date' => $m->created_at->toIso8601String(),
]);
file_put_contents($path, json_encode($messages, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
}
public function failed(\Throwable $e): void
{
$this->export->update(['status' => 'failed']);
Log::error('Export failed', ['export_id' => $this->export->id, 'error' => $e->getMessage()]);
}
}
Signed URL для скачивания
public function download(Export $export): RedirectResponse
{
$this->authorize('download', $export);
if ($export->status !== 'ready' || $export->expires_at->isPast()) {
abort(410, 'Экспорт недоступен или устарел');
}
$url = Storage::disk('s3')->temporaryUrl($export->s3_key, now()->addMinutes(15));
return redirect()->away($url);
}
Форматы экспорта
| Формат | Применение | Инструмент |
|---|---|---|
| JSON | API, GDPR-выгрузка | PHP json_encode, Node JSON.stringify |
| CSV | Таблицы, Excel | fputcsv, csv-writer |
| XLSX | Бухгалтерия, аналитика | PhpSpreadsheet, ExcelJS |
| XML | B2B интеграции | SimpleXML, xmlbuilder2 |
| ZIP | Архивы нескольких файлов | ZipArchive, archiver (Node) |
Срок реализации
Базовый экспорт данных пользователя (JSON + CSV в ZIP): 2–3 дня. С очередью, S3 и email-уведомлением: 3–4 дня. GDPR-совместимый экспорт всех персональных данных: 4–5 дней.







