Skip to content

Instantly share code, notes, and snippets.

@renepardon
Created June 22, 2020 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renepardon/e414fe2b3bbaa6e244662932d388790b to your computer and use it in GitHub Desktop.
Save renepardon/e414fe2b3bbaa6e244662932d388790b to your computer and use it in GitHub Desktop.
<?php
namespace Modules\Whatever\Http\Controllers\Api;
use Illuminate\Routing\Controller;
class UploadController extends Controller
{
public function handleUpload()
{
/** @var \TusPhp\Tus\Server $tusServer */
$tusServer = app('tus-server');
/** @var \Symfony\Component\HttpFoundation\Response $response */
$response = $tusServer->serve();
return $response->send();
}
}
<?php
namespace Modules\Whatever\Tests\Feature;
use Ramsey\Uuid\Uuid;
use Tests\TestCase;
class UploadTest extends TestCase
{
public function test_resumable_upload_works()
{
$client = new \TusPhp\Tus\Client('http://localhorst');
$client->setApiPath('/api/whatever/files');
$key = sprintf('video-%s', Uuid::uuid4()->toString());
$name = sprintf("%s.mp4", $key);
$path = sys_get_temp_dir() . '/' . $name;
$stub = __DIR__ . '/stubs/whatever.mp4';
copy($stub, $path);
$client->setKey($key)->file($path, $name);
// Chunk uploading
$bytesUploaded = $client->upload(1000000);
$bytesUploaded = $client->upload(1000000);
$int = $client->file($path, $name)->upload();
$this->assertEquals(filesize($stub), $int);
$this->assertFileExists(storage_path('app/uploads/' . $name));
@unlink(storage_path('app/uploads/' . $name));
}
}
<?php
namespace Modules\Whatever\Providers;
use Illuminate\Support\ServiceProvider;
use Modules\Whatever\Events\VideoUploaded;
use TusPhp\Events\TusEvent;
use TusPhp\Tus\Server as TusServer;
class WhateverServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('tus-server', function ($app) {
$server = new TusServer('redis');
$server->setApiPath('/api/whatever/files');
$server->setUploadDir(storage_path('app/uploads'));
$server->event()->addListener('tus-server.upload.complete', function (TusEvent $event) {
event(new VideoUploaded($event));
});
return $server;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment