178 lines
No EOL
4.9 KiB
JavaScript
178 lines
No EOL
4.9 KiB
JavaScript
// update the publish status
|
|
function updatePublishStatus(msg){
|
|
document.getElementById('publish-status').innerHTML = msg;
|
|
}
|
|
|
|
function validateFile(file) {
|
|
if (!file) {
|
|
throw new Error('no file provided');
|
|
}
|
|
// validate size and type
|
|
switch (file.type) {
|
|
case 'image/jpeg':
|
|
case 'image/png':
|
|
case 'image/gif':
|
|
if (file.size > 5000000){
|
|
throw new Error('Sorry, images are limitted to 5 megabytes.');
|
|
}
|
|
break;
|
|
case 'video/mp4':
|
|
if (file.size > 50000000){
|
|
throw new Error('Sorry, videos are limitted to 50 megabytes.');
|
|
}
|
|
break;
|
|
default:
|
|
throw new Error('The ' + file.Type + ' content type is not supported. Only, .jpeg, .png, .gif, and .mp4 files are currently supported.')
|
|
}
|
|
}
|
|
|
|
function validateSubmission(stagedFiles, name){
|
|
// make sure only 1 file was selected
|
|
if (!stagedFiles) {
|
|
throw new FileError("Please select a file");
|
|
} else if (stagedFiles.length > 1) {
|
|
throw new FileError("Only one file is allowed at a time");
|
|
}
|
|
// validate 'name' field
|
|
var invalidCharacters = /[^A-Za-z0-9,-]/.exec(name);
|
|
if (invalidCharacters) {
|
|
throw new NameError(invalidCharacters + ' is not allowed. A-Z, a-z, 0-9, and "-" only.');
|
|
} else if (name.length < 1) {
|
|
throw new NameError("You must enter a name for your claim");
|
|
}
|
|
}
|
|
|
|
function validateClaimName (name) {
|
|
var deferred = new Promise(function(resolve, reject) {
|
|
var xhttp;
|
|
xhttp = new XMLHttpRequest();
|
|
xhttp.open('GET', '/api/isClaimAvailable/' + name, true);
|
|
xhttp.responseType = 'json';
|
|
xhttp.onreadystatechange = function() {
|
|
if (this.readyState == 4 ) {
|
|
if ( this.status == 200) {
|
|
if (this.response == true) {
|
|
resolve();
|
|
} else {
|
|
reject("That name has already been claimed by spee.ch. Please choose a different name.");
|
|
}
|
|
} else {
|
|
reject("request to check claim name failed with status:" + this.status);
|
|
};
|
|
}
|
|
};
|
|
xhttp.send();
|
|
});
|
|
return deferred;
|
|
}
|
|
|
|
/* regular publish helper functions */
|
|
|
|
function previewAndStageFile(selectedFile){
|
|
var previewHolder = document.getElementById('asset-preview-holder');
|
|
var dropzone = document.getElementById('drop-zone');
|
|
var previewReader = new FileReader();
|
|
var nameInput = document.getElementById('publish-name');
|
|
// validate the file
|
|
try {
|
|
validateFile(selectedFile);
|
|
} catch (error) {
|
|
showError('input-error-file-selection', error.message);
|
|
return;
|
|
}
|
|
// set the preview
|
|
if (selectedFile.type === 'video/mp4') {
|
|
|
|
} else {
|
|
previewReader.readAsDataURL(selectedFile);
|
|
previewReader.onloadend = function () {
|
|
dropzone.style.display = 'none';
|
|
previewHolder.style.display = 'block';
|
|
previewHolder.innerHTML = '<img width="100%" src="' + previewReader.result + '" alt="image preview"/>';
|
|
|
|
};
|
|
}
|
|
// set the name input value to the image name if none is set yet
|
|
if (nameInput.value === "") {
|
|
nameInput.value = selectedFile.name.substring(0, selectedFile.name.indexOf('.'));
|
|
}
|
|
// store the selected file for upload
|
|
stagedFiles = [selectedFile];
|
|
}
|
|
|
|
/* drop zone functions */
|
|
|
|
function drop_handler(ev) {
|
|
ev.preventDefault();
|
|
// if dropped items aren't files, reject them
|
|
var dt = ev.dataTransfer;
|
|
if (dt.items) {
|
|
if (dt.items[0].kind == 'file') {
|
|
var droppedFile = dt.items[0].getAsFile();
|
|
previewAndStageFile(droppedFile);
|
|
}
|
|
}
|
|
}
|
|
|
|
function dragover_handler(ev) {
|
|
ev.preventDefault();
|
|
}
|
|
|
|
function dragend_handler(ev) {
|
|
var dt = ev.dataTransfer;
|
|
if (dt.items) {
|
|
for (var i = 0; i < dt.items.length; i++) {
|
|
dt.items.remove(i);
|
|
}
|
|
} else {
|
|
ev.dataTransfer.clearData();
|
|
}
|
|
}
|
|
|
|
/* meme publish functions */
|
|
|
|
function startPublish() {
|
|
//download the image
|
|
var dataUrl = canvas.toDataURL('image/jpeg'); // canvas defined in memeDraw.js
|
|
var blob = dataURItoBlob(dataUrl)
|
|
var fileName = nameInput.value + ".jpeg"; //note: need to dynamically grab type
|
|
var file = new File([blob], fileName, {type: 'image/jpeg', lastModified: Date.now()});
|
|
stageAndPublish(file);
|
|
};
|
|
|
|
function stageAndPublish(file) {
|
|
var name = nameInput.value;
|
|
var invalidCharacters = /[^A-Za-z0-9,-]/.exec(name);
|
|
// validate 'name'
|
|
if (invalidCharacters) {
|
|
showError('input-error-claim-name', invalidCharacters + ' is not allowed. A-Z, a-z, 0-9, "_" and "-" only.');
|
|
return;
|
|
} else if (name.length < 1) {
|
|
showError('input-error-claim-name', 'You must enter a name for your claim');
|
|
return;
|
|
}
|
|
// stage files
|
|
stagedFiles = [file]; // stores the selected file for
|
|
// make sure a file was selected
|
|
if (stagedFiles) {
|
|
// make sure only 1 file was selected
|
|
if (stagedFiles.length < 1) {
|
|
showError('input-error-file-selection', 'A file is needed');
|
|
return;
|
|
}
|
|
// make sure the content type is acceptable
|
|
switch (stagedFiles[0].type) {
|
|
case "image/png":
|
|
case "image/jpeg":
|
|
case "image/gif":
|
|
case "video/mp4":
|
|
uploader.submitFiles(stagedFiles);
|
|
break;
|
|
default:
|
|
showError('input-error-publish-submit', 'Only .png, .jpeg, .gif, and .mp4 files are currently supported');
|
|
break;
|
|
}
|
|
} else {
|
|
showError('input-error-file-selection', 'Please select a file');
|
|
}
|
|
} |