Skip to content

Commit

Permalink
Added functionality to download zips
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdordoy committed Oct 16, 2024
1 parent 50cb9d9 commit dd6f9e2
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 131 deletions.
56 changes: 56 additions & 0 deletions src/Actions/Documents/Download.php
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";

Check failure on line 35 in src/Actions/Documents/Download.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property JamesDordoy\HTMLable\Models\Document::$name.
$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;
}
}
2 changes: 1 addition & 1 deletion src/Actions/Documents/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function importAssets(Document $document, string $assetPath)

foreach ($assetFiles as $file) {
if ($this->isImage($file)) {
$document->addMedia($file->getRealPath())->toMediaCollection();
$document->addMedia($file->getRealPath())->toMediaCollection('images');
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/Actions/RegisterRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@

use Illuminate\Support\Facades\Route;
use JamesDordoy\HTMLable\Http\Controllers\Documents\DocumentsController;
use JamesDordoy\HTMLable\Http\Controllers\Documents\DownloadDocumentController;
use JamesDordoy\HTMLable\Http\Controllers\Documents\RenderDocumentController;
use JamesDordoy\HTMLable\Http\Controllers\Elements\ElementsController;
use JamesDordoy\HTMLable\Http\Controllers\Elements\RenderElementController;
use JamesDordoy\HTMLable\Http\Controllers\Media\ServeMediaController;
use JamesDordoy\HTMLable\Http\Controllers\Media\ServeMediaSignedController;
use JamesDordoy\HTMLable\Http\Controllers\Values\ValuesController;

class RegisterRoutes
{
public function __invoke()
{
Route::resource('/htmlable/documents', DocumentsController::class);

Route::get('/htmlable/documents/{document}/render', RenderDocumentController::class);
Route::get('/htmlable/documents/{document}/download', DownloadDocumentController::class);

Route::resource('/htmlable/elements', ElementsController::class);

Route::get('/htmlable/elements/{element}/render', RenderElementController::class);

Route::resource('/htmlable/values', ValuesController::class);

Route::get('/htmlable/media/{media}/serve-signed', ServeMediaSignedController::class)
Route::get('/htmlable/media/{media}/serve-signed', ServeMediaController::class)
->middleware(['signed'])
->name('htmlable.media.serve-signed');

Expand Down
19 changes: 19 additions & 0 deletions src/Http/Controllers/Documents/DownloadDocumentController.php
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 src/Http/Controllers/Documents/RenderDocumentImageController.php
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 src/Http/Controllers/Documents/RenderDocumentPDFController.php
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();
}
}
3 changes: 2 additions & 1 deletion src/Http/Controllers/Media/ServeMediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Symfony\Component\HttpFoundation\StreamedResponse;

class ServeMediaController extends Controller
{
public function __invoke(Request $request, Media $media)
public function __invoke(Request $request, Media $media): StreamedResponse
{
return $media->toInlineResponse($request);
}
Expand Down
15 changes: 0 additions & 15 deletions src/Http/Controllers/Media/ServeMediaSignedController.php

This file was deleted.

8 changes: 8 additions & 0 deletions src/Models/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public function render(): HtmlString
return new HtmlString("{$docType}{$rootElement->render()}");
}

public function renderWithLocalPaths()
{
$rootElement = $this->elements()->whereNull('parent_id')->first();
$docType = $this->doctype ?? '<!DOCTYPE>';

return new HtmlString("{$docType}{$rootElement->renderForDownload()}");
}

public function parse(HtmlString|string $html)
{
$dom = new DOMDocument;
Expand Down
6 changes: 2 additions & 4 deletions src/Models/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,15 @@ public function render(): HtmlString
return new HtmlString("<{$tag} data-id='{$this->uuid}' {$attributes}>{$html}</{$tag}>");
}

public function renderForDownload()
public function renderForDownload(): HtmlString
{
$tag = $this->tag;

$attributes = $this->values->map(fn ($value) => $value->renderAttributesLocally())->implode(' ');

$contentValue = $this->values->firstWhere('key', 'content')->value ?? '';

$childrenHtml = '';
foreach ($this->children as $child) {
$childrenHtml .= $child->render();
$childrenHtml .= $child->renderForDownload();
}

$html = $contentValue.$childrenHtml;
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function renderAttributesLocally(): string
$segments = collect(explode('/', $path));
$media = Media::find($segments->get(3));

return "{$this->key}=\"./{$media->file_name}\"";
return "{$this->key}=\"./assets/{$media->file_name}\"";
}

return "{$this->key}=\"{$this->value}\"";
Expand Down
32 changes: 0 additions & 32 deletions src/Parsers/ToHTML.php

This file was deleted.

74 changes: 0 additions & 74 deletions src/Parsers/ToJSON.php

This file was deleted.

0 comments on commit dd6f9e2

Please sign in to comment.