webbuild: source files upload support

This commit is contained in:
Silvan Calarco 2013-06-04 02:46:14 +02:00
parent 0e2960e708
commit becb15708b
4 changed files with 326 additions and 11 deletions

View File

@ -111,3 +111,207 @@ function replaceHTML(el, html) {
to the new element, which can be used to restore variable references. */
return newEl;
};
// ajaxFileUpload
jQuery.extend({
createUploadIframe: function(id, uri)
{
//create frame
var frameId = 'jUploadFrame' + id;
var iframeHtml = '<iframe id="' + frameId + '" name="' + frameId + '" style="position:absolute; top:-9999px; left:-9999px"';
if(window.ActiveXObject)
{
if(typeof uri== 'boolean'){
iframeHtml += ' src="' + 'javascript:false' + '"';
}
else if(typeof uri== 'string'){
iframeHtml += ' src="' + uri + '"';
}
}
iframeHtml += ' />';
jQuery(iframeHtml).appendTo(document.body);
return jQuery('#' + frameId).get(0);
},
createUploadForm: function(id, fileElementId, data)
{
//create form
var formId = 'jUploadForm' + id;
var fileId = 'jUploadFile' + id;
var form = jQuery('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
if(data)
{
for(var i in data)
{
jQuery('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);
}
}
var oldElement = jQuery('#' + fileElementId);
var newElement = jQuery(oldElement).clone();
jQuery(oldElement).attr('id', fileId);
jQuery(oldElement).before(newElement);
jQuery(oldElement).appendTo(form);
//set attributes
jQuery(form).css('position', 'absolute');
jQuery(form).css('top', '-1200px');
jQuery(form).css('left', '-1200px');
jQuery(form).appendTo('body');
return form;
},
ajaxFileUpload: function(s) {
// TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
s = jQuery.extend({}, jQuery.ajaxSettings, s);
var id = new Date().getTime()
var form = jQuery.createUploadForm(id, s.fileElementId, (typeof(s.data)=='undefined'?false:s.data));
var io = jQuery.createUploadIframe(id, s.secureuri);
var frameId = 'jUploadFrame' + id;
var formId = 'jUploadForm' + id;
// Watch for a new set of requests
if ( s.global && ! jQuery.active++ )
{
jQuery.event.trigger( "ajaxStart" );
}
var requestDone = false;
// Create the request object
var xml = {}
if ( s.global )
jQuery.event.trigger("ajaxSend", [xml, s]);
// Wait for a response to come back
var uploadCallback = function(isTimeout)
{
var io = document.getElementById(frameId);
try
{
if(io.contentWindow)
{
xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
}else if(io.contentDocument)
{
xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
}
}catch(e)
{
jQuery.handleError(s, xml, null, e);
}
if ( xml || isTimeout == "timeout")
{
requestDone = true;
var status;
try {
status = isTimeout != "timeout" ? "success" : "error";
// Make sure that the request was successful or notmodified
if ( status != "error" )
{
// process the data (runs the xml through httpData regardless of callback)
var data = jQuery.uploadHttpData( xml, s.dataType );
// If a local callback was specified, fire it and pass it the data
if ( s.success )
s.success( data, status );
// Fire the global callback
if( s.global )
jQuery.event.trigger( "ajaxSuccess", [xml, s] );
} else
jQuery.handleError(s, xml, status);
} catch(e)
{
status = "error";
jQuery.handleError(s, xml, status, e);
}
// The request was completed
if( s.global )
jQuery.event.trigger( "ajaxComplete", [xml, s] );
// Handle the global AJAX counter
if ( s.global && ! --jQuery.active )
jQuery.event.trigger( "ajaxStop" );
// Process result
if ( s.complete )
s.complete(xml, status);
jQuery(io).unbind()
setTimeout(function()
{ try
{
jQuery(io).remove();
jQuery(form).remove();
} catch(e)
{
jQuery.handleError(s, xml, null, e);
}
}, 100)
xml = null
}
}
// Timeout checker
if ( s.timeout > 0 )
{
setTimeout(function(){
// Check to see if the request is still happening
if( !requestDone ) uploadCallback( "timeout" );
}, s.timeout);
}
try
{
var form = jQuery('#' + formId);
jQuery(form).attr('action', s.url);
jQuery(form).attr('method', 'POST');
jQuery(form).attr('target', frameId);
if(form.encoding)
{
jQuery(form).attr('encoding', 'multipart/form-data');
}
else
{
jQuery(form).attr('enctype', 'multipart/form-data');
}
jQuery(form).submit();
} catch(e)
{
jQuery.handleError(s, xml, null, e);
}
jQuery('#' + frameId).load(uploadCallback );
return {abort: function () {}};
},
uploadHttpData: function( r, type ) {
var data = !type;
data = type == "xml" || data ? r.responseXML : r.responseText;
// If the type is "script", eval it in global context
if ( type == "script" )
jQuery.globalEval( data );
// Get the JavaScript object, if JSON is used.
if ( type == "json" )
eval( "data = " + data );
// evaluate scripts within html
if ( type == "html" )
jQuery("<div>").html(data).evalScripts();
return data;
}
})
// end ajaxFileUpload

View File

@ -1,6 +1,7 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head>
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<!-- styles needed by jScrollPane -->
<link type="text/css" href="/scripts/jquery.jscrollpane.css" rel="stylesheet" media="all" >
<!-- latest jQuery direct from google's CDN -->
@ -196,7 +197,54 @@ function ajax_getvalues(request,confirm) {
}
}
//-->
// ajaxFileUpload
function ajaxFileUpload(request)
{
/* $("#loading")
.ajaxStart(function(){
$(this).show();
})
.ajaxComplete(function(){
$(this).hide();
});*/
$.ajaxFileUpload
(
{
url:'/cgi-bin/webbuild',
secureuri:false,
fileElementId:'fileToUpload',
dataType: 'xml',
beforeSend:function()
{
$("#loading").show();
},
complete:function()
{
$("#loading").hide();
},
success: function (data, status)
{
ajax_getvalues(request);
if(typeof(data.error) != 'undefined')
{
if(data.error != '')
{
alert(data.error);
}else
{
alert(data.msg);
}
}
},
error: function (data, status, e)
{
alert(e);
}
}
)
return false;
}
</script>
</head><body>
<table width="100%">

View File

@ -376,7 +376,7 @@ if [ "$REQUEST" = "refresh" -o "$REQUEST" = "refreshjobs" ]; then
echo -n "<img style=\"margin-top:-1px;margin-right:2px;\" width=10 height=10 src=\"/images/inprogress.gif\" alt=\"running...\" title=\"running...\">"
else
if [ $RETCODE -eq 0 ]; then
# echo -n "<font color=green><b>OK</b></font>"
# echo -n "<font color=lightgreen><b>OK</b></font>"
echo -n "<img style=\"margin-top:-1px;margin-right:2px;\" src=\"/images/ok.png\" width=10 height=10 alt=\"OK\" title=\"Job successfully completed\">"
else
# echo -n "<font color=red><b>ERROR ($RETCODE)</b></font>"
@ -920,7 +920,7 @@ if [ "$REQUEST" = "prepare" -o "$REQUEST" = "updatespec" -o "$REQUEST" = "speccr
esac
echo -n "</pre>"
if [ $RET -eq 0 ]; then
echo "Result: <font color=green><b>OK</b></font>"
echo "Result: <font color=lightgreen><b>OK</b></font>"
else
echo "Result: <font color=red><b>ERROR ($RET)</b></font>"
fi
@ -1296,7 +1296,7 @@ if [ "$MAINTAINERMODE" != "true" ]; then
echo -n "SPECTEXT=\"+encodeURIComponent(editor.getValue())+\"&"
echo -n "SENDREPOSITORY=\"+getSelectedValueById('sendrepository')"
echo -n ",\"`cgi_encodevar "Send <b>$PACKAGE</b> built packages to <b>"`\"+getSelectedValueById('sendrepository')+"
echo -n "\"`cgi_encodevar "</b>?<br><br><font color=green>HINT: does summary look short and good?</font><br><b>${SPECVAR_Name[0]} - ${SPECVAR_Summary[0]}.</b>"`\""
echo -n "\"`cgi_encodevar "</b>?<br><br><font color=lightgreen>HINT: does summary look short and good?</font><br><b>${SPECVAR_Name[0]} - ${SPECVAR_Summary[0]}.</b>"`\""
echo -n "); class=redbutton $HREFADD>"
# recreate and send SRPM
if [ "$SPECVAR_CHECK_NOAUTOBUILDREQ" ]; then
@ -1327,6 +1327,15 @@ if [ "$MAINTAINERMODE" != "true" ]; then
echo -n "<br><div style=\"white-space:nowrap;font-size:9px;width:100%;overflow:hidden\"><span style=\"float:left;margin-top:6px;\">$PACKAGE-${SPECVAR_Version}-</span>"
echo -n "<span style=\"display:block;float:right;margin-top:6px;\">.patch</span>"
echo -n "<span style=\"display:block;overflow:hidden;padding-right:5px;\"><input type=text id=addpatchname value=\"\" style=\"width:100%\"></span></div>"
# upload sources
echo -n "<hr><b>Upload source:</b>"
echo -n "<form name=\"form\" action=\"javascript:\" method=\"POST\" enctype=\"multipart/form-data\">"
echo -n "<input id=\"fileToUpload\" type=\"file\" size=\"10\" name=\"fileToUpload\" class=\"input\">"
echo -n "<button class=\"button\" id=\"buttonUpload\" onclick=ajaxFileUpload(\""
echo -n "REQUEST=uploadsource&ENVIRONMENT=$ENVIRONMENT&REPOSITORY=$REPOSITORY&PACKAGE=$PACKAGEENCODED&"
echo -n "SPECTEXT=\"+encodeURIComponent(editor.getValue())+\"&"
echo -n "UPLOADFILENAME=\"+encodeURIComponent(getElementById('fileToUpload').value));>Upload</button>"
echo -n "</form>"
echo -n "</div><hr>"
# info from package sources
echo -n "<b>Info from package in the works:</b><br>"
@ -1626,16 +1635,16 @@ case $REQUEST in
[ $RET -eq 0 ] && break
done
if [ $RET -eq 0 ]; then
echo -n "<br><font color=green>Info: good, patch applies successfully with -p$l option!</font>"
echo -n "<br><font color=lightgreen>Info: good, patch applies successfully with -p$l option!</font>"
echo -n "<br><br><font color=gold>HINT: add the following lines in the appropriate sections:</font><br>"
else
echo -n "<br><font color=red>Warning: patch does not apply with '-p0' to '-p3' options!</font>"
echo "<br><br><font color=gold>HINT: you might want to retry from a clean build dir after pressing the 'unpack' button or check if it applies after changing working subdirectory in build tree.</font>"
echo -n "<br><font color=gold>HINT: add anyway the following lines in the appropriate sections and press 'prepare' to see patch command output:</font><br>"
fi
echo -n "<br><font color=gold>1) in the header after Source[0-9]: and Patch[0-9]: lines:</font><br>"
echo -n "<br><font color=gold>1) in the header after last 'Patch<n>:' entry (or after 'Source<n>:' if this is the first patch) :</font><br>"
if [ "${SPECVAR_Patch[*]}" ]; then
NEWPATCHNUM=$((${SPECVAR_Patch_idx[${#SPECVAR_Patch_idx[@]}-1]}+1))
NEWPATCHNUM=$((${SPECVAR_Patch_idx[${#SPECVAR_Patch[@]}-1]}+1))
else
NEWPATCHNUM=0
fi
@ -1645,9 +1654,39 @@ case $REQUEST in
RET=-1
}
else
echo "Patch URL or destination name not provided."
echo "<font color=red>ERROR: Patch URL or destination name not provided.</font>"
RET=1
fi ;;
"uploadsource") if [ "$UPLOADFILENAME" ]; then
UPLOADFILENAME="${UPLOADFILENAME/*\\}"
if [ -e $WEBBUILD_STATEDIR/uploads/$UPLOADFILENAME ]; then
tmpfile=`mktemp --suffix=webbuildtmp`
mv $WEBBUILD_STATEDIR/uploads/$UPLOADFILENAME $tmpfile
# $SUDO_WRAPPER 0 $ENVIRONMENT "$USER" $PACKAGE "if [ -e $SPECVAR_RPMSOURCESDIR/${UPLOADFILENAME} ]; then echo \"Warning: file already exists in SOURCE directory\"; fi"
$SUDO_WRAPPER 0 $ENVIRONMENT "$USER" $PACKAGE "cp $tmpfile $SPECVAR_RPMSOURCESDIR/${UPLOADFILENAME}" $tmpfile
RET=$?
rm -f $tmpfile
if [ $RET -eq 0 ]; then
echo "<font color=lightgreen>Info: file ${UPLOADFILENAME} successfully added to SOURCES directory.</font>"
echo -n "<br><font color=gold>HINT: add the following line in the header after last 'Source<n>:' entry:</font>"
if [ "${SPECVAR_Source[*]}" ]; then
NEWSOURCENUM=$((${SPECVAR_Source_idx[${#SPECVAR_Source[*]}-1]}+1))
else
NEWSOURCENUM=0
fi
echo -ne "<br><pre>Source$NEWSOURCENUM: ${UPLOADFILENAME}</pre>"
RET=-1
else
echo "<font color=red>ERROR: could not copy file to SOURCES directory.</font>"
fi
else
echo "<font color=red>ERROR: Upload of source file ${UPLOADFILENAME} FAILED.</font>"
RET=-1
fi
else
echo "<font color=red>ERROR: upload filename is empty.</font>"
RET=1
fi ;;
"repositoryimport")
[ "$PACKAGES" ] || {
echo "ERROR: no packages(s) selected."
@ -1833,7 +1872,7 @@ if [ "$REQUEST" != "" ]; then
echo -n "<!-- ENDSCROLL --></pre>"
fi
if [ $RET -eq 0 ]; then
echo "Result: <font color=green><b>OK</b></font>"
echo "Result: <font color=lightgreen><b>OK</b></font>"
elif [ $RET -gt 0 ]; then
echo "Result: <font color=red><b>ERROR ($RET)</b></font>"
fi

View File

@ -56,17 +56,41 @@ function cgi_encodevar() {
# REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p
}
function cgi_get_POST_upload()
# NOTE: only single file is supported
{
local content_disposition content_filename content_type
saveifs=$IFS
IFS=$'\r'
read -r
read -r content_disposition
eval content_filename=${content_disposition/*filename=}
# security, remove any trailing path
content_filename=${content_filename/*\/}
read -r content_type
read -r
IFS=$saveifs
head -n -1 | head -c -2 | cat > $WEBBUILD_STATEDIR/uploads/$content_filename
}
#This code for getting code from post data is from http://oinkzwurgl.org/bash_cgi and
#was written by Phillippe Kehi <phkehi@gmx.net> and flipflip industries
# (internal) routine to store POST data
function cgi_get_POST_vars()
{
# check content type
# FIXME: not sure if we could handle uploads with this..
# Handle uploads
if [ "${CONTENT_TYPE:0:19}" = "multipart/form-data" ]; then
cgi_get_POST_upload "${CONTENT_TYPE/*boundary=}"
return
fi
[ "${CONTENT_TYPE}" != "application/x-www-form-urlencoded" ] && \
echo "Warning: you should probably use MIME type "\
"application/x-www-form-urlencoded!" 1>&2
"application/x-www-form-urlencoded instead of ${CONTENT_TYPE}!" 1>&2
# save POST variables (only first time this is called)
[ -z "$QUERY_STRING_POST" \
-a "$REQUEST_METHOD" = "POST" -a ! -z "$CONTENT_LENGTH" ] &&