Skip to content

Commit 2641586

Browse files
committed
Merge branch 'development' into release
2 parents 6d2cd20 + 33b4688 commit 2641586

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+625
-511
lines changed

app/App/Providers/RouteServiceProvider.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,12 @@ protected function configureRateLimiting(): void
8585
RateLimiter::for('public', function (Request $request) {
8686
return Limit::perMinute(10)->by($request->ip());
8787
});
88+
89+
RateLimiter::for('exports', function (Request $request) {
90+
$user = user();
91+
$attempts = $user->isGuest() ? 4 : 10;
92+
$key = $user->isGuest() ? $request->ip() : $user->id;
93+
return Limit::perMinute($attempts)->by($key);
94+
});
8895
}
8996
}

app/Exports/Controllers/BookExportController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function __construct(
1616
protected ExportFormatter $exportFormatter,
1717
) {
1818
$this->middleware('can:content-export');
19+
$this->middleware('throttle:exports');
1920
}
2021

2122
/**
@@ -75,6 +76,6 @@ public function zip(string $bookSlug, ZipExportBuilder $builder)
7576
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
7677
$zip = $builder->buildForBook($book);
7778

78-
return $this->download()->streamedDirectly(fopen($zip, 'r'), $bookSlug . '.zip', filesize($zip));
79+
return $this->download()->streamedFileDirectly($zip, $bookSlug . '.zip', filesize($zip), true);
7980
}
8081
}

app/Exports/Controllers/ChapterExportController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function __construct(
1616
protected ExportFormatter $exportFormatter,
1717
) {
1818
$this->middleware('can:content-export');
19+
$this->middleware('throttle:exports');
1920
}
2021

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

84-
return $this->download()->streamedDirectly(fopen($zip, 'r'), $chapterSlug . '.zip', filesize($zip));
85+
return $this->download()->streamedFileDirectly($zip, $chapterSlug . '.zip', filesize($zip), true);
8586
}
8687
}

app/Exports/Controllers/PageExportController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function __construct(
1717
protected ExportFormatter $exportFormatter,
1818
) {
1919
$this->middleware('can:content-export');
20+
$this->middleware('throttle:exports');
2021
}
2122

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

88-
return $this->download()->streamedDirectly(fopen($zip, 'r'), $pageSlug . '.zip', filesize($zip));
89+
return $this->download()->streamedFileDirectly($zip, $pageSlug . '.zip', filesize($zip), true);
8990
}
9091
}

app/Exports/PdfGenerator.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,28 @@ protected function renderUsingCommand(string $html): string
9090
$process = Process::fromShellCommandline($command);
9191
$process->setTimeout($timeout);
9292

93+
$cleanup = function () use ($inputHtml, $outputPdf) {
94+
foreach ([$inputHtml, $outputPdf] as $file) {
95+
if (file_exists($file)) {
96+
unlink($file);
97+
}
98+
}
99+
};
100+
93101
try {
94102
$process->run();
95103
} catch (ProcessTimedOutException $e) {
104+
$cleanup();
96105
throw new PdfExportException("PDF Export via command failed due to timeout at {$timeout} second(s)");
97106
}
98107

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

103113
$pdfContents = file_get_contents($outputPdf);
104-
unlink($outputPdf);
114+
$cleanup();
105115

106116
if ($pdfContents === false) {
107117
throw new PdfExportException("PDF Export via command failed, unable to read PDF output file");

app/Exports/ZipExports/ZipExportBuilder.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,27 @@ protected function build(): string
8484
$zip->addEmptyDir('files');
8585

8686
$toRemove = [];
87-
$this->files->extractEach(function ($filePath, $fileRef) use ($zip, &$toRemove) {
88-
$zip->addFile($filePath, "files/$fileRef");
89-
$toRemove[] = $filePath;
90-
});
87+
$addedNames = [];
88+
89+
try {
90+
$this->files->extractEach(function ($filePath, $fileRef) use ($zip, &$toRemove, &$addedNames) {
91+
$entryName = "files/$fileRef";
92+
$zip->addFile($filePath, $entryName);
93+
$toRemove[] = $filePath;
94+
$addedNames[] = $entryName;
95+
});
96+
} catch (\Exception $exception) {
97+
// Cleanup the files we've processed so far and respond back with error
98+
foreach ($toRemove as $file) {
99+
unlink($file);
100+
}
101+
foreach ($addedNames as $name) {
102+
$zip->deleteName($name);
103+
}
104+
$zip->close();
105+
unlink($zipFile);
106+
throw new ZipExportException("Failed to add files for ZIP export, received error: " . $exception->getMessage());
107+
}
91108

92109
$zip->close();
93110

app/Http/DownloadResponseFactory.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class DownloadResponseFactory
1010
{
1111
public function __construct(
12-
protected Request $request
12+
protected Request $request,
1313
) {
1414
}
1515

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

38+
/**
39+
* Create a response that downloads the given file via a stream.
40+
* Has the option to delete the provided file once the stream is closed.
41+
*/
42+
public function streamedFileDirectly(string $filePath, string $fileName, int $fileSize, bool $deleteAfter = false): StreamedResponse
43+
{
44+
$stream = fopen($filePath, 'r');
45+
46+
if ($deleteAfter) {
47+
// Delete the given file if it still exists after the app terminates
48+
$callback = function () use ($filePath) {
49+
if (file_exists($filePath)) {
50+
unlink($filePath);
51+
}
52+
};
53+
54+
// We watch both app terminate and php shutdown to cover both normal app termination
55+
// as well as other potential scenarios (connection termination).
56+
app()->terminating($callback);
57+
register_shutdown_function($callback);
58+
}
59+
60+
return $this->streamedDirectly($stream, $fileName, $fileSize);
61+
}
62+
63+
3864
/**
3965
* Create a file download response that provides the file with a content-type
4066
* correct for the file, in a way so the browser can show the content in browser,

0 commit comments

Comments
 (0)