PHP code example of tourze / tus-upload-server-bundle
1. Go to this page and download the library: Download tourze/tus-upload-server-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tourze / tus-upload-server-bundle example snippets
namespace App\Service;
use League\Flysystem\Filesystem;
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
use Aws\S3\S3Client;
class CustomFilesystemFactory
{
public function createS3Filesystem(): Filesystem
{
$client = new S3Client([
'credentials' => [
'key' => $_ENV['AWS_ACCESS_KEY_ID'],
'secret' => $_ENV['AWS_SECRET_ACCESS_KEY'],
],
'region' => $_ENV['AWS_DEFAULT_REGION'],
'version' => 'latest',
]);
$adapter = new AwsS3V3Adapter($client, $_ENV['AWS_BUCKET']);
return new Filesystem($adapter);
}
}
namespace App\Service;
use Tourze\TusUploadServerBundle\Entity\Upload;
class DateBasedUploadPathStrategy
{
public function generatePath(Upload $upload): string
{
$date = $upload->getCreateTime()->format('Y/m/d');
$hash = substr(md5($upload->getUploadId()), 0, 8);
return sprintf('uploads/%s/%s/%s', $date, $hash, $upload->getFilename());
}
}
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Tourze\TusUploadServerBundle\Event\UploadCompletedEvent;
use Tourze\TusUploadServerBundle\Event\UploadCreatedEvent;
class UploadProcessingSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
UploadCreatedEvent::class => 'onUploadCreated',
UploadCompletedEvent::class => 'onUploadCompleted',
];
}
public function onUploadCreated(UploadCreatedEvent $event): void
{
$upload = $event->getUpload();
// Log upload creation, check quotas, etc.
}
public function onUploadCompleted(UploadCompletedEvent $event): void
{
$upload = $event->getUpload();
// Process completed file, generate thumbnails, scan for viruses, etc.
// Example: Move file to final destination
$finalPath = sprintf('processed/%s', $upload->getFilename());
// ... move file logic
}
}
use Symfony\Contracts\Cache\CacheInterface;
class CachedUploadService
{
public function __construct(
private readonly TusUploadService $tusUploadService,
private readonly CacheInterface $cache
) {}
public function getUpload(string $uploadId): ?Upload
{
return $this->cache->get(
"upload.{$uploadId}",
fn() => $this->tusUploadService->getUpload($uploadId)
);
}
}
use Tourze\TusUploadServerBundle\Service\TusUploadService;
class YourService
{
public function __construct(
private TusUploadService $tusUploadService
) {}
public function processCompletedUpload(string $uploadId): void
{
$upload = $this->tusUploadService->getUpload($uploadId);
if ($upload->isCompleted()) {
$content = $this->tusUploadService->getFileContent($upload);
// Process the file content...
}
}
}
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Tourze\TusUploadServerBundle\Event\UploadCompletedEvent;
class UploadEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
UploadCompletedEvent::class => 'onUploadCompleted',
];
}
public function onUploadCompleted(UploadCompletedEvent $event): void
{
$upload = $event->getUpload();
// Handle completed upload...
}
}