tus-php: Throwing 404 errors in Laravel
Your API seems great ! But i’ve been scratching my head for a few days now to integrate it. I’m still a beginner in Laravel and in php in general but it seems that after following all your integration process (I found it very clear), I guess it throws 405 error at the upload stage.
I already included csrf in the header request.
Ever encountered this issue before ? Help would be much appreciated ! 😃
Routes
//Tus routes
Route::any('/tus/{any?}', function () {
$response = app('tus-server')->serve();
return $response->send();
})->where('any', '.*');
Route::any('/files/{any?}', function () {
$response = app('tus-server')->serve();
return $response->send();
})->where('any', '.*');
Route::get('test', function (){
return view('test');
});
Route::post('verify', 'TusController@TusFile');
Route::post('upload', 'TusController@UploadTus');
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use TusPhp\Exception\FileException;
use TusPhp\Exception\ConnectionException;
use GuzzleHttp\Exception\ConnectException;
use TusPhp\Exception\Exception as TusException;
use GuzzleHttp\Client;
require '/Users/fabrique/code/dancenote/vendor/autoload.php';
class TusController extends Controller
{
/**
* Uploads files in 5mb chunk.
*/
public function TusFile () {
$client = new \TusPhp\Tus\Client('http://dancenote.test/tus');
// Alert: Sanitize all inputs properly in production code
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,
'upload_key' => $uploadKey,
]);
} catch (ConnectException $e) {
echo json_encode([
'status' => 'errorconnect',
'bytes_uploaded' => -1,
]);
} catch (FileException $e) {
echo json_encode([
'status' => 'resume',
'bytes_uploaded' => 0,
'upload_key' => '',
]);
}
} else {
echo json_encode([
'status' => 'error',
'bytes_uploaded' => -1,
]);
}
}
public function UploadTus() {
$client = new \GuzzleHttp\Client(['base_uri'=>'http://dancenote.test/tus', 'debug'=> true);
// Alert: Sanitize all inputs properly in production code
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(5000000); // Chunk of 5 mb
echo json_encode([
'status' => 'uploading',
'bytes_uploaded' => $bytesUploaded,
'upload_key' => $uploadKey
]);
} catch (ConnectionException | FileException | TusException $e) {
echo json_encode([
'status' => 'error',
'bytes_uploaded' => -1,
'upload_key' => '',
'error' => $e->getMessage(),
]);
}
} else {
echo json_encode([
'status' => 'error',
'bytes_uploaded' => -1,
'error' => 'No input!',
]);
}
}
}
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 19 (7 by maintainers)
Forgot to create it. Directory was missing what a fool. It seems to work (pause resume and so on).
Really thank you you’ve been great help !
@alexandrelgy you can find laravel logs in
storage/logs
. Please check the logs and try to figure out the issue first. We can’t help unless you provide us enough info about the issue.