1
0

Imported plupload module git tree as a subtree of gallery3-contrib

This commit is contained in:
Anthony Callegaro 2013-03-06 11:20:07 +01:00
commit ce8e253437
9 changed files with 511 additions and 0 deletions

View File

@ -0,0 +1,138 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2012 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Uploader_Controller extends Controller {
public function index($id) {
$item = ORM::factory("item", $id);
access::required("view", $item);
access::required("add", $item);
if (!$item->is_album()) {
$item = $item->parent();
}
print $this->_get_add_form($item);
}
public function start() {
access::verify_csrf();
batch::start();
}
public function add_photo($id) {
$album = ORM::factory("item", $id);
access::required("view", $album);
access::required("add", $album);
access::verify_csrf();
// The Flash uploader not call /start directly, so simulate it here for now.
if (!batch::in_progress()) {
batch::start();
}
$form = $this->_get_add_form($album);
// Uploadify adds its own field to the form, so validate that separately.
/*$file_validation = new Validation($_FILES);
$file_validation->add_rules(
"Filedata", "upload::valid", "upload::required",
"upload::type[" . implode(",", legal_file::get_extensions()) . "]");*/
if ($form->validate()) {
$temp_filename = upload::save("file");
Event::add("system.shutdown", create_function("", "unlink(\"$temp_filename\");"));
try {
$item = ORM::factory("item");
$item->name = substr(basename($temp_filename), 10); // Skip unique identifier Kohana adds
$item->title = item::convert_filename_to_title($item->name);
$item->parent_id = $album->id;
$item->set_data_file($temp_filename);
// Remove double extensions from the filename - they'll be disallowed in the model but if
// we don't do it here then it'll result in a failed upload.
$item->name = legal_file::smash_extensions($item->name);
$path_info = @pathinfo($temp_filename);
if (array_key_exists("extension", $path_info) &&
in_array(strtolower($path_info["extension"]), array("flv", "mp4", "m4v"))) {
$item->type = "movie";
$item->save();
log::success("content", t("Added a movie"),
html::anchor("movies/$item->id", t("view movie")));
} else {
$item->type = "photo";
$item->save();
log::success("content", t("Added a photo"),
html::anchor("photos/$item->id", t("view photo")));
}
module::event("add_photos_form_completed", $item, $form);
} catch (Exception $e) {
// The Flash uploader has no good way of reporting complex errors, so just keep it simple.
Kohana_Log::add("error", $e->getMessage() . "\n" . $e->getTraceAsString());
// Ugh. I hate to use instanceof, But this beats catching the exception separately since
// we mostly want to treat it the same way as all other exceptions
if ($e instanceof ORM_Validation_Exception) {
Kohana_Log::add("error", "Validation errors: " . print_r($e->validation->errors(), 1));
}
header("HTTP/1.1 500 Internal Server Error");
print "ERROR: " . $e->getMessage();
return;
}
print "FILEID: $item->id";
} else {
header("HTTP/1.1 400 Bad Request");
print "ERROR: " . t("Invalid upload");
}
}
public function status($success_count, $error_count) {
if ($error_count) {
// The "errors" won't be properly pluralized :-/
print t2("Uploaded %count photo (%error errors)",
"Uploaded %count photos (%error errors)",
(int)$success_count,
array("error" => (int)$error_count));
} else {
print t2("Uploaded %count photo", "Uploaded %count photos", $success_count);}
}
public function finish() {
access::verify_csrf();
batch::stop();
json::reply(array("result" => "success"));
}
private function _get_add_form($album) {
$form = new Forge("uploader/finish", "", "post", array("id" => "g-add-photos-form"));
$group = $form->group("add_photos")
->label(t("Add photos to %album_title", array("album_title" => html::purify($album->title))));
$group->plupload("plupload")->album($album);
$group = $form->group("actions");
//$group->plupload_buttons("");
module::event("add_photos_form", $album, $form);
return $form;
}
}

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,73 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2012 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Form_Plupload_Core extends Form_Input {
protected $data = array(
"name" => false,
"type" => "UNKNOWN",
"url" => "",
"text" => "");
public function __construct($name) {
parent::__construct($name);
$this->data["script_data"] = array(
"g3sid" => Session::instance()->id(),
"user_agent" => Input::instance()->server("HTTP_USER_AGENT"),
"csrf" => access::csrf_token());
}
public function album(Item_Model $album) {
$this->data["album"] = $album;
return $this;
}
public function script_data($key, $value) {
$this->data["script_data"][$key] = $value;
}
public function render() {
$v = new View("form_plupload.html");
$v->album = $this->data["album"];
$v->script_data = $this->data["script_data"];
$v->simultaneous_upload_limit = module::get_var("gallery", "simultaneous_upload_limit");
$v->resize_size = module::get_var("gallery", "resize_size");
$v->movies_allowed = (bool) movie::find_ffmpeg();
$v->extensions = legal_file::get_filters();
$v->suhosin_session_encrypt = (bool) ini_get("suhosin.session.encrypt");
list ($toolkit_max_filesize_bytes, $toolkit_max_filesize) = graphics::max_filesize();
$upload_max_filesize = trim(ini_get("upload_max_filesize"));
$upload_max_filesize_bytes = num::convert_to_bytes($upload_max_filesize);
if ($upload_max_filesize_bytes < $toolkit_max_filesize_bytes) {
$v->size_limit_bytes = $upload_max_filesize_bytes;
$v->size_limit = $upload_max_filesize;
} else {
$v->size_limit_bytes = $toolkit_max_filesize_bytes;
$v->size_limit = $toolkit_max_filesize;
}
return $v;
}
public function validate() {
return true;
}
}

