rearranged preview code

This commit is contained in:
bill bittner 2017-10-03 10:05:09 -07:00
parent 3a3ad9926b
commit bed72b7e94
9 changed files with 65 additions and 105 deletions

View file

@ -26,14 +26,7 @@ module.exports = {
res.status(400).send(error); res.status(400).send(error);
} }
}, },
handlePublishError (error) { useObjectPropertiesIfNoKeys (err) {
logger.error('Publish Error:', useObjectPropertiesIfNoKeys(error)); return useObjectPropertiesIfNoKeys(err);
if (error.code === 'ECONNREFUSED') {
return 'Connection refused. The daemon may not be running.';
} else if (error.response.data.error) {
return error.response.data.error.message;
} else {
return error;
}
}, },
}; };

View file

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -6,7 +6,6 @@ function getRequest (url) {
xhttp.responseType = 'json'; xhttp.responseType = 'json';
xhttp.onreadystatechange = () => { xhttp.onreadystatechange = () => {
if (xhttp.readyState == 4 ) { if (xhttp.readyState == 4 ) {
console.log(xhttp);
if ( xhttp.status == 200) { if ( xhttp.status == 200) {
console.log('response:', xhttp.response); console.log('response:', xhttp.response);
resolve(xhttp.response); resolve(xhttp.response);
@ -28,7 +27,6 @@ function postRequest (url, params) {
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.onreadystatechange = () => { xhttp.onreadystatechange = () => {
if (xhttp.readyState == 4 ) { if (xhttp.readyState == 4 ) {
console.log(xhttp);
if ( xhttp.status == 200) { if ( xhttp.status == 200) {
console.log('response:', xhttp.response); console.log('response:', xhttp.response);
resolve(xhttp.response); resolve(xhttp.response);

View file

@ -1,26 +1,39 @@
/* publish functions */ /* publish functions */
function cancelPublish () {
window.location.href = '/';
}
// When a file is selected for publish, validate that file and // When a file is selected for publish, validate that file and
// stage it so it will be ready when the publish button is clicked. // stage it so it will be ready when the publish button is clicked.
function previewAndStageFile(selectedFile){ function previewAndStageFile(selectedFile){
const publishForm = document.getElementById('publish-form-wrapper'); const publishForm = document.getElementById('publish-form-wrapper');
var previewHolder = document.getElementById('asset-preview-holder'); const previewHolder = document.getElementById('asset-preview-holder');
var dropzoneWrapper = document.getElementById('publish-dropzone-wrapper'); const dropzoneWrapper = document.getElementById('publish-dropzone-wrapper');
var previewReader = new FileReader(); const previewReader = new FileReader();
var nameInput = document.getElementById('claim-name-input'); const nameInput = document.getElementById('claim-name-input');
const fileSelectionError = document.getElementById('input-error-file-selection');
// validate the file's name, type, and size // validate the file's name, type, and size
try { try {
console.log('validating file');
validateFile(selectedFile); validateFile(selectedFile);
} catch (error) { } catch (error) {
showError('input-error-file-selection', error.message); console.log('file validation failed with error:', error);
showError(fileSelectionError, error.message);
return; return;
} }
// set the image preview, if an image was provided // set the image preview, if an image was provided
console.log('file type:', selectedFile.type)
if (selectedFile.type !== 'video/mp4') { if (selectedFile.type !== 'video/mp4') {
if (selectedFile.type === 'image/gif') {
previewHolder.innerHTML = `<h2>loading preview...</h2>`
}
previewReader.readAsDataURL(selectedFile); previewReader.readAsDataURL(selectedFile);
previewReader.onloadend = function () { previewReader.onloadend = function () {
previewHolder.innerHTML = '<img width="100%" src="' + previewReader.result + '" alt="image preview"/>'; previewHolder.innerHTML = '<img width="100%" src="' + previewReader.result + '" alt="image preview"/>';
}; };
} else {
previewHolder.innerHTML = `<img width="100%" src="/assets/img/black_video_play.jpg"/>`
} }
// hide the drop zone // hide the drop zone
dropzoneWrapper.hidden = true; dropzoneWrapper.hidden = true;
@ -36,9 +49,13 @@ function previewAndStageFile(selectedFile){
} }
// Validate the publish submission and then trigger publishing. // Validate the publish submission and then trigger publishing.
function publishSelectedImage(event) { function publishStagedFile(event) {
var claimName = document.getElementById('claim-name-input').value; const claimName = document.getElementById('claim-name-input').value;
var channelName = document.getElementById('channel-name-select').value; const channelName = document.getElementById('channel-name-select').value;
const fileSelectionError = document.getElementById('input-error-file-selection');
const claimNameError = document.getElementById('input-error-claim-name');
const channelSelectError = document.getElementById('input-error-channel-select');
const publishSubmitError = document.getElementById('input-error-publish-submit');
// prevent default so this script can handle submission // prevent default so this script can handle submission
event.preventDefault(); event.preventDefault();
// validate, submit, and handle response // validate, submit, and handle response
@ -48,14 +65,14 @@ function publishSelectedImage(event) {
}) })
.catch(error => { .catch(error => {
if (error.name === 'FileError') { if (error.name === 'FileError') {
showError(document.getElementById('input-error-file-selection'), error.message); showError(fileSelectionError, error.message);
} else if (error.name === 'NameError') { } else if (error.name === 'NameError') {
showError(document.getElementById('input-error-claim-name'), error.message); showError(claimNameError, error.message);
} else if (error.name === 'ChannelNameError'){ } else if (error.name === 'ChannelNameError'){
console.log(error); console.log(error);
showError(document.getElementById('input-error-channel-select'), error.message); showError(channelSelectError, error.message);
} else { } else {
showError(document.getElementById('input-error-publish-submit'), error.message); showError(publishSubmitError, error.message);
} }
return; return;
}) })

View file

@ -3,9 +3,11 @@
// validation function which checks the proposed file's type, size, and name // validation function which checks the proposed file's type, size, and name
function validateFile(file) { function validateFile(file) {
if (!file) { if (!file) {
console.log('no file found');
throw new Error('no file provided'); throw new Error('no file provided');
} }
if (/'/.test(file.name)) { if (/'/.test(file.name)) {
console.log('file name had apostrophe in it');
throw new Error('apostrophes are not allowed in the file name'); throw new Error('apostrophes are not allowed in the file name');
} }
// validate size and type // validate size and type
@ -13,17 +15,25 @@ function validateFile(file) {
case 'image/jpeg': case 'image/jpeg':
case 'image/jpg': case 'image/jpg':
case 'image/png': case 'image/png':
if (file.size > 10000000){
console.log('file was too big');
throw new Error('Sorry, images are limited to 10 megabytes.');
}
break;
case 'image/gif': case 'image/gif':
if (file.size > 50000000){ if (file.size > 50000000){
throw new Error('Sorry, images are limited to 50 megabytes.'); console.log('file was too big');
throw new Error('Sorry, .gifs are limited to 50 megabytes.');
} }
break; break;
case 'video/mp4': case 'video/mp4':
if (file.size > 50000000){ if (file.size > 50000000){
console.log('file was too big');
throw new Error('Sorry, videos are limited to 50 megabytes.'); throw new Error('Sorry, videos are limited to 50 megabytes.');
} }
break; break;
default: default:
console.log('file type is not supported');
throw new Error(file.type + ' is not a supported file type. Only, .jpeg, .png, .gif, and .mp4 files are currently supported.') throw new Error(file.type + ' is not a supported file type. Only, .jpeg, .png, .gif, and .mp4 files are currently supported.')
} }
} }
@ -133,8 +143,9 @@ function checkChannelName(name){
// validation function which checks all aspects of the publish submission // validation function which checks all aspects of the publish submission
function validateFilePublishSubmission(stagedFiles, claimName, channelName){ function validateFilePublishSubmission(stagedFiles, claimName, channelName){
console.log(`validating name: ${claimName} channel: ${channelName} file:`, stagedFiles);
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
// 1. make sure only 1 file was selected // 1. make sure 1 file was staged
if (!stagedFiles) { if (!stagedFiles) {
return reject(new FileError("Please select a file")); return reject(new FileError("Please select a file"));
} else if (stagedFiles.length > 1) { } else if (stagedFiles.length > 1) {

View file

@ -1,7 +1,7 @@
const logger = require('winston'); const logger = require('winston');
const publishController = require('../controllers/publishController.js'); const publishController = require('../controllers/publishController.js');
const publishHelpers = require('../helpers/publishHelpers.js'); const publishHelpers = require('../helpers/publishHelpers.js');
const errorHandlers = require('../helpers/errorHandlers.js'); const { useObjectPropertiesIfNoKeys } = require('../helpers/errorHandlers.js');
const { postToStats } = require('../controllers/statsController.js'); const { postToStats } = require('../controllers/statsController.js');
module.exports = (app, siofu, hostedContentPath) => { module.exports = (app, siofu, hostedContentPath) => {
@ -47,17 +47,17 @@ module.exports = (app, siofu, hostedContentPath) => {
// publish the file // publish the file
publishController.publish(publishParams, file.name, file.meta.type) publishController.publish(publishParams, file.name, file.meta.type)
.then(result => { .then(result => {
postToStats('PUBLISH', '/', null, null, null, 'success');
socket.emit('publish-complete', { name: publishParams.name, result }); socket.emit('publish-complete', { name: publishParams.name, result });
postToStats('PUBLISH', '/', null, null, null, 'success');
}) })
.catch(error => { .catch(error => {
error = errorHandlers.handlePublishError(error);
postToStats('PUBLISH', '/', null, null, null, error);
socket.emit('publish-failure', error); socket.emit('publish-failure', error);
logger.error('Publish Error:', useObjectPropertiesIfNoKeys(error));
postToStats('PUBLISH', '/', null, null, null, error);
}); });
} else { } else {
logger.error(`An error occurred in uploading the client's file`);
socket.emit('publish-failure', 'File uploaded, but with errors'); socket.emit('publish-failure', 'File uploaded, but with errors');
logger.error(`An error occurred in uploading the client's file`);
postToStats('PUBLISH', '/', null, null, null, 'File uploaded, but with errors'); postToStats('PUBLISH', '/', null, null, null, 'File uploaded, but with errors');
// to-do: remove the file, if not done automatically // to-do: remove the file, if not done automatically
} }

View file

@ -5,14 +5,16 @@
<div id="publish-dropzone-wrapper"> <div id="publish-dropzone-wrapper">
<div id="publish-dropzone" class="align-content-center" ondrop="drop_handler(event);" ondragover="dragover_handler(event);" ondragend="dragend_handler(event)"> <div id="publish-dropzone" class="align-content-center" ondrop="drop_handler(event);" ondragover="dragover_handler(event);" ondragend="dragend_handler(event)">
<div id="publish-dropzone-instructions"> <div id="publish-dropzone-instructions">
<div class="row">
<p>Drag & drop image or video here</p> <p>Drag & drop image or video here</p>
<p>OR</p> <p>OR</p>
<form> <form>
<div class="info-message info-message--failure" id="input-error-file-selection" hidden="true"></div>
<label class="input-file-label" for="siofu_input">CHOOSE FILE</label> <label class="input-file-label" for="siofu_input">CHOOSE FILE</label>
<input class=" input-file" type="file" id="siofu_input" name="siofu_input" accept="video/*,image/*" onchange="previewAndStageFile(event.target.files[0])" enctype="multipart/form-data"/> <input class=" input-file" type="file" id="siofu_input" name="siofu_input" accept="video/*,image/*" onchange="previewAndStageFile(event.target.files[0])" enctype="multipart/form-data"/>
</form> </form>
</div> </div>
<div class="info-message info-message--failure" id="input-error-file-selection" hidden="true"></div>
</div>
</div> </div>
</div> </div>
<div id="publish-form-wrapper" hidden="true"> <div id="publish-form-wrapper" hidden="true">
@ -48,6 +50,7 @@
var socket = io(); var socket = io();
var uploader = new SocketIOFileUpload(socket); var uploader = new SocketIOFileUpload(socket);
var stagedFiles = null; var stagedFiles = null;
/* drop zone functions */ /* drop zone functions */
function drop_handler(ev) { function drop_handler(ev) {
ev.preventDefault(); ev.preventDefault();
@ -73,6 +76,7 @@
ev.dataTransfer.clearData(); ev.dataTransfer.clearData();
} }
} }
/* socketio-file-upload listeners */ /* socketio-file-upload listeners */
function updatePublishStatus(msg){ function updatePublishStatus(msg){
document.getElementById('publish-status').innerHTML = msg; document.getElementById('publish-status').innerHTML = msg;

View file

@ -1,6 +1,6 @@
<div id="details-detail" hidden="true"> <div id="details-detail" hidden="true">
<div class="row row--short"> <div class="row">
<div class="column column--4"> <div class="column column--4">
<label for="publish-license" class="label">License:* </label> <label for="publish-license" class="label">License:* </label>
</div><div class="column column--6"> </div><div class="column column--6">
@ -11,7 +11,7 @@
</div> </div>
</div> </div>
<div class="row row--short"> <div class="row">
<div class="column column--4"> <div class="column column--4">
<label for="publish-nsfw" class="label">NSFW*</label> <label for="publish-nsfw" class="label">NSFW*</label>
</div><div class="column column--6"> </div><div class="column column--6">

View file

@ -1,74 +1,11 @@
<div class="row"> <div class="row">
<div class="input-error" id="input-error-publish-submit" hidden="true"></div> <div class="input-error" id="input-error-publish-submit" hidden="true"></div>
<button id="publish-submit" class="button--primary button--large" onclick="publishSelectedImage(event)">Upload</button> <button id="publish-submit" class="button--primary button--large" onclick="publishStagedFile(event)">Upload</button>
</div> </div>
<div class="row row--short align-content-center"> <div class="row row--short align-content-center">
<button class="button--cancel" onclick="resetPublishArea()">Cancel</button> <button class="button--cancel" onclick="cancelPublish()">Cancel</button>
</div> </div>
<script type="text/javascript" > <script type="text/javascript" >
function resetPublishArea () {
window.location.href = '/';
}
/* publish functions */
// When a file is selected for publish, validate that file and
// stage it so it will be ready when the publish button is clicked.
function previewAndStageFile(selectedFile){
const publishForm = document.getElementById('publish-form-wrapper');
var previewHolder = document.getElementById('asset-preview-holder');
var dropzone = document.getElementById('publish-drop-zone-wrapper');
var previewReader = new FileReader();
var nameInput = document.getElementById('claim-name-input');
// validate the file's name, type, and size
try {
validateFile(selectedFile);
} catch (error) {
showError('input-error-file-selection', error.message);
return;
}
// set the image preview, if an image was provided
if (selectedFile.type !== 'video/mp4') {
previewReader.readAsDataURL(selectedFile);
previewReader.onloadend = function () {
previewHolder.innerHTML = '<img width="100%" src="' + previewReader.result + '" alt="image preview"/>';
};
}
// hide the drop zone
dropzone.hidden = true;
publishForm.hidden = false;
// set the name input value to the image name if none is set yet
if (nameInput.value === "") {
var filename = selectedFile.name.substring(0, selectedFile.name.indexOf('.'))
nameInput.value = cleanseClaimName(filename);
checkClaimName(nameInput.value);
}
// store the selected file for upload
stagedFiles = [selectedFile];
}
// Validate the publish submission and then trigger publishing.
function publishSelectedImage(event) {
var claimName = document.getElementById('claim-name-input').value;
var channelName = document.getElementById('channel-name-select').value;
// prevent default so this script can handle submission
event.preventDefault();
// validate, submit, and handle response
validateFilePublishSubmission(stagedFiles, claimName, channelName)
.then(() => {
uploader.submitFiles(stagedFiles);
})
.catch(error => {
if (error.name === 'FileError') {
showError(document.getElementById('input-error-file-selection'), error.message);
} else if (error.name === 'NameError') {
showError(document.getElementById('input-error-claim-name'), error.message);
} else if (error.name === 'ChannelNameError'){
console.log(error);
showError(document.getElementById('input-error-channel-select'), error.message);
} else {
showError(document.getElementById('input-error-publish-submit'), error.message);
}
return;
})
};
</script> </script>