Closed terrylinooo closed 6 years ago
@terrylinooo uppy internally uses tus-js-client. When using tus-php with uppy you don't need to do anything but pass the tus endpoint. The step would be something like this:
endpoint
in uppy config as you didClient part is auto handled by uppy. You can refer to this uppy integration example.
How to setup server is defined here. If you are using Laravel/Lumen framework, you can refer this doc.
Thank you. I finally give up to try uppy. I use your basic example in Codeigniter 3 framework and it works good. Thank you.
Here is my code in Codeigniter 3, if someone need it.
<?php
use TusPhp\Exception\FileException;
use TusPhp\Exception\ConnectionException;
use TusPhp\Exception\Exception as TusException;
class TusServer extends MY_Controller
{
public $fileStoreFolder = '';
public $tusServerUrl = '';
public $tusServerPath = '';
public function __construct()
{
parent::__construct();
$this->fileStoreFolder = APPPATH . '../resource/upload';
$this->tusServerUrl = base_url() . 'tusServer/server';
$this->tusServerPath = 'tusServer/server';
$oldUmask = umask(0);
if (!is_dir($this->fileStoreFolder)) {
mkdir($this->fileStoreFolder, 0777, true);
}
umask($oldUmask);
}
/**
* Tus Server
*/
public function server()
{
$server = new \TusPhp\Tus\Server('redis');
$server->setMaxUploadSize(2000000);
$server->setApiPath($this->tusServerPath);
$server->setUploadDir($this->fileStoreFolder);
$response = $server->serve();
$response->send();
exit(0);
}
/**
* Upload
*/
public function upload()
{
$client = new \TusPhp\Tus\Client(base_url(), 'redis');
$client->setApiPath($this->tusServerPath);
if ( ! empty($_FILES)) {
$fileMeta = $_FILES['tus_file'];
$uploadKey = hash_file('md5', $fileMeta['tmp_name']);
try {
$client->setKey($uploadKey)->file($fileMeta['tmp_name'], time() . '_' . $fileMeta['name']);
$bytesUploaded = $client->upload(2000000); // Chunk of 5 mb
echo json_encode([
'status' => 'uploading',
'bytes_uploaded' => $bytesUploaded,
'checksum' => $client->getChecksum(),
]);
} catch (ConnectionException | FileException | TusException $e) {
echo json_encode([
'status' => 'error',
'bytes_uploaded' => -1,
'checksum' => '',
'error' => $e->getMessage(),
]);
}
} else {
echo json_encode([
'status' => 'error',
'bytes_uploaded' => -1,
'error' => 'No input!',
]);
}
}
/**
* Verify
*/
public function verify()
{
$client = new \TusPhp\Tus\Client(base_url(), 'redis');
$client->setApiPath($this->tusServerPath);
if (!empty($_FILES)) {
$status = 'new';
$fileMeta = $_FILES['tus_file'];
$uploadKey = hash_file('md5', $fileMeta['tmp_name']);
try {
$offset = $client->setKey($uploadKey)->file($fileMeta['tmp_name'])->getOffset();
if (false !== $offset) {
$status = $offset >= $fileMeta['size'] ? 'uploaded' : 'resume';
} else {
$offset = 0;
}
echo json_encode([
'status' => $status,
'bytes_uploaded' => $offset,
'checksum' => $client->getChecksum(),
]);
} catch (ConnectionException | ConnectException $e) {
echo json_encode([
'status' => 'error',
'bytes_uploaded' => -1,
]);
} catch (FileException $e) {
echo json_encode([
'status' => 'resume',
'bytes_uploaded' => 0,
'checksum' => '',
]);
}
} else {
echo json_encode([
'status' => 'error',
'bytes_uploaded' => -1,
]);
}
}
}
Thanks for sharing 👍
Here is my code in Codeigniter 3, if someone need it.
<?php use TusPhp\Exception\FileException; use TusPhp\Exception\ConnectionException; use TusPhp\Exception\Exception as TusException; class TusServer extends MY_Controller { public $fileStoreFolder = ''; public $tusServerUrl = ''; public $tusServerPath = ''; public function __construct() { parent::__construct(); $this->fileStoreFolder = APPPATH . '../resource/upload'; $this->tusServerUrl = base_url() . 'tusServer/server'; $this->tusServerPath = 'tusServer/server'; $oldUmask = umask(0); if (!is_dir($this->fileStoreFolder)) { mkdir($this->fileStoreFolder, 0777, true); } umask($oldUmask); } /** * Tus Server */ public function server() { $server = new \TusPhp\Tus\Server('redis'); $server->setMaxUploadSize(2000000); $server->setApiPath($this->tusServerPath); $server->setUploadDir($this->fileStoreFolder); $response = $server->serve(); $response->send(); exit(0); } /** * Upload */ public function upload() { $client = new \TusPhp\Tus\Client(base_url(), 'redis'); $client->setApiPath($this->tusServerPath); if ( ! empty($_FILES)) { $fileMeta = $_FILES['tus_file']; $uploadKey = hash_file('md5', $fileMeta['tmp_name']); try { $client->setKey($uploadKey)->file($fileMeta['tmp_name'], time() . '_' . $fileMeta['name']); $bytesUploaded = $client->upload(2000000); // Chunk of 5 mb echo json_encode([ 'status' => 'uploading', 'bytes_uploaded' => $bytesUploaded, 'checksum' => $client->getChecksum(), ]); } catch (ConnectionException | FileException | TusException $e) { echo json_encode([ 'status' => 'error', 'bytes_uploaded' => -1, 'checksum' => '', 'error' => $e->getMessage(), ]); } } else { echo json_encode([ 'status' => 'error', 'bytes_uploaded' => -1, 'error' => 'No input!', ]); } } /** * Verify */ public function verify() { $client = new \TusPhp\Tus\Client(base_url(), 'redis'); $client->setApiPath($this->tusServerPath); if (!empty($_FILES)) { $status = 'new'; $fileMeta = $_FILES['tus_file']; $uploadKey = hash_file('md5', $fileMeta['tmp_name']); try { $offset = $client->setKey($uploadKey)->file($fileMeta['tmp_name'])->getOffset(); if (false !== $offset) { $status = $offset >= $fileMeta['size'] ? 'uploaded' : 'resume'; } else { $offset = 0; } echo json_encode([ 'status' => $status, 'bytes_uploaded' => $offset, 'checksum' => $client->getChecksum(), ]); } catch (ConnectionException | ConnectException $e) { echo json_encode([ 'status' => 'error', 'bytes_uploaded' => -1, ]); } catch (FileException $e) { echo json_encode([ 'status' => 'resume', 'bytes_uploaded' => 0, 'checksum' => '', ]); } } else { echo json_encode([ 'status' => 'error', 'bytes_uploaded' => -1, ]); } } }
can you give more details of how you put library in application and share sample code with usage in controller.
I know the endpoint is set for Tus server.
But I do not know how to call client, any tips will be appreciated.