2.0.3: Monkeys are great at storage and do dependency injection
- Implemented the MongoDB / GridFS storage backend;
- Refactor of the
StorageInterface
; - Multiple improvements related to dependency injection.
MongoDB Storage Backend
// Connect to your MongDB
$con = new \MongoDB\Client($configs->uri, $configs->options]);
$mongodb= $con->selectDatabase($configs->db]);
// Start ThunderTUS
$server = new \ThunderTUS\Server($request, $response);
// Load the MongoDB backend
$mongoBackend = new MongoDB($mongodb);
$server->setStorageBackend($mongoBackend );
// Set other settings and process requests
$server->setUploadMaxFileSize(50000);
$server->setApiPath("/tus");
$server->handle();
// Send the response back to the client
$response = $server->getResponse();
Process / Fetch Finished Uploads
Since StorageInterface
was refactored every storage backend must now implement public function fetchFromStorage(string $name, string $destinationDirectory): bool
.
This method is used to fetch a finished upload from the backend storage. It abstracts backend storage file retrivel in a way that the programmer doen't need to know what backend storage is being used at all times. This is useful when the TUS Server is provided by some kind of Service Provider in a dependency injection scenarios. Here's an example of how to retrieve an uploaded file:
$finalStorageDirectory = "/var/www/html/uploads";
$server = new Server();
$status = $server->fetchFromStorage($filename, $finalStorageDirectory);
if (!$status) {
throw new \Exception("Could not fetch ({$filename}) from storage backend: not found.");
}
The file will be deleted from the storage backend and stored in $finalStorageDirectory
directory.
ThunderTUS & Dependency Injection
This release also make it easier to use thunder-tus-php in dependency injection scenarios. You can now delay providing the http request and response implementations until later. This allows you, for instance, to instantiate the Server in a Service Provider and use it in a controller.
Here is an example of a ThunderTUS service provider:
public static function register()
{
$settings = $this->container->get("settings.tusProtocol");
// Create the server
$server = new Server(); // No request or response implementations passed here
// Load the filesystem Backend
$backend = new FileSystem($settings->path]);
$server->setStorageBackend($backend);
// Set TUS upload parameters
$server->setUploadMaxFileSize((int)$settings->maxSize]);
$server->setApiPath($settings->endpoint]);
return $server;
}
Now in your controller responsible for handling uploads you might do something like:
public function upload()
{
// Resolve TUS using the container
/** @var \ThunderTUS\Server $server */
$server = $this->container->get(\ThunderTUS\Server::class);
// Load the request or response implementations here!
$server->loadHTTPInterfaces($this->request, $this->response);
// Handle the upload request
$server->handle();
// Send the response back to the client
return $server->getResponse();
}