From b13dc53f0122d99c0d84d6f63ed8da33dc3be95c Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 20 Jun 2017 16:36:19 -0700 Subject: [PATCH] meme publish works --- config/development.json | 2 +- controllers/publishController.js | 7 +- helpers/logging/loggerSetup.js | 10 ++- public/assets/js/memeDraw.js | 50 +++++++---- public/assets/js/memePublish.js | 83 +++++-------------- routes/home-routes.js | 2 +- views/index.handlebars | 1 + views/memeFodder.handlebars | 4 +- views/partials/memeMaker.handlebars | 10 +-- ...ults.handlebars => memeResults.handlebars} | 0 10 files changed, 76 insertions(+), 93 deletions(-) rename views/partials/{memeFodderResults.handlebars => memeResults.handlebars} (100%) diff --git a/config/development.json b/config/development.json index 2c64d379..2712bde3 100644 --- a/config/development.json +++ b/config/development.json @@ -10,7 +10,7 @@ "PublishUploadPath": "C:\\lbry\\speech\\hosted_content" }, "Logging": { - "LogLevel": "debug", + "LogLevel": "silly", "LogDirectory": "C:\\lbry\\speech\\logs\\" } } \ No newline at end of file diff --git a/controllers/publishController.js b/controllers/publishController.js index eb1d7660..fc632836 100644 --- a/controllers/publishController.js +++ b/controllers/publishController.js @@ -7,6 +7,9 @@ const errorHandlers = require('../helpers/libraries/errorHandlers.js'); function createPublishParams (claim, filePath, license, nsfw) { logger.debug(`Creating Publish Parameters for "${claim}"`); + if (typeof nsfw === 'string') { + nsfw = (nsfw.toLowerCase() === 'on'); + } const publishParams = { name : claim, file_path: filePath, @@ -17,11 +20,12 @@ function createPublishParams (claim, filePath, license, nsfw) { author : 'spee.ch', language : 'en', license, - nsfw : nsfw.toLowerCase() === 'on', + nsfw, }, claim_address : walledAddress, change_address: walledAddress, }; + logger.debug('publishParams:', publishParams); return publishParams; } @@ -49,6 +53,7 @@ module.exports = { socket.emit('publish-complete', { name: claim, result }); }) .catch(error => { + logger.error(`Error publishing ${fileName}`, error); visitor.event('Publish Route', 'Publish Failure', filePath).send(); socket.emit('publish-failure', errorHandlers.handlePublishError(error)); deleteTemporaryFile(filePath); diff --git a/helpers/logging/loggerSetup.js b/helpers/logging/loggerSetup.js index 61c5e192..7a0b2e41 100644 --- a/helpers/logging/loggerSetup.js +++ b/helpers/logging/loggerSetup.js @@ -8,10 +8,12 @@ module.exports = (winston, logLevel, logDir) => { winston.configure({ transports: [ new (winston.transports.Console)({ - level : logLevel, - timestamp : false, - colorize : true, - prettyPrint: true, + level : logLevel, + timestamp : false, + colorize : true, + prettyPrint : true, + handleExceptions : true, + humanReadableUnhandledException: true, }), new (winston.transports.File)({ filename : `${logDir}/speechLogs.log`, diff --git a/public/assets/js/memeDraw.js b/public/assets/js/memeDraw.js index 4e4ea0a2..76d6ba3e 100644 --- a/public/assets/js/memeDraw.js +++ b/public/assets/js/memeDraw.js @@ -6,8 +6,7 @@ var fontSize = 28; var topText = document.getElementById('top-text'); var bottomText = document.getElementById('bottom-text'); var ctx = canvas.getContext('2d'); -var downloadLink = document.getElementById("meme-download-link"); -var memeFileName = document.getElementById("meme-file-name"); +var fileNameInput = document.getElementById("file-name-input"); // create the canvas img.onload = function() { @@ -60,12 +59,13 @@ function wrapText(context, text, x, y, maxWidth, lineHeight, fromBottom) { lineHeight = (fromBottom)?-lineHeight:lineHeight; var lines = []; - var y = y; - var line =''; + var y = y; + var line =''; var words = text.split(' '); + for (var i = 0; i < words.length; i++) { - var testLine = line + ' ' + words[i]; - var metrics = context.measureText(testLine); + var testLine = line + ' ' + words[i]; + var metrics = context.measureText(testLine); var testWidth = metrics.width; if (testWidth > maxWidth) { @@ -75,6 +75,7 @@ function wrapText(context, text, x, y, maxWidth, lineHeight, fromBottom) { line = testLine; } } + lines[pushMethod](line); for (var k in lines ) { @@ -83,16 +84,33 @@ function wrapText(context, text, x, y, maxWidth, lineHeight, fromBottom) { } } -// save the meme -function downloadMeme() { - var dt = canvas.toDataURL('image/jpeg'); - this.href = dt; -}; +function dataURItoBlob(dataURI) { + // convert base64/URLEncoded data component to raw binary data held in a string + var byteString; + if (dataURI.split(',')[0].indexOf('base64') >= 0) + byteString = atob(dataURI.split(',')[1]); + else + byteString = unescape(dataURI.split(',')[1]); -function updateFileName(){ - downloadLink.download = this.value; + // separate out the mime component + var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]; + + // write the bytes of the string to a typed array + var ia = new Uint8Array(byteString.length); + for (var i = 0; i < byteString.length; i++) { + ia[i] = byteString.charCodeAt(i); + } + + return new Blob([ia], {type:mimeString}); } -memeFileName.addEventListener('keyup', updateFileName); - -downloadLink.addEventListener('click', downloadMeme, false); \ No newline at end of file +// save the meme +function startPublish() { + //download the image + var dataUrl = canvas.toDataURL('image/jpeg'); + var blob = dataURItoBlob(dataUrl) + var filename = fileNameInput.value; + var file = new File([blob], filename, {type: 'image/jpeg', lastModified: Date.now()}); + console.log(file); + publishMeme(file); // note: this function is in memePublish.js +}; \ No newline at end of file diff --git a/public/assets/js/memePublish.js b/public/assets/js/memePublish.js index 99db22d9..57192811 100644 --- a/public/assets/js/memePublish.js +++ b/public/assets/js/memePublish.js @@ -3,6 +3,9 @@ var socket = io(); var uploader = new SocketIOFileUpload(socket); var stagedFiles = null; +var name = 'meme-fodder-entry'; +var license = 'Creative Commons'; +var nsfw = false; /* helper functions */ // create a progress animation @@ -23,67 +26,16 @@ function createProgressBar(element, size){ }; setInterval(addOne, 300); } -// preview file and stage the image for upload -function previewAndStageFile(selectedFile){ - var preview = document.getElementById('image-preview'); - var dropzone = document.getElementById('drop-zone'); - var previewReader = new FileReader(); - preview.style.display = 'block'; - dropzone.style.display = 'none'; - - previewReader.onloadend = function () { - preview.src = previewReader.result; - }; - - if (selectedFile) { - previewReader.readAsDataURL(selectedFile); // reads the data and sets the img src - document.getElementById('publish-name').value = selectedFile.name.substring(0, selectedFile.name.indexOf('.')); // updates metadata inputs - stagedFiles = [selectedFile]; // stores the selected file for upload - } else { - preview.src = ''; - } -} -// update the publish status -function updatePublishStatus(msg){ - document.getElementById('publish-status').innerHTML = msg; -} -// process the drop-zone drop -function drop_handler(ev) { - console.log("drop"); - 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); - } else { - console.log("no files were found") - } - } else { - console.log("no items were found") - } -} -// prevent the browser's default drag behavior -function dragover_handler(ev) { - ev.preventDefault(); -} -// remove all of the drag data -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(); - } -} - -/* configure the submit button */ -document.getElementById('publish-submit').addEventListener('click', function(event){ - event.preventDefault(); +function publishMeme(file) { + // get image data + //var imgData = ctx.getImageData(0, 0, canvasWidth, canvasHeight); + //console.log(imgData); + // stage files + stagedFiles = [file]; // stores the selected file for upload + //stagedFiles = [selectedFile.getAsFile()]; // stores the selected file for upload + console.log(stagedFiles[0]); + console.log('file staged'); // make sure a file was selected if (stagedFiles) { // make sure only 1 file was selected @@ -104,13 +56,16 @@ document.getElementById('publish-submit').addEventListener('click', function(eve break; } } -}) + +} + +// update the publish status +function updatePublishStatus(msg){ + document.getElementById('publish-status').innerHTML = msg; +} /* socketio-file-upload listeners */ uploader.addEventListener('start', function(event){ - var name = document.getElementById('publish-name').value; - var license = document.getElementById('publish-license').value; - var nsfw = document.getElementById('publish-nsfw').value; event.file.meta.name = name; event.file.meta.license = license; event.file.meta.nsfw = nsfw; diff --git a/routes/home-routes.js b/routes/home-routes.js index e389b4ae..09c40644 100644 --- a/routes/home-routes.js +++ b/routes/home-routes.js @@ -7,7 +7,7 @@ module.exports = app => { }); // a catch-all route if someone visits a page that does not exist app.use('*', (req, res) => { - logger.error(`Get request on ${req.originalUrl} which was 404`); + logger.error(`Get request on ${req.originalUrl} which was a 404`); res.status(404).render('fourOhFour'); }); }; diff --git a/views/index.handlebars b/views/index.handlebars index ea81218f..120edfff 100644 --- a/views/index.handlebars +++ b/views/index.handlebars @@ -55,6 +55,7 @@ }; if (selectedFile) { + console.log(selectedFile); previewReader.readAsDataURL(selectedFile); // reads the data and sets the img src document.getElementById('publish-name').value = selectedFile.name.substring(0, selectedFile.name.indexOf('.')); // updates metadata inputs stagedFiles = [selectedFile]; // stores the selected file for upload diff --git a/views/memeFodder.handlebars b/views/memeFodder.handlebars index 9f42ddc3..7b9bb451 100644 --- a/views/memeFodder.handlebars +++ b/views/memeFodder.handlebars @@ -5,7 +5,7 @@ {{> memeMaker}}
- {{> memeFodderResults}} + {{> memeResults}}
@@ -13,3 +13,5 @@ + + diff --git a/views/partials/memeMaker.handlebars b/views/partials/memeMaker.handlebars index bf3740a2..520f3283 100644 --- a/views/partials/memeMaker.handlebars +++ b/views/partials/memeMaker.handlebars @@ -16,27 +16,27 @@ a picture to make your meme with -
+
- +
- +
- +
- +
diff --git a/views/partials/memeFodderResults.handlebars b/views/partials/memeResults.handlebars similarity index 100% rename from views/partials/memeFodderResults.handlebars rename to views/partials/memeResults.handlebars