View File

@ -0,0 +1,7 @@
name = Plupload
description = "Use Plupload to replaces the Flash based uploader"
version = 1
author_name = "LeTic"
author_url = "http://codex.gallery2.org/User:Letic"
info_url = "http://codex.gallery2.org/Gallery3:Modules:plupload"
discuss_url = "http://gallery.menalto.com/node/107819"

View File

@ -0,0 +1,28 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<?php
/**
* Plupload gallery3 module admin page template
*
* Copyright (C) Anthony Callegaro
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
*-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* A copy of the GPL2 License is available here :
* http://www.gnu.org/licenses/gpl-2.0.html
*/
?>
<div class="g-block">
<h1> <?= t("Plupload") ?> </h1>
<div class="g-block-content">
<p>
<?= t("Plupload admin page") ?>
</p>
</div>
</div>

View File

@ -0,0 +1,261 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<script type="text/javascript" src="<?= url::file("modules/plupload/lib/plupload.js") ?>"></script>
<script type="text/javascript" src="<?= url::file("modules/plupload/lib/plupload.flash.js") ?>"></script>
<script type="text/javascript" src="<?= url::file("modules/plupload/lib/plupload.html5.js") ?>"></script>
<script type="text/javascript">
// Convert divs to queue widgets when the DOM is ready
$("#g-add-photos-canvas").ready($(function() {
var success_count = 0, error_count = 0, updating = 0;
var uploader;
var update_status = function() {
if (updating) {
// poor man's mutex
setTimeout(function() { update_status(); }, 500);
}
updating = 1;
$.get("<?= url::site("uploader/status/_S/_E") ?>"
.replace("_S", success_count).replace("_E", error_count),
function(data) {
$("#g-add-photos-status-message").html(data);
updating = 0;
});
};
function remove_file_queue(file, spanclass) {
$('#g-plupload' + file.id).slideUp("slow").remove();
if (spanclass == "g-success") {
spantext = <?= t("Completed")->for_js() ?>;
} else {
spantext = <?= t("Cancelled")->for_js() ?>;
}
$("#g-add-photos-status ul").append(
"<li id=\"q" + file.id + "\" class=\"" + spanclass + "\"><span></span> - " + spantext + "</li>");
$("#g-add-photos-status li#q" + file.id + " span").text(file.name);
setTimeout(function() { $("#q" + file.id).slideUp("slow").remove() }, 5000);
}
function cancel_all_upload() {
var files = uploader.files;
while (files.length > 0) {
_cancel_upload(files[0]);
}
}
function cancel_upload(id) {
_cancel_upload(uploader.getFile(id));
}
function _cancel_upload(file) {
var status_before = file.status;
remove_file_queue(file, "g-error");
uploader.removeFile(file);
if(uploader.state == plupload.STARTED && status_before == plupload.UPLOADING) {
uploader.stop();
uploader.start();
}
}
uploader = new plupload.Uploader({
// General settings
runtimes : 'html5,flash',
url : "<?= url::site("uploader/add_photo/{$album->id}") ?>",
max_file_size : <?= $size_limit_bytes ?>,
max_file_count: <?= $simultaneous_upload_limit ?>, // user can add no more then 20 files at a time
//chunk_size : '1mb',
unique_names : true,
multiple_queues : true,
browse_button : 'g-add-photos-button',
container : "g-add-photos-canvas",
multipart : true,
multipart_params : <?= json_encode($script_data) ?>,
// Resize images on clientside if we can
resize : {width : <?= $resize_size ?>, height : <?= $resize_size ?>, quality : 90},
// Rename files by clicking on their titles
rename: true,
// Sort files
sortable: true,
// Specify what files to browse for
filters : [
{title : "Image files", extensions : "jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG"},
{title : "Movie files", extensions : "flv,FLV,mp4,MP4,m4v,M4V"},
],
// Flash settings
flash_swf_url : 'modules/plupload/lib/plupload.flash.swf',
onError: function(event, queueID, fileObj, errorObj) {
if (errorObj.type == "HTTP") {
if (errorObj.info == "500") {
error_msg = <?= t("Unable to process this photo")->for_js() ?>;
} else if (errorObj.info == "404") {
error_msg = <?= t("The upload script was not found")->for_js() ?>;
} else if (errorObj.info == "400") {
error_msg = <?= t("This photo is too large (max is %size bytes)",
array("size" => $size_limit))->for_js() ?>;
} else {
msg += (<?= t("Server error: __INFO__ (__TYPE__)")->for_js() ?>
.replace("__INFO__", errorObj.info)
.replace("__TYPE__", errorObj.type));
}
} else if (errorObj.type == "File Size") {
error_msg = <?= t("This photo is too large (max is %size bytes)",
array("size" => $size_limit))->for_js() ?>;
} else {
error_msg = <?= t("Server error: __INFO__ (__TYPE__)")->for_js() ?>
.replace("__INFO__", errorObj.info)
.replace("__TYPE__", errorObj.type);
}
msg = " - <a target=\"_blank\" href=\"http://codex.gallery2.org/Gallery3:Troubleshooting:Uploading\">" +
error_msg + "</a>";
$("#g-add-photos-status ul").append(
"<li id=\"q" + queueID + "\" class=\"g-error\"><span></span>" + msg + "</li>");
$("#g-add-photos-status li#q" + queueID + " span").text(fileObj.name);
$("#g-plupload").uploadifyCancel(queueID);
error_count++;
update_status();
}
});
// http://www.plupload.com/example_jquery_ui.php
//var uploader = $("#g-plupload").plupload('getUploader');
uploader.bind('Init', function(up, params) {
$('#filelist').html("<div><b>Debug :</b>Current runtime: " + params.runtime + "</div>");
});
uploader.init();
uploader.bind('FilesAdded', function(up, files) {
$('#g-add-photos-canvas').append(
'<div id="g-pluploadQueue" class="uploadifyQueue">');
$.each(files, function(i, file) {
$('#g-pluploadQueue').append(
'<div id="g-plupload' + file.id + '" class="uploadifyQueueItem">' +
'<div class="cancel">' +
'<a id="g-plupload' + file.id + 'Cancel" href="#" ><img src="/lib/uploadify/cancel.png" border="0"></a>' +
'</div>' +
'<span class="fileName">"' + file.name + '" (' + plupload.formatSize(file.size) + ')</span>' +
'<span id="g-plupload' + file.id + 'Percentage" class="percentage"></span>' +
'<div class="uploadifyProgress">' +
'<div id="g-plupload' + file.id + 'ProgressBar" class="uploadifyProgressBar"><!--Progress Bar--></div>' +
'</div>' +
'</div>');
$('#g-plupload' + file.id + 'Cancel').bind('click', function(event) {
var reg = /g-plupload(.*)Cancel/g;
cancel_upload(reg.exec(this.id)[1]);
});
});
uploader.start();
if ($("#g-upload-cancel-all").hasClass("ui-state-disabled")) {
$("#g-upload-cancel-all")
.removeClass("ui-state-disabled")
.attr("disabled", null);
$("#g-upload-done")
.addClass("ui-state-disabled")
.attr("disabled", "disabled");
}
up.refresh(); // Reposition Flash/Silverlight
});
uploader.bind('UploadProgress', function(up, file) {
$('#g-plupload' + file.id + 'ProgressBar').css('width', file.percent + '%');
if (file.percent != 100) {
$('#g-plupload' + file.id + 'Percentage').text(' - ' +file.percent + '%');
} else {
$('#g-plupload' + file.id + 'Percentage').text(' - Completed');
}
});
uploader.bind('Error', function(up, err) {
$('#filelist').append("<div>Error: " + err.code +
", Message: " + err.message +
(err.file ? ", File: " + err.file.name : "") +
"</div>"
);
up.refresh(); // Reposition Flash/Silverlight
});
uploader.bind('FileUploaded', function(up, file) {
/* $('#g-plupload' + file.id).slideUp("slow").remove();
$("#g-add-photos-status ul").append(
"<li id=\"q" + file.id + "\" class=\"g-success\"><span></span> - " +
<?= t("Completed")->for_js() ?> + "</li>");
$("#g-add-photos-status li#q" + file.id + " span").text(file.name);
setTimeout(function() { $("#q" + file.id).slideUp("slow").remove() }, 5000); */
remove_file_queue(file, "g-success");
success_count++;
update_status();
});
uploader.bind('UploadComplete', function(up, files) {
$("#g-upload-cancel-all")
.addClass("ui-state-disabled")
.attr("disabled", "disabled");
$("#g-upload-done")
.removeClass("ui-state-disabled")
.attr("disabled", null);
});
$('#g-upload-cancel-all').bind('click', function(event){
cancel_all_upload();
uploader.trigger('UploadComplete');
return false;
});
})
);
</script>
<div>
<? if ($suhosin_session_encrypt || (identity::active_user()->admin && !$movies_allowed)): ?>
<div class="g-message-block g-info">
<? if ($suhosin_session_encrypt): ?>
<p class="g-error">
<?= t("Error: your server is configured to use the <a href=\"%encrypt_url\"><code>suhosin.session.encrypt</code></a> setting from <a href=\"%suhosin_url\">Suhosin</a>. You must disable this setting to upload photos.",
array("encrypt_url" => "http://www.hardened-php.net/suhosin/configuration.html#suhosin.session.encrypt",
"suhosin_url" => "http://www.hardened-php.net/suhosin/")) ?>
</p>
<? endif ?>
<? if (identity::active_user()->admin && !$movies_allowed): ?>
<p class="g-warning">
<?= t("Can't find <i>ffmpeg</i> on your system. Movie uploading disabled. <a href=\"%help_url\">Help!</a>", array("help_url" => "http://codex.gallery2.org/Gallery3:FAQ#Why_does_it_say_I.27m_missing_ffmpeg.3F")) ?>
</p>
<? endif ?>
</div>
<? endif ?>
<div>
<ul class="g-breadcrumbs">
<? foreach ($album->parents() as $i => $parent): ?>
<li<? if ($i == 0) print " class=\"g-first\"" ?>> <?= html::clean($parent->title) ?> </li>
<? endforeach ?>
<li class="g-active"> <?= html::purify($album->title) ?> </li>
</ul>
</div>
<div id="g-add-photos-canvas">
<button id="g-add-photos-button" class="g-button ui-state-default ui-corner-all" href="#"><?= t("Select photos (%size max per file)...", array("size" => $size_limit)) ?></button>
<div id="filelist">No runtime found.</div>
<span id="g-plupload"></span>
</div>
<div id="g-add-photos-status">
<ul id="g-action-status" class="g-message-block">
</ul>
</div>
<!-- Proxy the done request back to our form, since its been ajaxified -->
<div>
<button id="g-upload-done" class="ui-state-default ui-corner-all" onclick="$('#g-add-photos-form').submit();return false;">
<?= t("Done") ?>
</button>
<button id="g-upload-cancel-all" class="ui-state-default ui-corner-all ui-state-disabled" disabled="disabled">
<?= t("Cancel uploads") ?>
</button>
<span id="g-add-photos-status-message" />
</div>
</div>