PHP code example of railroad / remotestorage

1. Go to this page and download the library: Download railroad/remotestorage library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

railroad / remotestorage example snippets


'providers' => [
    # ...
    \Railroad\RemoteStorage\Providers\RemoteStorageServiceProvider::class,
]

/** @var Railroad\RemoteStorage\Services\RemoteStorageService $remoteStorageService */
protected $remoteStorageService;

public function __constructor(Railroad\RemoteStorage\Services\RemoteStorageService $remoteStorageService){
    $this->remoteStorageService = $remoteStorageService;
}

use Railroad\RemoteStorage\Services;

/** @var RemoteStorageService $remoteStorageService */
protected $remoteStorageService;

public function __constructor(RemoteStorageService $remoteStorageService){
    $this->remoteStorageService = $remoteStorageService;
}

$upload = $this->remoteStorageService->put($filenameRelative, $filenameAbsolute);

/** Upload product thumbnail on remote storage using remotestorage package.
 * Throw an error JSON response if the upload failed or return the uploaded thumbnail url.
 *
 * @param Request $request
 * @return JsonResponse
 */
public function uploadThumbnail(Request $request)
{
    $target = $request->get('target');
    $upload = $this->remoteStorageService->put($target, $request->file('file'));

    throw_if(
        (!$upload),
        new JsonResponse('Upload product thumbnail failed', 400)
    );

    return new JsonResponse(
        $this->remoteStorageService->url($target), 201
    );
}

$file = $this->remoteStorageService->read($filenameRelative);

$exists = $this->remoteStorageService->exists('foo/bar.jpg');

/** 
 * @param Request $request
 * @return JsonResponse
 */
public function uploadThumbnailIfDoesNotAlreadyExist(Request $request)
{
    $target = 'foo/' . $request->get('target');    
    if(!$this->remoteStorageService->exists('foo/')){
        $upload = $this->remoteStorageService->put($target, $request->file('file'));
        throw_if((!$upload), new JsonResponse('Upload product thumbnail failed', 400));
    }
    return new JsonResponse(['exists' => true]);
}

$this->remoteStorageService->delete('foo/bar.jpg');

public function deleteThumbnail(Request $request)
{
    $target = $request->get('target');    
    $delete = $this->remoteStorageService->delete('foo/' . $target);
    throw_if((!$delete), new JsonResponse('product thumbnail deletion failed', 400));
    return new JsonResponse(['deleted' => true]);
}