Commit e507e99b authored by beuchatp's avatar beuchatp
Browse files

Updated code tab of web interface so that files can not be uploaded...

Updated code tab of web interface so that files can not be uploaded asynchronously and placed in the correct location
parent 6103e57e
......@@ -2,22 +2,9 @@
<div class="body-container">
<main style="text-align:left;">
<br>
<form action="upload.php" method="post" enctype="multipart/form-data">
Upload your code below.
<br>
<br>
<input class="file" type="file" id="myToUpload" name="fileToUpload">
<br>
<input type="submit" value="Upload Code" name="submit">
</form>
<br><br><br>
<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="1000000" />
<br>
<hr class="hr-basic navy">
......@@ -25,9 +12,7 @@
Select your StudentControllerService.<b>cpp</b> file
</label>
<br>
<form id="cppuploadform" action="upload_async.php" method="POST" enctype="multipart/form-data">
<input type="file" id="cppfileinput" name="cppfile" class="file-input" />
</form>
<input type="file" id="cppfileinput" name="cppfile" class="file-input" />
<br>
......@@ -49,9 +34,7 @@
Select your StudentControllerService.<b>yaml</b> file
</label>
<br>
<form id="yamluploadform" action="upload_async.php" method="POST" enctype="multipart/form-data">
<input type="file" id="yamlfileinput" name="yamlfile" class="file-input" />
</form>
<input type="file" id="yamlfileinput" name="yamlfile" class="file-input" />
<br>
......
......@@ -15,13 +15,13 @@ function cppFileInputChangedEvent(event)
{
// Get the file list object
// > There should only be one file
var files = event.target.files;
var files_for_upload = event.target.files;
// Parse the file
parseFileDetails( files[0] , "cppfiledetails" );
parseFileDetails( files_for_upload[0] , "cppfiledetails" );
// Upload the file
uploadFile( files[0] , "text/x-c++src" , "cppfileuploadstatus" , "cppuploadform" );
uploadFile( files_for_upload[0] , "StudentControllerServiceCpp" , "any" , "cppfileuploadstatus" );
}
......@@ -30,100 +30,93 @@ function yamlFileInputChangedEvent(event)
{
// Get the file list object
// > There should only be one file
var files = event.target.files;
var files_for_upload = event.target.files;
// Parse the file
parseFileDetails( files[0] , "yamlfiledetails" );
parseFileDetails( files_for_upload[0] , "yamlfiledetails" );
// Upload the file
uploadFile( files[0] , "application/x-yaml" , "yamlfileuploadstatus" , "yamluploadform" );
uploadFile( files_for_upload[0] , "StudentControllerYaml" , "any" , "yamlfileuploadstatus" );
}
function parseFileDetails(file,detailsID)
function parseFileDetails(file_for_upload,detailsID)
{
document.getElementById(detailsID).innerHTML =
"<p>File information: <br>" +
"name: <strong>" + file.name + "</strong><br>" +
"type: <strong>" + file.type + "</strong><br>" +
"size: <strong>" + file.size + "</strong> bytes" +
"name: <strong>" + file_for_upload.name + "</strong><br>" +
"type: <strong>" + file_for_upload.type + "</strong><br>" +
"size: <strong>" + file_for_upload.size/1000.0 + "</strong> kilobytes" +
"</p>";
// // display an image
// if (file.type.indexOf("image") == 0) {
// var reader = new FileReader();
// reader.onload = function(e) {
// Output(
// "<p><strong>" + file.name + ":</strong><br />" +
// '<img src="' + e.target.result + '" /></p>'
// );
// }
// reader.readAsDataURL(file);
// }
// // display text
// if (file.type.indexOf("text") == 0) {
// var reader = new FileReader();
// reader.onload = function(e) {
// Output(
// "<p><strong>" + file.name + ":</strong></p><pre>" +
// e.target.result.replace(/</g, "&lt;").replace(/>/g, "&gt;") +
// "</pre>"
// );
// }
// reader.readAsText(file);
// }
}
function uploadFile(file,expectedFileType,statusID,formID)
function uploadFile(file_for_upload,file_id_within_dfall_system,expectedFileType,statusID)
{
// Create a variable for sending an AJAX request
var xmlhttp = new XMLHttpRequest();
if (
xmlhttp.upload
&&
file.type == expectedFileType
&&
file.size <= document.getElementById("MAX_FILE_SIZE").value
)
// Check that uploads are possible
var flag_uploads_possible = false;
if (xmlhttp.upload)
{
flag_uploads_possible = true;
}
document.getElementById(statusID).innerHTML = "Upload progress 0%";
// Perform checks on the file type:
// > Note: a cpp file type is: "text/x-c++src"
// a yaml file type is: "application/x-yaml"
var flag_file_type_isOk = false;
if (expectedFileType == "any")
{
flag_file_type_isOk = true;
}
else if (file_for_upload.type == expectedFileType)
{
flag_file_type_isOk = true;
}
// create progress bar
//var o = $id("progress");
//var progress = o.appendChild(document.createElement("p"));
//progress.appendChild(document.createTextNode("upload " + file.name));
// Perform checks on the file size:
var flag_file_size_isOk = false;
if (file_for_upload.size <= document.getElementById("MAX_FILE_SIZE").value)
{
flag_file_size_isOk = true;
}
// Perform the upload if all checks are passed
if ( flag_uploads_possible && flag_file_type_isOk && flag_file_size_isOk)
{
// Initialise the upload progress field
document.getElementById(statusID).innerHTML = "Upload progress 0%";
// progress bar
// Respond to progress update events
xmlhttp.upload.addEventListener("progress", function(event)
{
var percent_complete = parseInt( event.loaded / event.total * 100 );
//progress.style.backgroundPosition = pc + "% 0";
document.getElementById(statusID).innerHTML = "Upload progress " + percent_complete + "%";
}, false);
// file received/failed
// Respond to "ready state change" events
xmlhttp.onreadystatechange = function(event)
{
//if (xmlhttp.readyState == 4) {
// progress.className = (xmlhttp.status == 200 ? "success" : "failure");
//}
if (this.readyState == 4 && this.status == 200)
{
document.getElementById(statusID).innerHTML = "Upload complete." + this.responseText;
var base_string = "";
document.getElementById(statusID).innerHTML = base_string + this.responseText;
}
};
// start upload
xmlhttp.open("POST", "upload_async.php", true);
xmlhttp.setRequestHeader("X_FILENAME", file.name);
xmlhttp.send(file);
// Put the file_for_upload into a "FormData" object
var form_data_for_upload = new FormData();
form_data_for_upload.append( 'file' , file_for_upload );
// Start the upload
// > The syntax is the third argument is an optional
// boolean for whether the request is asynchronous
// or not.
xmlhttp.open("POST", "upload_dfall_system_file.php?dfallfileid=" + file_id_within_dfall_system, true);
xmlhttp.send(form_data_for_upload);
}
else
......@@ -131,20 +124,19 @@ function uploadFile(file,expectedFileType,statusID,formID)
// Display an "ERROR" status
document.getElementById(statusID).innerHTML = "<strong>ERROR:</strong>";
// Check if upload was the cause of the error:
if (!(xmlhttp.upload))
if (!(flag_uploads_possible))
{
document.getElementById(statusID).innerHTML += "<br>" + "XMLHttpRequest() does not support upload.";
}
// Check if file type is the cause of the error
if (!(file.type == expectedFileType))
if (!(flag_file_type_isOk))
{
document.getElementById(statusID).innerHTML += "<br>" + "file type = " + file.type + ", but it is required to be = " + expectedFileType;
document.getElementById(statusID).innerHTML += "<br>" + "file type = " + file_for_upload.type + ", but it is required to be = " + expectedFileType;
}
// Check if file size is the cause of the error
if (!(file.size <= document.getElementById("MAX_FILE_SIZE").value))
if (!(flag_file_size_isOk))
{
document.getElementById(statusID).innerHTML += "<br>" + "file size = " + file.size + ", is greater than the max allowed of " + document.getElementById("MAX_FILE_SIZE").value + " bytes.";
document.getElementById(statusID).innerHTML += "<br>" + "file size = " + file_for_upload.size + ", is greater than the max allowed of " + document.getElementById("MAX_FILE_SIZE").value + " bytes.";
}
}
}
\ No newline at end of file
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Allow certain file formats
if($imageFileType != "cpp") {
echo "Sorry, only C++ files are allowed. ";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
\ No newline at end of file
<?php
echo "DEBUG1";
$fn = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : false);
echo "DEBUG2";
echo "$fn";
if ($fn)
{
echo "DEBUG3";
// AJAX call
file_put_contents(
'uploads/' . $fn,
file_get_contents('php://input')
);
echo "$fn uploaded";
exit();
}
else
{
echo "DEBUG4";
// // form submit
// $files = $_FILES['fileselect'];
// foreach ($files['error'] as $id => $err) {
// if ($err == UPLOAD_ERR_OK) {
// $fn = $files['name'][$id];
// move_uploaded_file(
// $files['tmp_name'][$id],
// 'uploads/' . $fn
// );
// echo "<p>File $fn uploaded.</p>";
// }
// }
}
?>
\ No newline at end of file
<?php
// Get the ID string that specifies
// which file this is within the
// dfall-system
$dfall_system_file_id = $_GET['dfallfileid'];
// Convert the file id to all lower case
$file_id_lower = strtolower( $dfall_system_file_id );
// Check if the uploaded file has an error:
if ( 0 < $_FILES['file']['error'] )
{
// Echo the error message
echo "Upload ERROR: error message = " . $_FILES['file']['error'] . "<br>";
}
// Otherwise, process the uploaded file:
else
{
// Specify the dfall-system base path
// to keep the following code cleaner
$dfall_system_base_path = "/home/www-share/dfall/dfall-system/dfall_ws/src/dfall_pkg/";
//echo "dfall system file id = " . $dfall_system_file_id;
// Specify the upload location based
// on the "dfall_system_file_id"
if ($file_id_lower == "studentcontrollerservicecpp")
{
$upload_location = $dfall_system_base_path . "src/nodes/StudentControllerService.cpp";
}
elseif ($file_id_lower == "studentcontrolleryaml")
{
$upload_location = $dfall_system_base_path . "param/StudentController.yaml";
}
else
{
// Echo that the file id is NOT recognised
echo "ERROR: file id NOT recognised, file id provided = " . $dfall_system_file_id . "<br>";
// Specify a default upload location
$upload_location = "uploads/unknown.txt";
}
// Move the uploaded file to the specified location
move_uploaded_file($_FILES['file']['tmp_name'], $upload_location );
// Echo success
echo "Upload SUCESSFUL.<br>";
// Echo the from and to paths
//echo " Moved " . $_FILES['file']['tmp_name'] . " to " . $upload_location;
// Echo the to path
echo "Upload location = " . $upload_location;
}
?>
\ No newline at end of file
Supports Markdown
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