<?php  // vim:set et ts=4 sw=4 sts=4:
# Copyright (C) 2003-2014 hipplay Inc.
#
# This is UNPUBLISHED PROPRIETARY SOURCE CODE OF hipplay Inc.
# The copyright notice above does not evidence any actual or intended
# publication of such source code. The contents of this file may not
# be disclosed to third parties, copied or duplicated in any form, in
# whole or in part, without the prior written permission of
# hipplay Inc.
#
namespace HipPlayConnect\REST\services\files;



use HipPlayConnect\FileSystem\AxImageFile;
use HipPlayConnect\FileSystem\AxRegularFile;
use HipPlayConnect\REST\AxAbstractRESTResource;
use HipPlayConnect\REST\AxRESTResourceInterface;
use HipPlayConnect\REST\AxRESTResourceNotFoundException;
use HipPlayConnect\REST\AxRESTResponse;

class AxFileRESTResource extends AxAbstractRESTResource implements AxRESTResourceInterface {

    /**
     * List of valid subresources for this resource type.
     *
     * @var array
     */
    protected static $SUBRESOURCES =
            array
            (
                    'webview' => TRUE,
                    'src'     => TRUE,
                    'index'   => TRUE
            );

    /**
     * The file object we are accessing
     *
     * @var AxImageFile
     */
    protected $file = NULL;

    /**
     * The filesystem path of the file we are accessing
     * @var string
     */
    protected $fsPath;

    /**
     * The subresource being requested
     * @var string
     */
    protected $subresource;

    /**
     * Create a new AxFileRESTResource object.
     * @param string $fsPath      The filesystem path of the file to present
     * @param string $subresource The subresource being requested
     *
     * @throws AxRESTResourceNotFoundException
     */
    public function __construct ($fsPath, $subresource) {
        $this->fsPath = $fsPath;
        $this->subresource = $subresource;

        if (!array_key_exists($subresource, self::$SUBRESOURCES))
            throw new AxRESTResourceNotFoundException();

        $this->file = AxRegularFile::getFile($fsPath, NULL, 'AxFilesRESTService'); // factory
    }

    public function getFile () {
        return $this->file;
    }

    public function GET (array $queryParams = array()) {
        switch ($this->subresource) {
            case 'webview':
                // Generate the thumb on demand
                $thumbPath = $this->file->generateView($this->subresource);

                // Return a 404 if the thumbs failed to generate
                if ($thumbPath == NULL)
                    return new AxRESTResponse(404);

                // Only delete thumbs that are not in the final resting place
                $cachedThumbPath = $this->file->getViewFSPathFor( $this->subresource, $this->fsPath );
                $delete = ($thumbPath != $cachedThumbPath);

                // Send the thumbnail to the client
                AxRegularFile::downloadFileFromPath($thumbPath, TRUE, FALSE, $delete);
                break;

            case 'src':
                $this->file->download();
                break;

            case 'index':
                // TODO: Return all of the file metadata for the MediaConnectIndexer
                break;
        }

        return new AxRESTResponse(405);
    }

}