Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File Upload In Restler #107

Closed
dragoninfrontofme opened this issue Jan 30, 2013 · 5 comments
Closed

File Upload In Restler #107

dragoninfrontofme opened this issue Jan 30, 2013 · 5 comments
Assignees

Comments

@dragoninfrontofme
Copy link

Hi Luracast,

Just quick check with you guys, is there any example for upload File in Restler?
and Is restler support streaming mechanism for uploading file / image?

Thank you
Andi

@Arul-
Copy link
Member

Arul- commented Jan 30, 2013

Upload is not yet supported in the current v3 version. You will need a format to enable it. We will be adding new format to restler called UploadFormat, once you add that to $r->setSupportedFormats you will be able to upload

Till then, you can use the following code for UploadFormat

<?php
namespace Luracast\Restler\Format;

use Luracast\Restler\RestException;

/**
 * Support for Multi Part Form Data and File Uploads
 *
 * @category   Framework
 * @package    Restler
 * @subpackage format
 * @author     R.Arul Kumaran <arul@luracast.com>
 * @copyright  2010 Luracast
 * @license    http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link       http://luracast.com/products/restler/
 * @version    3.0.0rc3
 */
class UploadFormat extends Format
{
    const MIME = 'multipart/form-data';
    const EXTENSION = 'post';

    /**
     * use it if you need to restrict uploads based on file type
     * setting it as an empty array allows all file types
     * default is to allow only png and jpeg images
     *
     * @var array
     */
    public static $allowedMimeTypes = array('image/jpeg', 'image/png');

    /**
     * use it to restrict uploads based on file size
     * set it to 0 to allow all sizes
     * please note that it upload restrictions in the server
     * takes precedence so it has to be lower than or equal to that
     * default value is 1MB (1024x1024)bytes
     * usual value for the server is 8388608
     *
     * @var int
     */
    public static $maximumFileSize = 1048576;

    /**
     * Your own validation function for validating each uploaded file
     * it can return false or throw an exception for invalid file
     * use anonymous function / closure in PHP 5.3 and above
     * use function name in other cases
     *
     * @var Callable
     */
    public static $customValidationFunction;

    public function encode($data, $humanReadable = false)
    {
        throw new RestException(500, 'UploadFormat is read only');
    }

    public function decode($data)
    {
        $doMimeCheck = !empty(self::$allowedMimeTypes);
        $doSizeCheck = self::$maximumFileSize ? TRUE : FALSE;
        //validate
        foreach ($_FILES as $index => $file) {
            if ($file['error']) {
                //server is throwing an error
                //assume that the error is due to maximum size limit
                throw new RestException(413, "Uploaded file ({$file['name']}) is too big.");
            }
            if ($doMimeCheck && !in_array($file['type'],
                self::$allowedMimeTypes)
            ) {
                throw new RestException(403, "File type ({$file['type']}) is not supported.");
            }
            if ($doSizeCheck && $file['size'] > self::$maximumFileSize) {
                throw new RestException(413, "Uploaded file ({$file['name']}) is too big.");
            }
            if (self::$customValidationFunction) {
                if (!call_user_func(self::$customValidationFunction, $file)) {
                    throw new RestException(403, "File ({$file['name']}) is not supported.");
                }
            }
        }
        //sort file order if needed;
        return $_FILES + $_POST;
    }

}

For some background you may read #26

@ghost
Copy link

ghost commented Jun 28, 2015

Where do i have to add this file?? In Restler>restler>UploadFormat.php

I am working with v2.

@Arul-
Copy link
Member

Arul- commented Jun 28, 2015

Since you use Restler 2, You need to use the UploadFormat as a reference to write your own that matches the iAuthenticate interface in v2 and then place it in a folder that is part of the include path

@ghost
Copy link

ghost commented Jun 28, 2015

Arul:

I have done following things, but after that browser failed to load the page, Let me know how can i make restler v2.0 to accept images/files:

  1. In index.php: $r->setSupportedFormats('JsonFormat', 'XmlFormat', 'UploadFormat');
  2. In restler.php, added the code:

class UploadFormat implements iFormat
{
const MIME = 'multipart/form-data';
const EXTENSION = 'post';

/**
 * use it if you need to restrict uploads based on file type
 * setting it as an empty array allows all file types
 * default is to allow only png and jpeg images
 *
 * @var array
 */
public static $allowedMimeTypes = array('image/jpeg', 'image/png');

/**
 * use it to restrict uploads based on file size
 * set it to 0 to allow all sizes
 * please note that it upload restrictions in the server
 * takes precedence so it has to be lower than or equal to that
 * default value is 1MB (1024x1024)bytes
 * usual value for the server is 8388608
 *
 * @var int
 */
public static $maximumFileSize = 1048576;

/**
 * Your own validation function for validating each uploaded file
 * it can return false or throw an exception for invalid file
 * use anonymous function / closure in PHP 5.3 and above
 * use function name in other cases
 *
 * @var Callable
 */
public static $customValidationFunction;

public function encode($data, $humanReadable = false)
{
    throw new RestException(500, 'UploadFormat is read only');
}

public function decode($data)
{
    $doMimeCheck = !empty(self::$allowedMimeTypes);
    $doSizeCheck = self::$maximumFileSize ? TRUE : FALSE;
    //validate
    foreach ($_FILES as $index => $file) {
        if ($file['error']) {
            //server is throwing an error
            //assume that the error is due to maximum size limit
            throw new RestException(413, "Uploaded file ({$file['name']}) is too big.");
        }
        if ($doMimeCheck && !in_array($file['type'],
            self::$allowedMimeTypes)
        ) {
            throw new RestException(403, "File type ({$file['type']}) is not supported.");
        }
        if ($doSizeCheck && $file['size'] > self::$maximumFileSize) {
            throw new RestException(413, "Uploaded file ({$file['name']}) is too big.");
        }
        if (self::$customValidationFunction) {
            if (!call_user_func(self::$customValidationFunction, $file)) {
                throw new RestException(403, "File ({$file['name']}) is not supported.");
            }
        }
    }
    //sort file order if needed;
    return $_FILES + $_POST;
}

}

@ghost
Copy link

ghost commented Jun 29, 2015

I think file upload is working for me without above code also in restler v2.0. I'll get back to you soon with the update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants