Commit 3fa1751b authored by Thomas Persohn's avatar Thomas Persohn
Browse files

Add upload sample

parent ae35afed
/.idea
/vendor
/doc
/cache
/nbproject/private/
/doc/samples/upload/vendor
\ No newline at end of file
{
"name": "libcast/samples-upload",
"require": {
"guzzlehttp/guzzle": "^6.0",
"moxiecode/plupload": "^2.1"
}
}
<?php
/**
* This is a sample of an upload on the Libcast Platform using Composer, Plupload and Guzzle
* Composer installation instructions https://getcomposer.org/
* Guzzle installation instructions http://guzzle.readthedocs.org/en/latest/overview.html#installation
* Plupload installation instructions http://www.plupload.com/download/
*
* In order to load the dependencies, type in the following commands:
* composer init
* composer require guzzlehttp/guzzle
* composer require moxiecode/plupload
* composer update
*/
// Configuration
$config = [
// The folder URL is retrieved from the API
// The root folder is located at /services/files
// Other folders are located at /services/files/<folder-slug>
'folder_url' => 'http://dev.console.libcast.fs.thomas.libcast.net:8080/services/files',
// Provide your login/password information. The digest protocol must NOT be changed
'auth' => ['admin', 'password', 'digest'],
];
require_once 'vendor/autoload.php';
/*
* STEP 1/2 : get the form URL with a webservice call
*/
// The webservice must be call with a POST method
$serviceUrl = $config['folder_url'] . '/upload_links';
$client = new GuzzleHttp\Client;
$response = $client->post($serviceUrl, ['auth' => $config['auth']]);
// Extract URL from XML response
$uploadUrlElem = new SimpleXMLElement($response->getBody());
foreach ($uploadUrlElem as $link) {
// Valid response formats are 'html', 'json'
// We choose the json response format
if ($link['rel'] == 'json') {
$uploadUrl = $link['href'];
}
}
/*
* STEP 2/2 : Build the form with Plupload and chunks enabled
* See http://www.plupload.com/docs/Chunking
*/
// This minimal sample is adapted from the official documentation:
// http://www.plupload.com/docs/Getting-Started
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Libcast Upload Sample with Guzzle</title>
<script type="text/javascript" src="vendor/moxiecode/plupload/js/plupload.full.min.js"></script>
</head>
<body>
<ul id="filelist"></ul>
<br />
<div id="container">
<a id="browse" href="javascript:;">[Browse...]</a>
<a id="start-upload" href="javascript:;">[Start Upload]</a>
</div>
<br />
<pre id="console"></pre>
<script type="text/javascript">
var uploader = new plupload.Uploader({
// Required parameter to allow chunking
required_features: "send_browser_cookies",
// this can be an id of a DOM element or the DOM element itself
browse_button: 'browse',
// The upload URL previously generated by the webservice
url: '<?php echo $uploadUrl ?>',
// file_data_name must NOT be changed
file_data_name: 'file[path]',
// The chunk size on Libcast Platform is 64mb. Any other value may lead to upload failures
chunk_size : '64mb',
multipart_params : { 'error' : true }
});
uploader.init();
uploader.bind('FilesAdded', function(up, files) {
var html = '';
plupload.each(files, function(file) {
html += '<li id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></li>';
});
document.getElementById('filelist').innerHTML += html;
});
uploader.bind('UploadProgress', function(up, file) {
document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
});
uploader.bind('Error', function(up, err) {
document.getElementById('console').innerHTML += "\nError #" + err.code + ": " + err.message;
});
document.getElementById('start-upload').onclick = function() {
uploader.start();
};
</script>
</body>
</html>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment