generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added functionality to download zips
- Loading branch information
1 parent
50cb9d9
commit dd6f9e2
Showing
13 changed files
with
121 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace JamesDordoy\HTMLable\Actions\Documents; | ||
|
||
use JamesDordoy\HTMLable\Models\Document; | ||
use Spatie\TemporaryDirectory\TemporaryDirectory; | ||
use ZipArchive; | ||
|
||
class Download | ||
{ | ||
public function __invoke(Document $document): string | ||
{ | ||
$temporaryDirectory = (new TemporaryDirectory())->create(); | ||
|
||
$tempPath = $temporaryDirectory->path(); | ||
|
||
$htmlContent = $document->renderWithLocalPaths(); | ||
|
||
$htmlFilePath = $tempPath . '/index.html'; | ||
file_put_contents($htmlFilePath, $htmlContent); | ||
|
||
$assetsDir = $tempPath . '/assets'; | ||
if (!file_exists($assetsDir)) { | ||
mkdir($assetsDir, 0755, true); | ||
} | ||
|
||
foreach ($document->getMedia('images') as $media) { | ||
// Copy the image to the 'assets' folder | ||
$mediaPath = $media->getPath(); | ||
$fileName = $media->file_name; | ||
copy($mediaPath, $assetsDir . '/' . $fileName); | ||
} | ||
|
||
// Step 4: Create a ZIP file | ||
$zipFileName = "{$document->name}.zip"; | ||
$zipFilePath = $tempPath . '/' . $zipFileName; | ||
|
||
$zip = new ZipArchive(); | ||
if ($zip->open($zipFilePath, ZipArchive::CREATE) === TRUE) { | ||
// Add the HTML file | ||
$zip->addFile($htmlFilePath, 'index.html'); | ||
|
||
// Add the images from the 'assets' folder | ||
$files = scandir($assetsDir); | ||
foreach ($files as $file) { | ||
if ($file != "." && $file != "..") { | ||
$zip->addFile($assetsDir . '/' . $file, 'assets/' . $file); | ||
} | ||
} | ||
|
||
$zip->close(); | ||
} | ||
|
||
return $zipFilePath; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Http/Controllers/Documents/DownloadDocumentController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace JamesDordoy\HTMLable\Http\Controllers\Documents; | ||
|
||
use JamesDordoy\HTMLable\Actions\Documents\Download; | ||
use JamesDordoy\HTMLable\Models\Document; | ||
|
||
class DownloadDocumentController | ||
{ | ||
public function __invoke(Document $document) | ||
{ | ||
$path = app(Download::class)(document: $document); | ||
|
||
return response()->download(file: $path)->deleteFileAfterSend(true); | ||
} | ||
} | ||
|
||
|
||
|
14 changes: 14 additions & 0 deletions
14
src/Http/Controllers/Documents/RenderDocumentImageController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace JamesDordoy\HTMLable\Http\Controllers\Documents; | ||
|
||
use Illuminate\Support\HtmlString; | ||
use JamesDordoy\HTMLable\Models\Document; | ||
|
||
class RenderDocumentImageController | ||
{ | ||
public function __invoke(Document $document): HtmlString | ||
{ | ||
return $document->render(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Http/Controllers/Documents/RenderDocumentPDFController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace JamesDordoy\HTMLable\Http\Controllers\Documents; | ||
|
||
use Illuminate\Support\HtmlString; | ||
use JamesDordoy\HTMLable\Models\Document; | ||
|
||
class RenderDocumentPDFController | ||
{ | ||
public function __invoke(Document $document): HtmlString | ||
{ | ||
return $document->render(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.