Giter VIP home page Giter VIP logo

Comments (5)

ankitpokhrel avatar ankitpokhrel commented on May 25, 2024

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

from tus-php.

terrylinooo avatar terrylinooo commented on May 25, 2024

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

from tus-php.

terrylinooo avatar terrylinooo commented on May 25, 2024

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

}

from tus-php.

ankitpokhrel avatar ankitpokhrel commented on May 25, 2024

Thanks for sharing 👍

from tus-php.

hatimdisawala avatar hatimdisawala commented on May 25, 2024

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.

from tus-php.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.