Skip to content

Commit

Permalink
Merge branch 'development' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddanbrown committed Jan 4, 2025
2 parents 6d2cd20 + 33b4688 commit 2641586
Show file tree
Hide file tree
Showing 63 changed files with 625 additions and 511 deletions.
7 changes: 7 additions & 0 deletions app/App/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,12 @@ protected function configureRateLimiting(): void
RateLimiter::for('public', function (Request $request) {
return Limit::perMinute(10)->by($request->ip());
});

RateLimiter::for('exports', function (Request $request) {
$user = user();
$attempts = $user->isGuest() ? 4 : 10;
$key = $user->isGuest() ? $request->ip() : $user->id;
return Limit::perMinute($attempts)->by($key);
});
}
}
3 changes: 2 additions & 1 deletion app/Exports/Controllers/BookExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function __construct(
protected ExportFormatter $exportFormatter,
) {
$this->middleware('can:content-export');
$this->middleware('throttle:exports');
}

/**
Expand Down Expand Up @@ -75,6 +76,6 @@ public function zip(string $bookSlug, ZipExportBuilder $builder)
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
$zip = $builder->buildForBook($book);

return $this->download()->streamedDirectly(fopen($zip, 'r'), $bookSlug . '.zip', filesize($zip));
return $this->download()->streamedFileDirectly($zip, $bookSlug . '.zip', filesize($zip), true);
}
}
3 changes: 2 additions & 1 deletion app/Exports/Controllers/ChapterExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function __construct(
protected ExportFormatter $exportFormatter,
) {
$this->middleware('can:content-export');
$this->middleware('throttle:exports');
}

/**
Expand Down Expand Up @@ -81,6 +82,6 @@ public function zip(string $bookSlug, string $chapterSlug, ZipExportBuilder $bui
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
$zip = $builder->buildForChapter($chapter);

return $this->download()->streamedDirectly(fopen($zip, 'r'), $chapterSlug . '.zip', filesize($zip));
return $this->download()->streamedFileDirectly($zip, $chapterSlug . '.zip', filesize($zip), true);
}
}
3 changes: 2 additions & 1 deletion app/Exports/Controllers/PageExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct(
protected ExportFormatter $exportFormatter,
) {
$this->middleware('can:content-export');
$this->middleware('throttle:exports');
}

/**
Expand Down Expand Up @@ -85,6 +86,6 @@ public function zip(string $bookSlug, string $pageSlug, ZipExportBuilder $builde
$page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
$zip = $builder->buildForPage($page);

return $this->download()->streamedDirectly(fopen($zip, 'r'), $pageSlug . '.zip', filesize($zip));
return $this->download()->streamedFileDirectly($zip, $pageSlug . '.zip', filesize($zip), true);
}
}
12 changes: 11 additions & 1 deletion app/Exports/PdfGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,28 @@ protected function renderUsingCommand(string $html): string
$process = Process::fromShellCommandline($command);
$process->setTimeout($timeout);

$cleanup = function () use ($inputHtml, $outputPdf) {
foreach ([$inputHtml, $outputPdf] as $file) {
if (file_exists($file)) {
unlink($file);
}
}
};

try {
$process->run();
} catch (ProcessTimedOutException $e) {
$cleanup();
throw new PdfExportException("PDF Export via command failed due to timeout at {$timeout} second(s)");
}

if (!$process->isSuccessful()) {
$cleanup();
throw new PdfExportException("PDF Export via command failed with exit code {$process->getExitCode()}, stdout: {$process->getOutput()}, stderr: {$process->getErrorOutput()}");
}

$pdfContents = file_get_contents($outputPdf);
unlink($outputPdf);
$cleanup();

if ($pdfContents === false) {
throw new PdfExportException("PDF Export via command failed, unable to read PDF output file");
Expand Down
25 changes: 21 additions & 4 deletions app/Exports/ZipExports/ZipExportBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,27 @@ protected function build(): string
$zip->addEmptyDir('files');

$toRemove = [];
$this->files->extractEach(function ($filePath, $fileRef) use ($zip, &$toRemove) {
$zip->addFile($filePath, "files/$fileRef");
$toRemove[] = $filePath;
});
$addedNames = [];

try {
$this->files->extractEach(function ($filePath, $fileRef) use ($zip, &$toRemove, &$addedNames) {
$entryName = "files/$fileRef";
$zip->addFile($filePath, $entryName);
$toRemove[] = $filePath;
$addedNames[] = $entryName;
});
} catch (\Exception $exception) {
// Cleanup the files we've processed so far and respond back with error
foreach ($toRemove as $file) {
unlink($file);
}
foreach ($addedNames as $name) {
$zip->deleteName($name);
}
$zip->close();
unlink($zipFile);
throw new ZipExportException("Failed to add files for ZIP export, received error: " . $exception->getMessage());
}

$zip->close();

Expand Down
28 changes: 27 additions & 1 deletion app/Http/DownloadResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class DownloadResponseFactory
{
public function __construct(
protected Request $request
protected Request $request,
) {
}

Expand All @@ -35,6 +35,32 @@ public function streamedDirectly($stream, string $fileName, int $fileSize): Stre
);
}

/**
* Create a response that downloads the given file via a stream.
* Has the option to delete the provided file once the stream is closed.
*/
public function streamedFileDirectly(string $filePath, string $fileName, int $fileSize, bool $deleteAfter = false): StreamedResponse
{
$stream = fopen($filePath, 'r');

if ($deleteAfter) {
// Delete the given file if it still exists after the app terminates
$callback = function () use ($filePath) {
if (file_exists($filePath)) {
unlink($filePath);
}
};

// We watch both app terminate and php shutdown to cover both normal app termination
// as well as other potential scenarios (connection termination).
app()->terminating($callback);
register_shutdown_function($callback);
}

return $this->streamedDirectly($stream, $fileName, $fileSize);
}


/**
* Create a file download response that provides the file with a content-type
* correct for the file, in a way so the browser can show the content in browser,
Expand Down
Loading

0 comments on commit 2641586

Please sign in to comment.