Skip to content

Commit

Permalink
Source code
Browse files Browse the repository at this point in the history
  • Loading branch information
jenky committed Mar 24, 2015
1 parent 8fb7f61 commit f5a055c
Show file tree
Hide file tree
Showing 14 changed files with 642 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
composer.phar
composer.lock
.DS_Store
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- hhvm

before_script:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction --dev

script: phpunit
80 changes: 80 additions & 0 deletions assets/js/upload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
function createUploader(uploader)
{
var $uploader = $('#uploader-' + uploader);

if (!$uploader || !$uploader.length) {
alert('Cannot find uploader');
}

var $filelist = $uploader.find('.filelist'),
$uploaded = $uploader.find('.uploaded'),
$uploadAction = $uploader.find('.upload-actions'),
$uploadBtn = $('#' + $uploader.data('uploadbtn')) || false,
options = $uploader.data('options') || {},
autoStart = $uploader.data('autostart') || false;

defaultOptions = {
init: {
PostInit: function(up) {
if (!autoStart && $uploadBtn) {
$uploadBtn.click(function() {
$uploadAction.hide();
up.start();
return false;
});
}
},

FilesAdded: function(up, files) {
// $filelist.find('.alert-file button.close').trigger('click'); //limit uploading to 1
// $uploaded.html('');
// $uploadAction.hide();
$.each(files, function(i, file){
$filelist.append(
'<div id="' + file.id + '" class="alert alert-file">' +
'<span class="filename hide">' + file.name + ' (' + plupload.formatSize(file.size) + ') </span> <button type="button" class="close cancelUpload">&times;</button>' +
'<div class="progress progress-striped"><div class="progress-bar" style="width: 1%;"></div></div></div>');

$filelist.on('click', '#' + file.id + ' button.cancelUpload', function(){
$uploadAction.show();
});
});
up.refresh(); // Reposition Flash/Silverlight
if (autoStart) {
$uploadAction.hide();
up.start();
}
},

UploadProgress: function(up, file) {
//if(!$('#' + file.id + ' .progress').hasClass('progress-striped')){
$('#' + file.id + ' .progress').addClass('active');
$('#' + file.id + ' button.cancelUpload').hide();
//}
$('#' + file.id + ' .progress .progress-bar').animate({width: file.percent + '%'}, 100, 'linear');
},

Error: function(up, err) {
$filelist.append('<div class="alert alert-danger"><button type="button" class="close" data-dismiss="alert">&times;</button>' +
'Error: ' + err.code + ', Message: ' + err.message +
(err.file ? ', File: ' + err.file.name : '') +
"</div>"
);
up.refresh(); // Reposition Flash/Silverlight
},

FileUploaded: function(up, file, info) {
var response = JSON.parse(info.response);
$('#' + file.id + ' .progress .progress-bar').animate({width: '100%'}, 100, 'linear');
$('#' + file.id + ' .progress').removeClass('progress-striped').removeClass('active').fadeOut();
$('#' + file.id + ' .filename').removeClass('hide').show();
$('#' + file.id + ' button.cancelUpload').attr('data-id', response.id).show();
}
}
};

$.extend(options, defaultOptions);

var uploader = new plupload.Uploader(options);
uploader.init();
}
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "jenky/laravel-plupload",
"description": "",
"authors": [
{
"name": "Linh Tran",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "5.0.*"
},
"autoload": {
"psr-4": {
"Jenky\\LaravelPlupload": "src/"
}
},
"minimum-stability": "stable"
}
8 changes: 8 additions & 0 deletions config/plupload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
'chunk_path' => storage_path('plupload'),

'flash_swf_url' => asset("assets/plupload/js/Moxie.swf"),
'silverlight_xap_url' => asset("assets/plupload/js/Moxie.xap"),
];
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
5 changes: 5 additions & 0 deletions src/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php namespace Jenky\LaravelPlupload;

class Exception extends \Exception {

}
13 changes: 13 additions & 0 deletions src/Facades/Plupload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php namespace Jenky\LaravelPlupload\Facades;

use Illuminate\Support\Facades\Facade;

class Plupload extends Facade {

/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'plupload'; }
}
167 changes: 167 additions & 0 deletions src/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php namespace Jenky\LaravelPlupload;

use Illuminate\Http\Request;
use Closure;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class File {

/**
* @var Illuminate\Http\Request
*/
protected $request;

/**
* @var Illuminate\Filesystem\Filesystem
*/
protected $storage;

private $maxFileAge = 600; //600 secondes

public function __construct(Request $request)
{
$this->request = $request;
$this->storage = app('filesystem');
}

/**
* Get chuck upload path
*
* @return string
*/
public function getChunkPath()
{
$path = config('plupload.chunk_path');

if (!$this->storage->isDirectory($path))
{
$this->storage->makeDirectory($path, 0777, true);
}

return $path;
}

/**
* Process uploaded files
*
* @param string $name
* @param closure $closure
*
* @return array
*/
public function process($name, Closure $closure)
{
$response = [];
$response['jsonrpc'] = "2.0";

if ($this->hasChunks())
{
$result = $this->chunks($name, $closure);
}
else
{
$result = $this->single($name, $closure);
}

$response['result'] = $result;

return $response;
}

/**
* Handle single uploaded file
*
* @param string $name
* @param closure $closure
*
* @return mixed
*/
public function single($name, Closure $closure)
{
if ($this->request->hasFile($name))
{
return $closure($this->request->file($name));
}
}

/**
* Handle single uploaded file
*
* @param string $name
* @param closure $closure
*
* @return mixed
*/
public function chunks($name, Closure $closure)
{
$result = false;

if ($this->request->hasFile($name))
{
$file = $this->request->file($name);

$chunk = (int) $this->request->get('chunk', false);
$chunks = (int) $this->request->get('chunks', false);
$originalName = $this->request->get('name');

$filePath = $this->getChunkPath() . '/' . $originalName . '.part';

$this->removeOldData($filePath);
$this->appendData($filePath, $file);

if ($chunk == $chunks - 1)
{
$file = new UploadedFile($filePath, $originalName, 'blob', sizeof($filePath), UPLOAD_ERR_OK, true);
$result = $closure($file);
@unlink($filePath);
}
}

return $result;
}

/**
* Remove old chunks
*/
protected function removeOldData($filePath)
{
if ($this->storage->exists($filePath) && ($this->storage->lastModified($filePath) < time() - $this->maxFileAge))
{
$this->storage->delete($filePath);
}
}

/**
* Merge chunks
*/
protected function appendData($filePathPartial, UploadedFile $file)
{
if (!$out = @fopen($filePathPartial, "ab"))
{
throw new Exception("Failed to open output stream.", 102);
}

if (!$in = @fopen($file->getPathname(), "rb"))
{
throw new Exception("Failed to open input stream", 101);
}

while ($buff = fread($in, 4096))
{
fwrite($out, $buff);
}

@fclose($out);
@fclose($in);
}

/**
* Check if request has chunks
*
* @return bool
*/
public function hasChunks()
{
return (bool) $this->request->get('chunks', false);
}
}
Loading

0 comments on commit f5a055c

Please sign in to comment.