ankitpokhrel / tus-php

🚀 A pure PHP server and client for the tus resumable upload protocol v1.0.0
https://tus.io
MIT License
1.4k stars 204 forks source link

Client side for Uppy #54

Closed terrylinooo closed 6 years ago

terrylinooo commented 6 years ago

I know the endpoint is set for Tus server.

const uppy = Uppy.Core({debug: true, autoProceed: false})
    .use(Uppy.Dashboard, {target: '#uppyUploader', inline: true})
    .use(Uppy.Tus, {endpoint: 'http://0.0.0.0:8081/files/'})
    .run()
  uppy.on('success', (fileCount) => {
    console.log(`${fileCount} files uploaded`)
  })

But I do not know how to call client, any tips will be appreciated.

ankitpokhrel commented 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:

  1. Setup tus server using tus-php
  2. Pass server url to endpoint in uppy config as you did

Client 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.

terrylinooo commented 6 years ago

Thank you. I finally give up to try uppy. I use your basic example in Codeigniter 3 framework and it works good. Thank you.

terrylinooo commented 6 years ago

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,
            ]);
        }
    }

}
ankitpokhrel commented 6 years ago

Thanks for sharing 👍

hatimdisawala commented 4 years ago

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.