Skip to content

Commit 2ebe96d

Browse files
committed
Refactor campaign export to stream directly to s3 instead of locally first
1 parent b60e054 commit 2ebe96d

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

app/Services/Campaign/ExportService.php

+19-27
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ExportService
3131

3232
protected string $file;
3333

34-
protected $archive;
34+
protected \STS\ZipStream\Builder $archive;
3535

3636
protected bool $assets = false;
3737

@@ -168,9 +168,11 @@ protected function prepare(): self
168168
File::ensureDirectoryExists($saveFolder);
169169

170170
// We want the full path for jobs running in the queue.
171-
$this->file = $this->campaign->id . '_' . date('Ymd_His') . ($this->assets ? '_assets' : null) . '.zip';
171+
$this->file =
172+
Str::slug($this->campaign->name) . '_' .
173+
date('Ymd_His') . '.zip';
172174
CampaignCache::campaign($this->campaign);
173-
$this->path = $saveFolder . $this->file;
175+
//$this->path = $saveFolder . $this->file;
174176
$this->archive = Zip::create($this->file);
175177

176178
// Count the number of elements to export to get a rough idea of progress
@@ -240,7 +242,7 @@ protected function entities(): self
240242
'entity',
241243
'entity.entityTags', 'entity.relationships',
242244
'entity.posts', 'entity.posts.postTags', 'entity.abilities', 'entity.abilities.ability',
243-
'entity.events',
245+
'entity.reminders',
244246
'entity.image',
245247
'entity.header',
246248
'entity.assets',
@@ -263,9 +265,8 @@ protected function entities(): self
263265
}
264266
} catch (Exception $e) {
265267
Log::error('Campaign export', ['err' => $e->getMessage()]);
266-
$saveFolder = storage_path($this->exportPath);
267-
$this->archive->saveTo($saveFolder);
268-
unlink($this->path);
268+
//$saveFolder = storage_path($this->exportPath);
269+
//$this->archive->saveTo($saveFolder);
269270
throw new Exception(
270271
'Missing campaign entity relation: ' . $entity . '-' . $class . '? '
271272
. $e->getMessage()
@@ -281,7 +282,7 @@ protected function customEntities(): self
281282
$entityWith = [
282283
'entityTags', 'relationships',
283284
'posts', 'posts.postTags', 'abilities', 'abilities.ability',
284-
'events',
285+
'reminders',
285286
'image',
286287
'header',
287288
'assets',
@@ -311,9 +312,8 @@ protected function customEntities(): self
311312
}
312313
} catch (Exception $e) {
313314
Log::error('Campaign export', ['err' => $e->getMessage()]);
314-
$saveFolder = storage_path($this->exportPath);
315-
$this->archive->saveTo($saveFolder);
316-
unlink($this->path);
315+
// $saveFolder = storage_path($this->exportPath);
316+
// $this->archive->saveTo($saveFolder);
317317
throw new Exception(
318318
'Missing campaign entity relation: ' . $entityType->singular . '? '
319319
. $e->getMessage()
@@ -343,9 +343,8 @@ protected function gallery(): self
343343
/** @var Image $image */
344344
$this->processImage($image);
345345
} catch (Exception $e) {
346-
$saveFolder = storage_path($this->exportPath);
347-
$this->archive->saveTo($saveFolder);
348-
unlink($this->path);
346+
// $saveFolder = storage_path($this->exportPath);
347+
// $this->archive->saveTo($saveFolder);
349348
throw new Exception(
350349
$e->getMessage()
351350
);
@@ -461,31 +460,24 @@ protected function finish(): self
461460
{
462461
// Save all the content.
463462
try {
464-
$saveFolder = storage_path($this->exportPath);
465-
Log::info('Campaign export', ['path' => $saveFolder, 'exportPath' => $this->exportPath]);
466-
$this->archive->saveTo($saveFolder);
467-
$this->filesize = (int) floor(filesize($this->path) / pow(1024, 2));
463+
$path = 'exports/campaigns/' . $this->campaign->id . '/';
464+
$this->exportPath = $path . $this->file;
465+
Log::info('Campaign export finished', ['exportPath' => $this->exportPath]);
466+
$this->archive->saveToDisk('s3', $path);
467+
Storage::disk('s3')->setVisibility($this->exportPath, 'public');
468+
$this->filesize = (int) floor($this->archive->getFinalSize() / pow(1024, 2));
468469
} catch (Exception $e) {
469470
Log::error('Campaign export', ['action' => 'finish', 'err' => $e->getMessage()]);
470471
// The export might fail if the zip is too big.
471472
$this->files = 0;
472473
throw new Exception($e->getMessage());
473474
}
474-
if ($this->files === 0) {
475-
return $this;
476-
}
477-
478-
// Move to ?
479-
$this->exportPath = Storage::putFileAs('exports/campaigns/' . $this->campaign->id, $this->path, $this->file, 'public');
480-
unlink($this->path);
481475

482476
return $this;
483477
}
484478

485479
/**
486480
* Track that the export had an issue
487-
*
488-
* @return $this
489481
*/
490482
public function fail(): self
491483
{

resources/views/components/form.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@if ($files) enctype="multipart/form-data" @endif
77
@if ($unsaved) data-unload="1" @endif
88
@if ($shortcut && $method !== 'DELETE') data-shortcut="1" @endif
9-
{{ $attributes->merge(['class' => 'w-full flex flex-col gap-4']) }}
9+
{{ $attributes->merge(['class' => 'w-full']) }}
1010
@if (!empty($id)) id="{{ $id }}" @endif
1111

1212
{!! $extra() !!}

0 commit comments

Comments
 (0)