From 99a07d342cf9302a7412fc701373072f763606e1 Mon Sep 17 00:00:00 2001 From: NGPixel Date: Sun, 9 Oct 2016 01:26:25 -0400 Subject: [PATCH] Uploads model + watcher --- agent.js | 84 +++---------- assets/js/app.js | 2 +- client/js/components/editor-image.js | 67 ++++++++-- controllers/pages.js | 10 +- controllers/uploads.js | 15 ++- models/entries.js | 2 +- models/git.js | 17 +++ models/localdata.js | 64 +++++----- models/uploads.js | 176 +++++++++++++++++++++++++++ package.json | 1 + views/error-notexist.pug | 32 +++++ ws-server.js | 16 +++ 12 files changed, 367 insertions(+), 119 deletions(-) create mode 100644 models/uploads.js create mode 100644 views/error-notexist.pug diff --git a/agent.js b/agent.js index d3b5a3ab..0740f169 100644 --- a/agent.js +++ b/agent.js @@ -31,7 +31,8 @@ global.WSInternalKey = process.argv[2]; winston.info('[AGENT] Background Agent is initializing...'); var appconfig = require('./models/config')('./config.yml'); -let lcdata = require('./models/localdata').init(appconfig, 'agent'); +global.lcdata = require('./models/localdata').init(appconfig, 'agent'); +var upl = require('./models/uploads').init(appconfig); global.git = require('./models/git').init(appconfig); global.entries = require('./models/entries').init(appconfig); @@ -43,9 +44,6 @@ var Promise = require('bluebird'); var fs = Promise.promisifyAll(require("fs-extra")); var path = require('path'); var cron = require('cron').CronJob; -var readChunk = require('read-chunk'); -var fileType = require('file-type'); -var farmhash = require('farmhash'); global.ws = require('socket.io-client')('http://localhost:' + appconfig.wsPort, { reconnectionAttempts: 10 }); @@ -56,6 +54,8 @@ const mimeImgTypes = ['image/png', 'image/jpg'] // ---------------------------------------- var jobIsBusy = false; +var jobUplWatchStarted = false; + var job = new cron({ cronTime: '0 */5 * * * *', onTick: () => { @@ -168,71 +168,11 @@ var job = new cron({ let fldPath = path.join(uploadsPath, fldName); return fs.readdirAsync(fldPath).then((fList) => { return Promise.map(fList, (f) => { - let fPath = path.join(fldPath, f); - let fPathObj = path.parse(fPath); - let fUid = farmhash.fingerprint32(fldName + '/' + f); - - return fs.statAsync(fPath) - .then((s) => { - - if(!s.isFile()) { return false; } - - // Get MIME info - - let mimeInfo = fileType(readChunk.sync(fPath, 0, 262)); - - // Images - - if(s.size < 3145728) { // ignore files larger than 3MB - if(_.includes(['image/png', 'image/jpeg', 'image/gif', 'image/webp'], mimeInfo.mime)) { - return lcdata.getImageMetadata(fPath).then((mData) => { - - let cacheThumbnailPath = path.parse(path.join(dataPath, 'thumbs', fUid + '.png')); - let cacheThumbnailPathStr = path.format(cacheThumbnailPath); - - mData = _.pick(mData, ['format', 'width', 'height', 'density', 'hasAlpha', 'orientation']); - mData.uid = fUid; - mData.category = 'image'; - mData.mime = mimeInfo.mime; - mData.folder = fldName; - mData.filename = f; - mData.basename = fPathObj.name; - mData.filesize = s.size; - mData.uploadedOn = moment().utc(); - allFiles.push(mData); - - // Generate thumbnail - - return fs.statAsync(cacheThumbnailPathStr).then((st) => { - return st.isFile(); - }).catch((err) => { - return false; - }).then((thumbExists) => { - - return (thumbExists) ? true : fs.ensureDirAsync(cacheThumbnailPath.dir).then(() => { - return lcdata.generateThumbnail(fPath, cacheThumbnailPathStr); - }); - - }); - - }) - } - } - - // Other Files - - allFiles.push({ - uid: fUid, - category: 'file', - mime: mimeInfo.mime, - folder: fldName, - filename: f, - basename: fPathObj.name, - filesize: s.size, - uploadedOn: moment().utc() - }); - - }); + return upl.processFile(fldName, f).then((mData) => { + if(mData) { + allFiles.push(mData); + } + }); }, {concurrency: 3}); }); }, {concurrency: 1}).finally(() => { @@ -255,6 +195,12 @@ var job = new cron({ Promise.all(jobs).then(() => { winston.info('[AGENT] All jobs completed successfully! Going to sleep for now.'); + + if(!jobUplWatchStarted) { + jobUplWatchStarted = true; + upl.watch(); + } + }).catch((err) => { winston.error('[AGENT] One or more jobs have failed: ', err); }).finally(() => { diff --git a/assets/js/app.js b/assets/js/app.js index 51e7cf82..1aab045b 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -1 +1 @@ -"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function setInputSelection(e,t,o){if(e.focus(),"undefined"!=typeof e.selectionStart)e.selectionStart=t,e.selectionEnd=o;else if(document.selection&&document.selection.createRange){e.select();var a=document.selection.createRange();a.collapse(!0),a.moveEnd("character",o),a.moveStart("character",t),a.select()}}function makeSafePath(e){var t=_.split(_.trim(e),"/");return t=_.map(t,function(e){return _.kebabCase(_.deburr(_.trim(e)))}),_.join(_.filter(t,function(e){return!_.isEmpty(e)}),"/")}var _createClass=function(){function e(e,t){for(var o=0;o=3?(t.searchactive=!0,t.searchload++,o.emit("search",{terms:e},function(e){t.searchres=e.match,t.searchsuggest=e.suggest,t.searchmovearr=_.concat([],t.searchres,t.searchsuggest),t.searchload>0&&t.searchload--})):(t.searchactive=!1,t.searchres=[],t.searchsuggest=[],t.searchmovearr=[],t.searchload=0)},searchmoveidx:function(e,o){e>0?t.searchmovekey=t.searchmovearr[e-1].document?"res."+t.searchmovearr[e-1].document.entryPath:"sug."+t.searchmovearr[e-1]:t.searchmovekey=""}},methods:{useSuggestion:function(e){t.searchq=e},closeSearch:function(){t.searchq=""},moveSelectSearch:function(){if(!(t.searchmoveidx<1)){var e=t.searchmoveidx-1;t.searchmovearr[e].document?window.location.assign("/"+t.searchmovearr[e].document.entryPath):t.searchq=t.searchmovearr[e]}},moveDownSearch:function(){t.searchmoveidx0&&t.searchmoveidx--}}});e("main").on("click",t.closeSearch)}}),e("#page-type-view").length&&!function(){var o="home"!==e("#page-type-view").data("entrypath")?e("#page-type-view").data("entrypath"):"",a=o+"/new-page";e(".btn-create-prompt").on("click",function(t){e("#txt-create-prompt").val(a),e("#modal-create-prompt").toggleClass("is-active"),setInputSelection(e("#txt-create-prompt").get(0),o.length+1,a.length),e("#txt-create-prompt").removeClass("is-danger").next().addClass("is-hidden")}),e("#txt-create-prompt").on("keypress",function(t){13===t.which&&e(".btn-create-go").trigger("click")}),e(".btn-create-go").on("click",function(t){var o=makeSafePath(e("#txt-create-prompt").val());_.isEmpty(o)?e("#txt-create-prompt").addClass("is-danger").next().removeClass("is-hidden"):(e("#txt-create-prompt").parent().addClass("is-loading"),window.location.assign("/create/"+o))}),""!==o&&e(".btn-move-prompt").removeClass("is-hidden");var n=_.lastIndexOf(o,"/")+1;e(".btn-move-prompt").on("click",function(t){e("#txt-move-prompt").val(o),e("#modal-move-prompt").toggleClass("is-active"),setInputSelection(e("#txt-move-prompt").get(0),n,o.length),e("#txt-move-prompt").removeClass("is-danger").next().addClass("is-hidden")}),e("#txt-move-prompt").on("keypress",function(t){13===t.which&&e(".btn-move-go").trigger("click")}),e(".btn-move-go").on("click",function(a){var n=makeSafePath(e("#txt-move-prompt").val());_.isEmpty(n)||n===o||"home"===n?e("#txt-move-prompt").addClass("is-danger").next().removeClass("is-hidden"):(e("#txt-move-prompt").parent().addClass("is-loading"),e.ajax(window.location.href,{data:{move:n},dataType:"json",method:"PUT"}).then(function(e,o,a){e.ok?window.location.assign("/"+n):t.pushError("Something went wrong",e.error)},function(e,o,a){t.pushError("Something went wrong","Save operation failed.")}))})}(),e("#page-type-create").length){var a;!function(){var n=e("#page-type-create").data("entrypath");e(".btn-create-discard").on("click",function(t){e("#modal-create-discard").toggleClass("is-active")}),1===e("#mk-editor").length&&!function(){var n=!1;Vue.filter("filesize",function(e){return _.toUpper(filesize(e))});var i=new Vue({el:"#modal-editor-image",data:{isLoading:!1,isLoadingText:"",newFolderName:"",newFolderShow:!1,newFolderError:!1,fetchFromUrlURL:"",fetchFromUrlShow:!1,folders:[],currentFolder:"",currentImage:"",currentAlign:"left",images:[]},methods:{open:function(){n=!0,e("#modal-editor-image").slideDown(),i.refreshFolders()},cancel:function(t){n=!1,e("#modal-editor-image").slideUp()},insertImage:function(e){a.codemirror.doc.somethingSelected()&&a.codemirror.execCommand("singleSelection");var t=_.find(i.images,["uid",i.currentImage]);t.normalizedPath=""===t.folder?t.filename:t.folder+"/"+t.filename,t.titleGuess=_.startCase(t.basename);var o="!["+t.titleGuess+"](/uploads/"+t.normalizedPath+' "'+t.titleGuess+'")';switch(i.currentAlign){case"center":o+="{.align-center}";break;case"right":o+="{.align-right}";break;case"logo":o+="{.pagelogo}"}a.codemirror.doc.replaceSelection(o),i.cancel()},newFolder:function(t){i.newFolderName="",i.newFolderError=!1,i.newFolderShow=!0,_.delay(function(){e("#txt-editor-newfoldername").focus()},400)},newFolderDiscard:function(e){i.newFolderShow=!1},newFolderCreate:function(e){var t=new RegExp("^[a-z0-9][a-z0-9-]*[a-z0-9]$");return i.newFolderName=_.kebabCase(_.trim(i.newFolderName)),_.isEmpty(i.newFolderName)||!t.test(i.newFolderName)?void(i.newFolderError=!0):(i.newFolderDiscard(),i.isLoading=!0,i.isLoadingText="Creating new folder...",void Vue.nextTick(function(){o.emit("uploadsCreateFolder",{foldername:i.newFolderName},function(e){i.folders=e,i.currentFolder=i.newFolderName,i.images=[],i.isLoading=!1})}))},fetchFromUrl:function(e){i.fetchFromUrlShow=!0},fetchFromUrlDiscard:function(e){i.fetchFromUrlShow=!1},selectFolder:function(e){i.currentFolder=e,i.loadImages()},refreshFolders:function(){i.isLoading=!0,i.isLoadingText="Fetching folders list...",i.currentFolder="",i.currentImage="",Vue.nextTick(function(){o.emit("uploadsGetFolders",{},function(e){i.folders=e,i.loadImages()})})},loadImages:function(){i.isLoading=!0,i.isLoadingText="Fetching images...",Vue.nextTick(function(){o.emit("uploadsGetImages",{folder:i.currentFolder},function(e){i.images=e,i.isLoading=!1,i.attachContextMenus()})})},selectImage:function(e){i.currentImage=e},selectAlignment:function(e){i.currentAlign=e},attachContextMenus:function(){var t=_.map(i.folders,function(e){return{name:""!==e?e:"/ (root)",icon:"fa-folder"}});e.contextMenu("destroy",".editor-modal-imagechoices > figure"),e.contextMenu({selector:".editor-modal-imagechoices > figure",appendTo:".editor-modal-imagechoices",position:function(t,o,a){e(t.$trigger).addClass("is-contextopen");var n=e(t.$trigger).position(),i={w:e(t.$trigger).width()/2,h:e(t.$trigger).height()/2};t.$menu.css({top:n.top+i.h,left:n.left+i.w})},events:{hide:function(t){e(t.$trigger).removeClass("is-contextopen")}},items:{rename:{name:"Rename",icon:"fa-edit",callback:function(e,t){alert("Clicked on "+e)}},move:{name:"Move to...",icon:"fa-folder-open-o",items:t},delete:{name:"Delete",icon:"fa-trash",callback:function(e,t){alert("Clicked on "+e)}}}})}}});e("#btn-editor-uploadimage input").on("change",function(o){e(o.currentTarget).simpleUpload("/uploads/img",{name:"imgfile",data:{folder:i.currentFolder},limit:20,expect:"json",allowedExts:["jpg","jpeg","gif","png","webp"],allowedTypes:["image/png","image/jpeg","image/gif","image/webp"],maxFileSize:3145728,init:function(){i.isLoading=!0,i.isLoadingText="Preparing to upload..."},progress:function(e){i.isLoadingText="Uploading..."+Math.round(e)+"%"},success:function(e){e.ok||t.pushError("Upload error",e.msg)},error:function(e){i.isLoading=!1,t.pushError(e.message,this.upload.file.name)},finish:function(){i.isLoading=!1}})});var r=ace.edit("codeblock-editor");r.setTheme("ace/theme/tomorrow_night"),r.getSession().setMode("ace/mode/markdown"),r.setOption("fontSize","14px"),r.setOption("hScrollBarAlwaysVisible",!1),r.setOption("wrap",!0);var c=ace.require("ace/ext/modelist"),l=new Vue({el:"#modal-editor-codeblock",data:{modes:c.modesByName,modeSelected:"text"},watch:{modeSelected:function(e,t){d(e).done(function(){ace.require("ace/mode/"+e),r.getSession().setMode("ace/mode/"+e)})}},methods:{cancel:function(t){n=!1,e("#modal-editor-codeblock").slideUp()},insertCode:function(e){a.codemirror.doc.somethingSelected()&&a.codemirror.execCommand("singleSelection");var t="\n```"+l.modeSelected+"\n"+r.getValue()+"\n```\n";a.codemirror.doc.replaceSelection(t),l.cancel()}}}),s=[],d=function(t){return e.ajax({url:"/js/ace/mode-"+t+".js",dataType:"script",cache:!0,beforeSend:function(){if(_.includes(s,t))return!1},success:function(){s.push(t)}})};a=new SimpleMDE({autofocus:!0,autoDownloadFontAwesome:!1,element:e("#mk-editor").get(0),placeholder:"Enter Markdown formatted content here...",spellChecker:!1,status:!1,toolbar:[{name:"bold",action:SimpleMDE.toggleBold,className:"fa fa-bold",title:"Bold"},{name:"italic",action:SimpleMDE.toggleItalic,className:"fa fa-italic",title:"Italic"},{name:"strikethrough",action:SimpleMDE.toggleStrikethrough,className:"fa fa-strikethrough",title:"Strikethrough"},"|",{name:"heading-1",action:SimpleMDE.toggleHeading1,className:"fa fa-header fa-header-x fa-header-1",title:"Big Heading"},{name:"heading-2",action:SimpleMDE.toggleHeading2,className:"fa fa-header fa-header-x fa-header-2",title:"Medium Heading"},{name:"heading-3",action:SimpleMDE.toggleHeading3,className:"fa fa-header fa-header-x fa-header-3",title:"Small Heading"},{name:"quote",action:SimpleMDE.toggleBlockquote,className:"fa fa-quote-left",title:"Quote"},"|",{name:"unordered-list",action:SimpleMDE.toggleUnorderedList,className:"fa fa-list-ul",title:"Bullet List"},{name:"ordered-list",action:SimpleMDE.toggleOrderedList,className:"fa fa-list-ol",title:"Numbered List"},"|",{name:"link",action:function(t){n||(n=!0,e("#modal-editor-link").slideToggle())},className:"fa fa-link",title:"Insert Link"},{name:"image",action:function(e){n||i.open()},className:"fa fa-image",title:"Insert Image"},{name:"file",action:function(e){},className:"fa fa-file-text-o",title:"Insert File"},"|",{name:"inline-code",action:function(e){if(!e.codemirror.doc.somethingSelected())return t.pushError("Invalid selection","You must select at least 1 character first.");var o=e.codemirror.doc.getSelections();o=_.map(o,function(e){return"`"+e+"`"}),e.codemirror.doc.replaceSelections(o)},className:"fa fa-terminal",title:"Inline Code"},{name:"code-block",action:function(t){n||(n=!0,a.codemirror.doc.somethingSelected()?r.setValue(a.codemirror.doc.getSelection()):r.setValue(""),e("#modal-editor-codeblock").slideDown(400,function(){r.resize(),r.focus()}))},className:"fa fa-code",title:"Code Block"},"|",{name:"table",action:function(e){},className:"fa fa-table",title:"Insert Table"},{name:"horizontal-rule",action:SimpleMDE.drawHorizontalRule,className:"fa fa-minus",title:"Horizontal Rule"}],shortcuts:{toggleBlockquote:null,toggleFullScreen:null}})}(),e(".btn-edit-save, .btn-create-save").on("click",function(o){e.ajax(window.location.href,{data:{markdown:a.value()},dataType:"json",method:"PUT"}).then(function(e,o,a){e.ok?window.location.assign("/"+n):t.pushError("Something went wrong",e.error)},function(e,o,a){t.pushError("Something went wrong","Save operation failed.")})})}()}if(e("#page-type-edit").length){var a;!function(){var n=e("#page-type-edit").data("entrypath");e(".btn-edit-discard").on("click",function(t){e("#modal-edit-discard").toggleClass("is-active")}),1===e("#mk-editor").length&&!function(){var n=!1;Vue.filter("filesize",function(e){return _.toUpper(filesize(e))});var i=new Vue({el:"#modal-editor-image",data:{isLoading:!1,isLoadingText:"",newFolderName:"",newFolderShow:!1,newFolderError:!1,fetchFromUrlURL:"",fetchFromUrlShow:!1,folders:[],currentFolder:"",currentImage:"",currentAlign:"left",images:[]},methods:{open:function(){n=!0,e("#modal-editor-image").slideDown(),i.refreshFolders()},cancel:function(t){n=!1,e("#modal-editor-image").slideUp()},insertImage:function(e){a.codemirror.doc.somethingSelected()&&a.codemirror.execCommand("singleSelection");var t=_.find(i.images,["uid",i.currentImage]);t.normalizedPath=""===t.folder?t.filename:t.folder+"/"+t.filename,t.titleGuess=_.startCase(t.basename);var o="!["+t.titleGuess+"](/uploads/"+t.normalizedPath+' "'+t.titleGuess+'")';switch(i.currentAlign){case"center":o+="{.align-center}";break;case"right":o+="{.align-right}";break;case"logo":o+="{.pagelogo}"}a.codemirror.doc.replaceSelection(o),i.cancel()},newFolder:function(t){i.newFolderName="",i.newFolderError=!1,i.newFolderShow=!0,_.delay(function(){e("#txt-editor-newfoldername").focus()},400)},newFolderDiscard:function(e){i.newFolderShow=!1},newFolderCreate:function(e){var t=new RegExp("^[a-z0-9][a-z0-9-]*[a-z0-9]$");return i.newFolderName=_.kebabCase(_.trim(i.newFolderName)),_.isEmpty(i.newFolderName)||!t.test(i.newFolderName)?void(i.newFolderError=!0):(i.newFolderDiscard(),i.isLoading=!0,i.isLoadingText="Creating new folder...",void Vue.nextTick(function(){o.emit("uploadsCreateFolder",{foldername:i.newFolderName},function(e){i.folders=e,i.currentFolder=i.newFolderName,i.images=[],i.isLoading=!1})}))},fetchFromUrl:function(e){i.fetchFromUrlShow=!0},fetchFromUrlDiscard:function(e){i.fetchFromUrlShow=!1},selectFolder:function(e){i.currentFolder=e,i.loadImages()},refreshFolders:function(){i.isLoading=!0,i.isLoadingText="Fetching folders list...",i.currentFolder="",i.currentImage="",Vue.nextTick(function(){o.emit("uploadsGetFolders",{},function(e){i.folders=e,i.loadImages()})})},loadImages:function(){i.isLoading=!0,i.isLoadingText="Fetching images...",Vue.nextTick(function(){o.emit("uploadsGetImages",{folder:i.currentFolder},function(e){i.images=e,i.isLoading=!1,i.attachContextMenus()})})},selectImage:function(e){i.currentImage=e},selectAlignment:function(e){i.currentAlign=e},attachContextMenus:function(){var t=_.map(i.folders,function(e){return{name:""!==e?e:"/ (root)",icon:"fa-folder"}});e.contextMenu("destroy",".editor-modal-imagechoices > figure"),e.contextMenu({selector:".editor-modal-imagechoices > figure",appendTo:".editor-modal-imagechoices",position:function(t,o,a){e(t.$trigger).addClass("is-contextopen");var n=e(t.$trigger).position(),i={w:e(t.$trigger).width()/2,h:e(t.$trigger).height()/2};t.$menu.css({top:n.top+i.h,left:n.left+i.w})},events:{hide:function(t){e(t.$trigger).removeClass("is-contextopen")}},items:{rename:{name:"Rename",icon:"fa-edit",callback:function(e,t){alert("Clicked on "+e)}},move:{name:"Move to...",icon:"fa-folder-open-o",items:t},delete:{name:"Delete",icon:"fa-trash",callback:function(e,t){alert("Clicked on "+e)}}}})}}});e("#btn-editor-uploadimage input").on("change",function(o){e(o.currentTarget).simpleUpload("/uploads/img",{name:"imgfile",data:{folder:i.currentFolder},limit:20,expect:"json",allowedExts:["jpg","jpeg","gif","png","webp"],allowedTypes:["image/png","image/jpeg","image/gif","image/webp"],maxFileSize:3145728,init:function(){i.isLoading=!0,i.isLoadingText="Preparing to upload..."},progress:function(e){i.isLoadingText="Uploading..."+Math.round(e)+"%"},success:function(e){e.ok||t.pushError("Upload error",e.msg)},error:function(e){i.isLoading=!1,t.pushError(e.message,this.upload.file.name)},finish:function(){i.isLoading=!1}})});var r=ace.edit("codeblock-editor");r.setTheme("ace/theme/tomorrow_night"),r.getSession().setMode("ace/mode/markdown"),r.setOption("fontSize","14px"),r.setOption("hScrollBarAlwaysVisible",!1),r.setOption("wrap",!0);var c=ace.require("ace/ext/modelist"),l=new Vue({el:"#modal-editor-codeblock",data:{modes:c.modesByName,modeSelected:"text"},watch:{modeSelected:function(e,t){d(e).done(function(){ace.require("ace/mode/"+e),r.getSession().setMode("ace/mode/"+e)})}},methods:{cancel:function(t){n=!1,e("#modal-editor-codeblock").slideUp()},insertCode:function(e){a.codemirror.doc.somethingSelected()&&a.codemirror.execCommand("singleSelection");var t="\n```"+l.modeSelected+"\n"+r.getValue()+"\n```\n";a.codemirror.doc.replaceSelection(t),l.cancel()}}}),s=[],d=function(t){return e.ajax({url:"/js/ace/mode-"+t+".js",dataType:"script",cache:!0,beforeSend:function(){if(_.includes(s,t))return!1},success:function(){s.push(t)}})};a=new SimpleMDE({autofocus:!0,autoDownloadFontAwesome:!1,element:e("#mk-editor").get(0),placeholder:"Enter Markdown formatted content here...",spellChecker:!1,status:!1,toolbar:[{name:"bold",action:SimpleMDE.toggleBold,className:"fa fa-bold",title:"Bold"},{name:"italic",action:SimpleMDE.toggleItalic,className:"fa fa-italic",title:"Italic"},{name:"strikethrough",action:SimpleMDE.toggleStrikethrough,className:"fa fa-strikethrough",title:"Strikethrough"},"|",{name:"heading-1",action:SimpleMDE.toggleHeading1,className:"fa fa-header fa-header-x fa-header-1",title:"Big Heading"},{name:"heading-2",action:SimpleMDE.toggleHeading2,className:"fa fa-header fa-header-x fa-header-2",title:"Medium Heading"},{name:"heading-3",action:SimpleMDE.toggleHeading3,className:"fa fa-header fa-header-x fa-header-3",title:"Small Heading"},{name:"quote",action:SimpleMDE.toggleBlockquote,className:"fa fa-quote-left",title:"Quote"},"|",{name:"unordered-list",action:SimpleMDE.toggleUnorderedList,className:"fa fa-list-ul",title:"Bullet List"},{name:"ordered-list",action:SimpleMDE.toggleOrderedList,className:"fa fa-list-ol",title:"Numbered List"},"|",{name:"link",action:function(t){n||(n=!0,e("#modal-editor-link").slideToggle())},className:"fa fa-link",title:"Insert Link"},{name:"image",action:function(e){n||i.open()},className:"fa fa-image",title:"Insert Image"},{name:"file",action:function(e){},className:"fa fa-file-text-o",title:"Insert File"},"|",{name:"inline-code",action:function(e){if(!e.codemirror.doc.somethingSelected())return t.pushError("Invalid selection","You must select at least 1 character first.");var o=e.codemirror.doc.getSelections();o=_.map(o,function(e){return"`"+e+"`"}),e.codemirror.doc.replaceSelections(o)},className:"fa fa-terminal",title:"Inline Code"},{name:"code-block",action:function(t){n||(n=!0,a.codemirror.doc.somethingSelected()?r.setValue(a.codemirror.doc.getSelection()):r.setValue(""),e("#modal-editor-codeblock").slideDown(400,function(){r.resize(),r.focus()}))},className:"fa fa-code",title:"Code Block"},"|",{name:"table",action:function(e){},className:"fa fa-table",title:"Insert Table"},{name:"horizontal-rule",action:SimpleMDE.drawHorizontalRule,className:"fa fa-minus",title:"Horizontal Rule"}],shortcuts:{toggleBlockquote:null,toggleFullScreen:null}})}(),e(".btn-edit-save, .btn-create-save").on("click",function(o){e.ajax(window.location.href,{data:{markdown:a.value()},dataType:"json",method:"PUT"}).then(function(e,o,a){e.ok?window.location.assign("/"+n):t.pushError("Something went wrong",e.error)},function(e,o,a){t.pushError("Something went wrong","Save operation failed.")})})}()}if(e("#page-type-source").length){var n;!function(){n=ace.edit("source-display"),n.setTheme("ace/theme/tomorrow_night"),n.getSession().setMode("ace/mode/markdown"),n.setReadOnly(!0),n.renderer.updateFull();var o="home"!==e("#page-type-source").data("entrypath")?e("#page-type-source").data("entrypath"):"",a=o+"/new-page";e(".btn-create-prompt").on("click",function(t){e("#txt-create-prompt").val(a),e("#modal-create-prompt").toggleClass("is-active"),setInputSelection(e("#txt-create-prompt").get(0),o.length+1,a.length),e("#txt-create-prompt").removeClass("is-danger").next().addClass("is-hidden")}),e("#txt-create-prompt").on("keypress",function(t){13===t.which&&e(".btn-create-go").trigger("click")}),e(".btn-create-go").on("click",function(t){var o=makeSafePath(e("#txt-create-prompt").val());_.isEmpty(o)?e("#txt-create-prompt").addClass("is-danger").next().removeClass("is-hidden"):(e("#txt-create-prompt").parent().addClass("is-loading"),window.location.assign("/create/"+o))}),""!==o&&e(".btn-move-prompt").removeClass("is-hidden");var i=_.lastIndexOf(o,"/")+1;e(".btn-move-prompt").on("click",function(t){e("#txt-move-prompt").val(o),e("#modal-move-prompt").toggleClass("is-active"),setInputSelection(e("#txt-move-prompt").get(0),i,o.length),e("#txt-move-prompt").removeClass("is-danger").next().addClass("is-hidden")}),e("#txt-move-prompt").on("keypress",function(t){13===t.which&&e(".btn-move-go").trigger("click")}),e(".btn-move-go").on("click",function(a){var n=makeSafePath(e("#txt-move-prompt").val());_.isEmpty(n)||n===o||"home"===n?e("#txt-move-prompt").addClass("is-danger").next().removeClass("is-hidden"):(e("#txt-move-prompt").parent().addClass("is-loading"),e.ajax(window.location.href,{data:{move:n},dataType:"json",method:"PUT"}).then(function(e,o,a){e.ok?window.location.assign("/"+n):t.pushError("Something went wrong",e.error)},function(e,o,a){t.pushError("Something went wrong","Save operation failed.")}))})}()}});var Alerts=function(){function e(){_classCallCheck(this,e);var t=this;t.mdl=new Vue({el:"#alerts",data:{children:[]},methods:{acknowledge:function(e){t.close(e)}}}),t.uidNext=1}return _createClass(e,[{key:"push",value:function(e){var t=this,o=_.defaults(e,{_uid:t.uidNext,class:"is-info",message:"---",sticky:!1,title:"---"});t.mdl.children.push(o),o.sticky||_.delay(function(){t.close(o._uid)},5e3),t.uidNext++}},{key:"pushError",value:function(e,t){this.push({class:"is-danger",message:t,sticky:!1,title:e})}},{key:"pushSuccess",value:function(e,t){this.push({class:"is-success",message:t,sticky:!1,title:e})}},{key:"close",value:function(e){var t=this,o=_.findIndex(t.mdl.children,["_uid",e]),a=_.nth(t.mdl.children,o);o>=0&&a&&(a.class+=" exit",t.mdl.children.$set(o,a),_.delay(function(){t.mdl.children.$remove(a)},500))}}]),e}(); \ No newline at end of file +"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function setInputSelection(e,t,o){if(e.focus(),"undefined"!=typeof e.selectionStart)e.selectionStart=t,e.selectionEnd=o;else if(document.selection&&document.selection.createRange){e.select();var a=document.selection.createRange();a.collapse(!0),a.moveEnd("character",o),a.moveStart("character",t),a.select()}}function makeSafePath(e){var t=_.split(_.trim(e),"/");return t=_.map(t,function(e){return _.kebabCase(_.deburr(_.trim(e)))}),_.join(_.filter(t,function(e){return!_.isEmpty(e)}),"/")}var _createClass=function(){function e(e,t){for(var o=0;o=3?(t.searchactive=!0,t.searchload++,o.emit("search",{terms:e},function(e){t.searchres=e.match,t.searchsuggest=e.suggest,t.searchmovearr=_.concat([],t.searchres,t.searchsuggest),t.searchload>0&&t.searchload--})):(t.searchactive=!1,t.searchres=[],t.searchsuggest=[],t.searchmovearr=[],t.searchload=0)},searchmoveidx:function(e,o){e>0?t.searchmovekey=t.searchmovearr[e-1].document?"res."+t.searchmovearr[e-1].document.entryPath:"sug."+t.searchmovearr[e-1]:t.searchmovekey=""}},methods:{useSuggestion:function(e){t.searchq=e},closeSearch:function(){t.searchq=""},moveSelectSearch:function(){if(!(t.searchmoveidx<1)){var e=t.searchmoveidx-1;t.searchmovearr[e].document?window.location.assign("/"+t.searchmovearr[e].document.entryPath):t.searchq=t.searchmovearr[e]}},moveDownSearch:function(){t.searchmoveidx0&&t.searchmoveidx--}}});e("main").on("click",t.closeSearch)}}),e("#page-type-view").length&&!function(){var o="home"!==e("#page-type-view").data("entrypath")?e("#page-type-view").data("entrypath"):"",a=o+"/new-page";e(".btn-create-prompt").on("click",function(t){e("#txt-create-prompt").val(a),e("#modal-create-prompt").toggleClass("is-active"),setInputSelection(e("#txt-create-prompt").get(0),o.length+1,a.length),e("#txt-create-prompt").removeClass("is-danger").next().addClass("is-hidden")}),e("#txt-create-prompt").on("keypress",function(t){13===t.which&&e(".btn-create-go").trigger("click")}),e(".btn-create-go").on("click",function(t){var o=makeSafePath(e("#txt-create-prompt").val());_.isEmpty(o)?e("#txt-create-prompt").addClass("is-danger").next().removeClass("is-hidden"):(e("#txt-create-prompt").parent().addClass("is-loading"),window.location.assign("/create/"+o))}),""!==o&&e(".btn-move-prompt").removeClass("is-hidden");var n=_.lastIndexOf(o,"/")+1;e(".btn-move-prompt").on("click",function(t){e("#txt-move-prompt").val(o),e("#modal-move-prompt").toggleClass("is-active"),setInputSelection(e("#txt-move-prompt").get(0),n,o.length),e("#txt-move-prompt").removeClass("is-danger").next().addClass("is-hidden")}),e("#txt-move-prompt").on("keypress",function(t){13===t.which&&e(".btn-move-go").trigger("click")}),e(".btn-move-go").on("click",function(a){var n=makeSafePath(e("#txt-move-prompt").val());_.isEmpty(n)||n===o||"home"===n?e("#txt-move-prompt").addClass("is-danger").next().removeClass("is-hidden"):(e("#txt-move-prompt").parent().addClass("is-loading"),e.ajax(window.location.href,{data:{move:n},dataType:"json",method:"PUT"}).then(function(e,o,a){e.ok?window.location.assign("/"+n):t.pushError("Something went wrong",e.error)},function(e,o,a){t.pushError("Something went wrong","Save operation failed.")}))})}(),e("#page-type-create").length){var a;!function(){var n=e("#page-type-create").data("entrypath");e(".btn-create-discard").on("click",function(t){e("#modal-create-discard").toggleClass("is-active")}),1===e("#mk-editor").length&&!function(){var n=!1;Vue.filter("filesize",function(e){return _.toUpper(filesize(e))});var i=new Vue({el:"#modal-editor-image",data:{isLoading:!1,isLoadingText:"",newFolderName:"",newFolderShow:!1,newFolderError:!1,fetchFromUrlURL:"",fetchFromUrlShow:!1,folders:[],currentFolder:"",currentImage:"",currentAlign:"left",images:[],uploadSucceeded:!1,postUploadChecks:0},methods:{open:function(){n=!0,e("#modal-editor-image").slideDown(),i.refreshFolders()},cancel:function(t){n=!1,e("#modal-editor-image").slideUp()},insertImage:function(e){a.codemirror.doc.somethingSelected()&&a.codemirror.execCommand("singleSelection");var t=_.find(i.images,["uid",i.currentImage]);t.normalizedPath=""===t.folder?t.filename:t.folder+"/"+t.filename,t.titleGuess=_.startCase(t.basename);var o="!["+t.titleGuess+"](/uploads/"+t.normalizedPath+' "'+t.titleGuess+'")';switch(i.currentAlign){case"center":o+="{.align-center}";break;case"right":o+="{.align-right}";break;case"logo":o+="{.pagelogo}"}a.codemirror.doc.replaceSelection(o),i.cancel()},newFolder:function(t){i.newFolderName="",i.newFolderError=!1,i.newFolderShow=!0,_.delay(function(){e("#txt-editor-newfoldername").focus()},400)},newFolderDiscard:function(e){i.newFolderShow=!1},newFolderCreate:function(e){var t=new RegExp("^[a-z0-9][a-z0-9-]*[a-z0-9]$");return i.newFolderName=_.kebabCase(_.trim(i.newFolderName)),_.isEmpty(i.newFolderName)||!t.test(i.newFolderName)?void(i.newFolderError=!0):(i.newFolderDiscard(),i.isLoading=!0,i.isLoadingText="Creating new folder...",void Vue.nextTick(function(){o.emit("uploadsCreateFolder",{foldername:i.newFolderName},function(e){i.folders=e,i.currentFolder=i.newFolderName,i.images=[],i.isLoading=!1})}))},fetchFromUrl:function(e){i.fetchFromUrlShow=!0},fetchFromUrlDiscard:function(e){i.fetchFromUrlShow=!1},selectFolder:function(e){i.currentFolder=e,i.loadImages()},refreshFolders:function(){i.isLoading=!0,i.isLoadingText="Fetching folders list...",i.currentFolder="",i.currentImage="",Vue.nextTick(function(){o.emit("uploadsGetFolders",{},function(e){i.folders=e,i.loadImages()})})},loadImages:function(e){e||(i.isLoading=!0,i.isLoadingText="Fetching images..."),Vue.nextTick(function(){o.emit("uploadsGetImages",{folder:i.currentFolder},function(t){i.images=t,e||(i.isLoading=!1),i.attachContextMenus()})})},selectImage:function(e){i.currentImage=e},selectAlignment:function(e){i.currentAlign=e},attachContextMenus:function(){var t=_.map(i.folders,function(e){return{name:""!==e?e:"/ (root)",icon:"fa-folder"}});e.contextMenu("destroy",".editor-modal-imagechoices > figure"),e.contextMenu({selector:".editor-modal-imagechoices > figure",appendTo:".editor-modal-imagechoices",position:function(t,o,a){e(t.$trigger).addClass("is-contextopen");var n=e(t.$trigger).position(),i={w:e(t.$trigger).width()/2,h:e(t.$trigger).height()/2};t.$menu.css({top:n.top+i.h,left:n.left+i.w})},events:{hide:function(t){e(t.$trigger).removeClass("is-contextopen")}},items:{rename:{name:"Rename",icon:"fa-edit",callback:function(e,t){alert("Clicked on "+e)}},move:{name:"Move to...",icon:"fa-folder-open-o",items:t},delete:{name:"Delete",icon:"fa-trash",callback:function(e,t){alert("Clicked on "+e)}}}})},waitUploadComplete:function(){i.postUploadChecks++,i.isLoadingText="Processing uploads...";var e=i.images.length;i.loadImages(!0),Vue.nextTick(function(){_.delay(function(){e!==i.images.length?(i.postUploadChecks=0,i.isLoading=!1):i.postUploadChecks>5?(i.postUploadChecks=0,i.isLoading=!1,t.pushError("Unable to fetch new uploads","Try again later")):i.waitUploadComplete()},2e3)})}}});e("#btn-editor-uploadimage input").on("change",function(o){e(o.currentTarget).simpleUpload("/uploads/img",{name:"imgfile",data:{folder:i.currentFolder},limit:20,expect:"json",allowedExts:["jpg","jpeg","gif","png","webp"],allowedTypes:["image/png","image/jpeg","image/gif","image/webp"],maxFileSize:3145728,init:function(e){i.uploadSucceeded=!1,i.isLoading=!0,i.isLoadingText="Preparing to upload..."},progress:function(e){i.isLoadingText="Uploading..."+Math.round(e)+"%"},success:function(e){if(e.ok){var o=_.filter(e.results,["ok",!1]);o.length?(_.forEach(o,function(e){t.pushError("Upload error",e.msg)}),o.length figure"),e.contextMenu({selector:".editor-modal-imagechoices > figure",appendTo:".editor-modal-imagechoices",position:function(t,o,a){e(t.$trigger).addClass("is-contextopen");var n=e(t.$trigger).position(),i={w:e(t.$trigger).width()/2,h:e(t.$trigger).height()/2};t.$menu.css({top:n.top+i.h,left:n.left+i.w})},events:{hide:function(t){e(t.$trigger).removeClass("is-contextopen")}},items:{rename:{name:"Rename",icon:"fa-edit",callback:function(e,t){alert("Clicked on "+e)}},move:{name:"Move to...",icon:"fa-folder-open-o",items:t},delete:{name:"Delete",icon:"fa-trash",callback:function(e,t){alert("Clicked on "+e)}}}})},waitUploadComplete:function(){i.postUploadChecks++,i.isLoadingText="Processing uploads...";var e=i.images.length;i.loadImages(!0),Vue.nextTick(function(){_.delay(function(){e!==i.images.length?(i.postUploadChecks=0,i.isLoading=!1):i.postUploadChecks>5?(i.postUploadChecks=0,i.isLoading=!1,t.pushError("Unable to fetch new uploads","Try again later")):i.waitUploadComplete()},2e3)})}}});e("#btn-editor-uploadimage input").on("change",function(o){e(o.currentTarget).simpleUpload("/uploads/img",{name:"imgfile",data:{folder:i.currentFolder},limit:20,expect:"json",allowedExts:["jpg","jpeg","gif","png","webp"],allowedTypes:["image/png","image/jpeg","image/gif","image/webp"],maxFileSize:3145728,init:function(e){i.uploadSucceeded=!1,i.isLoading=!0,i.isLoadingText="Preparing to upload..."},progress:function(e){i.isLoadingText="Uploading..."+Math.round(e)+"%"},success:function(e){if(e.ok){var o=_.filter(e.results,["ok",!1]);o.length?(_.forEach(o,function(e){t.pushError("Upload error",e.msg)}),o.length=0&&a&&(a.class+=" exit",t.mdl.children.$set(o,a),_.delay(function(){t.mdl.children.$remove(a)},500))}}]),e}(); \ No newline at end of file diff --git a/client/js/components/editor-image.js b/client/js/components/editor-image.js index d856e750..e34c1ca9 100644 --- a/client/js/components/editor-image.js +++ b/client/js/components/editor-image.js @@ -13,7 +13,9 @@ let vueImage = new Vue({ currentFolder: '', currentImage: '', currentAlign: 'left', - images: [] + images: [], + uploadSucceeded: false, + postUploadChecks: 0 }, methods: { open: () => { @@ -126,13 +128,17 @@ let vueImage = new Vue({ * * @return {Void} Void */ - loadImages: () => { - vueImage.isLoading = true; - vueImage.isLoadingText = 'Fetching images...'; + loadImages: (silent) => { + if(!silent) { + vueImage.isLoading = true; + vueImage.isLoadingText = 'Fetching images...'; + } Vue.nextTick(() => { socket.emit('uploadsGetImages', { folder: vueImage.currentFolder }, (data) => { vueImage.images = data; - vueImage.isLoading = false; + if(!silent) { + vueImage.isLoading = false; + } vueImage.attachContextMenus(); }); }); @@ -209,6 +215,31 @@ let vueImage = new Vue({ } } }); + }, + + waitUploadComplete: () => { + + vueImage.postUploadChecks++; + vueImage.isLoadingText = 'Processing uploads...'; + + let currentUplAmount = vueImage.images.length; + vueImage.loadImages(true); + + Vue.nextTick(() => { + _.delay(() => { + if(currentUplAmount !== vueImage.images.length) { + vueImage.postUploadChecks = 0; + vueImage.isLoading = false; + } else if(vueImage.postUploadChecks > 5) { + vueImage.postUploadChecks = 0; + vueImage.isLoading = false; + alerts.pushError('Unable to fetch new uploads', 'Try again later'); + } else { + vueImage.waitUploadComplete(); + } + }, 2000); + }); + } } @@ -228,7 +259,8 @@ $('#btn-editor-uploadimage input').on('change', (ev) => { allowedTypes: ['image/png', 'image/jpeg', 'image/gif', 'image/webp'], maxFileSize: 3145728, // max 3 MB - init: () => { + init: (totalUploads) => { + vueImage.uploadSucceeded = false; vueImage.isLoading = true; vueImage.isLoadingText = 'Preparing to upload...'; }, @@ -240,18 +272,37 @@ $('#btn-editor-uploadimage input').on('change', (ev) => { success: (data) => { if(data.ok) { + let failedUpls = _.filter(data.results, ['ok', false]); + if(failedUpls.length) { + _.forEach(failedUpls, (u) => { + alerts.pushError('Upload error', u.msg); + }); + if(failedUpls.length < data.results.length) { + alerts.push({ + title: 'Some uploads succeeded', + message: 'Files that are not mentionned in the errors above were uploaded successfully.' + }); + vueImage.uploadSucceeded = true; + } + } else { + vueImage.uploadSucceeded = true; + } + } else { alerts.pushError('Upload error', data.msg); } }, error: function(error) { - vueImage.isLoading = false; alerts.pushError(error.message, this.upload.file.name); }, finish: () => { - vueImage.isLoading = false; + if(vueImage.uploadSucceeded) { + vueImage.waitUploadComplete(); + } else { + vueImage.isLoading = false; + } } }); diff --git a/controllers/pages.js b/controllers/pages.js index 38110aa8..1dfcd6f3 100644 --- a/controllers/pages.js +++ b/controllers/pages.js @@ -160,11 +160,15 @@ router.get('/*', (req, res, next) => { if(pageData) { return res.render('pages/view', { pageData }); } else { - res.render('error', { - message: err.message, - error: {} + res.render('error-notexist', { + newpath: safePath }); } + }).error((err) => { + res.render('error-notexist', { + message: err.message, + newpath: safePath + }); }).catch((err) => { res.render('error', { message: err.message, diff --git a/controllers/uploads.js b/controllers/uploads.js index f6ad8ef7..81eab1cb 100644 --- a/controllers/uploads.js +++ b/controllers/uploads.js @@ -72,13 +72,24 @@ router.post('/img', lcdata.uploadImgHandler, (req, res, next) => { }).then(() => { return { + ok: true, filename: destFilename, filesize: f.size }; - }); + }).reflect(); }, {concurrency: 3}).then((results) => { - res.json({ ok: true, results }); + let uplResults = _.map(results, (r) => { + if(r.isFulfilled()) { + return r.value(); + } else { + return { + ok: false, + msg: r.reason().message + } + } + }); + res.json({ ok: true, results: uplResults }); }).catch((err) => { res.json({ ok: false, msg: err.message }); }); diff --git a/models/entries.js b/models/entries.js index eba363ee..0f4db866 100644 --- a/models/entries.js +++ b/models/entries.js @@ -170,7 +170,7 @@ module.exports = { return false; } }).catch((err) => { - return Promise.reject(new Error('Entry ' + entryPath + ' does not exist!')); + return Promise.reject(new Promise.OperationalError('Entry ' + entryPath + ' does not exist!')); }); }, diff --git a/models/git.js b/models/git.js index 47f1d3cf..9c71dc6e 100644 --- a/models/git.js +++ b/models/git.js @@ -235,6 +235,23 @@ module.exports = { return true; }) + }, + + /** + * Commits uploads changes. + * + * @return {Promise} Resolve on commit success + */ + commitUploads() { + + let self = this; + + return self._git.add('uploads').then(() => { + return self._git.commit("Uploads repository sync").catch((err) => { + if(_.includes(err.stdout, 'nothing to commit')) { return true; } + }); + }); + } }; \ No newline at end of file diff --git a/models/localdata.js b/models/localdata.js index 5f8c503a..dbea0eca 100644 --- a/models/localdata.js +++ b/models/localdata.js @@ -267,6 +267,22 @@ module.exports = { }, + /** + * Parse relative Uploads path + * + * @param {String} f Relative Uploads path + * @return {Object} Parsed path (folder and filename) + */ + parseUploadsRelPath(f) { + + let fObj = path.parse(f); + return { + folder: fObj.dir, + filename: fObj.base + }; + + }, + /** * Sets the uploads files. * @@ -288,6 +304,19 @@ module.exports = { }, + /** + * Adds one or more uploads files. + * + * @param {Array} arrFiles The uploads files + * @return {Void} Void + */ + addUploadsFiles(arrFiles) { + if(_.isArray(arrFiles) || _.isPlainObject(arrFiles)) { + this._uploadsDb.Files.insert(arrFiles); + } + return; + }, + /** * Gets the uploads files. * @@ -301,41 +330,6 @@ module.exports = { '$and': [{ 'category' : cat },{ 'folder' : fld }] }).simplesort('filename').data(); - }, - - /** - * Generate thumbnail of image - * - * @param {String} sourcePath The source path - * @return {Promise} Promise returning the resized image info - */ - generateThumbnail(sourcePath, destPath) { - - let sharp = require('sharp'); - - return sharp(sourcePath) - .withoutEnlargement() - .resize(150,150) - .background('white') - .embed() - .flatten() - .toFormat('png') - .toFile(destPath); - - }, - - /** - * Gets the image metadata. - * - * @param {String} sourcePath The source path - * @return {Object} The image metadata. - */ - getImageMetadata(sourcePath) { - - let sharp = require('sharp'); - - return sharp(sourcePath).metadata(); - } }; \ No newline at end of file diff --git a/models/uploads.js b/models/uploads.js new file mode 100644 index 00000000..129c8afe --- /dev/null +++ b/models/uploads.js @@ -0,0 +1,176 @@ +"use strict"; + +var path = require('path'), + Promise = require('bluebird'), + fs = Promise.promisifyAll(require('fs-extra')), + readChunk = require('read-chunk'), + fileType = require('file-type'), + farmhash = require('farmhash'), + moment = require('moment'), + chokidar = require('chokidar'), + _ = require('lodash'); + +/** + * Uploads + * + * @param {Object} appconfig The application configuration + */ +module.exports = { + + _uploadsPath: './repo/uploads', + _uploadsThumbsPath: './data/thumbs', + + _watcher: null, + + /** + * Initialize Uploads model + * + * @param {Object} appconfig The application config + * @return {Object} Uploads model instance + */ + init(appconfig) { + + let self = this; + + self._uploadsPath = path.resolve(ROOTPATH, appconfig.datadir.repo, 'uploads'); + self._uploadsThumbsPath = path.resolve(ROOTPATH, appconfig.datadir.db, 'thumbs'); + + return self; + + }, + + watch() { + + let self = this; + + self._watcher = chokidar.watch(self._uploadsPath, { + persistent: true, + ignoreInitial: true, + cwd: self._uploadsPath, + depth: 1, + awaitWriteFinish: true + }); + + self._watcher.on('add', (p) => { + + let pInfo = lcdata.parseUploadsRelPath(p); + return self.processFile(pInfo.folder, pInfo.filename).then((mData) => { + ws.emit('uploadsAddFiles', { + auth: WSInternalKey, + content: mData + }); + }).then(() => { + return git.commitUploads(); + }); + + }); + + }, + + processFile(fldName, f) { + + let self = this; + + let fldPath = path.join(self._uploadsPath, fldName); + let fPath = path.join(fldPath, f); + let fPathObj = path.parse(fPath); + let fUid = farmhash.fingerprint32(fldName + '/' + f); + + return fs.statAsync(fPath).then((s) => { + + if(!s.isFile()) { return false; } + + // Get MIME info + + let mimeInfo = fileType(readChunk.sync(fPath, 0, 262)); + + // Images + + if(s.size < 3145728) { // ignore files larger than 3MB + if(_.includes(['image/png', 'image/jpeg', 'image/gif', 'image/webp'], mimeInfo.mime)) { + return self.getImageMetadata(fPath).then((mData) => { + + let cacheThumbnailPath = path.parse(path.join(self._uploadsThumbsPath, fUid + '.png')); + let cacheThumbnailPathStr = path.format(cacheThumbnailPath); + + mData = _.pick(mData, ['format', 'width', 'height', 'density', 'hasAlpha', 'orientation']); + mData.uid = fUid; + mData.category = 'image'; + mData.mime = mimeInfo.mime; + mData.folder = fldName; + mData.filename = f; + mData.basename = fPathObj.name; + mData.filesize = s.size; + mData.uploadedOn = moment().utc(); + + // Generate thumbnail + + return fs.statAsync(cacheThumbnailPathStr).then((st) => { + return st.isFile(); + }).catch((err) => { + return false; + }).then((thumbExists) => { + + return (thumbExists) ? mData : fs.ensureDirAsync(cacheThumbnailPath.dir).then(() => { + return self.generateThumbnail(fPath, cacheThumbnailPathStr); + }).return(mData); + + }); + + }) + } + } + + // Other Files + + return { + uid: fUid, + category: 'file', + mime: mimeInfo.mime, + folder: fldName, + filename: f, + basename: fPathObj.name, + filesize: s.size, + uploadedOn: moment().utc() + }; + + }); + + }, + + /** + * Generate thumbnail of image + * + * @param {String} sourcePath The source path + * @return {Promise} Promise returning the resized image info + */ + generateThumbnail(sourcePath, destPath) { + + let sharp = require('sharp'); + + return sharp(sourcePath) + .withoutEnlargement() + .resize(150,150) + .background('white') + .embed() + .flatten() + .toFormat('png') + .toFile(destPath); + + }, + + /** + * Gets the image metadata. + * + * @param {String} sourcePath The source path + * @return {Object} The image metadata. + */ + getImageMetadata(sourcePath) { + + let sharp = require('sharp'); + + return sharp(sourcePath).metadata(); + + } + +}; \ No newline at end of file diff --git a/package.json b/package.json index cc566c57..18510909 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "bson": "^0.5.5", "cheerio": "^0.22.0", "child-process-promise": "^2.1.3", + "chokidar": "^1.6.0", "compression": "^1.6.2", "connect-flash": "^0.1.1", "connect-loki": "^1.0.6", diff --git a/views/error-notexist.pug b/views/error-notexist.pug new file mode 100644 index 00000000..3f427759 --- /dev/null +++ b/views/error-notexist.pug @@ -0,0 +1,32 @@ +doctype html +html + head + meta(http-equiv='X-UA-Compatible', content='IE=edge') + meta(charset='UTF-8') + meta(name='viewport', content='width=device-width, initial-scale=1') + meta(name='theme-color', content='#009688') + meta(name='msapplication-TileColor', content='#009688') + meta(name='msapplication-TileImage', content='/favicons/ms-icon-144x144.png') + title= appconfig.title + + // Favicon + each favsize in [57, 60, 72, 76, 114, 120, 144, 152, 180] + link(rel='apple-touch-icon', sizes=favsize + 'x' + favsize, href='/favicons/apple-icon-' + favsize + 'x' + favsize + '.png') + link(rel='icon', type='image/png', sizes='192x192', href='/favicons/android-icon-192x192.png') + each favsize in [32, 96, 16] + link(rel='icon', type='image/png', sizes=favsize + 'x' + favsize, href='/favicons/favicon-' + favsize + 'x' + favsize + '.png') + link(rel='manifest', href='/manifest.json') + + // CSS + link(type='text/css', rel='stylesheet', href='/css/libs.css') + link(type='text/css', rel='stylesheet', href='/css/app.css') + + body(class='server-error') + section.hero.is-dark.is-fullheight + .hero-body + .container + a(href='/'): img(src='/favicons/android-icon-96x96.png') + h1.title(style={ 'margin-top': '30px'})= message + h2.subtitle(style={ 'margin-bottom': '50px'}) Would you like to create this entry? + a.button.is-dark.is-inverted(href='/create/' + newpath, style={'margin-right': '5px'}) Create + a.button.is-dark.is-inverted(href='/') Go Home \ No newline at end of file diff --git a/ws-server.js b/ws-server.js index 0b6bda93..0cf1d6ad 100644 --- a/ws-server.js +++ b/ws-server.js @@ -108,12 +108,14 @@ io.on('connection', (socket) => { }); socket.on('searchDel', (data, cb) => { + cb = cb || _.noop if(internalAuth.validateKey(data.auth)) { search.delete(data.entryPath); } }); socket.on('search', (data, cb) => { + cb = cb || _.noop search.find(data.terms).then((results) => { cb(results); }); @@ -124,28 +126,42 @@ io.on('connection', (socket) => { //----------------------------------------- socket.on('uploadsSetFolders', (data, cb) => { + cb = cb || _.noop if(internalAuth.validateKey(data.auth)) { lcdata.setUploadsFolders(data.content); } }); socket.on('uploadsGetFolders', (data, cb) => { + cb = cb || _.noop cb(lcdata.getUploadsFolders()); }); socket.on('uploadsCreateFolder', (data, cb) => { + cb = cb || _.noop lcdata.createUploadsFolder(data.foldername).then((fldList) => { cb(fldList); }); }); socket.on('uploadsSetFiles', (data, cb) => { + cb = cb || _.noop; if(internalAuth.validateKey(data.auth)) { lcdata.setUploadsFiles(data.content); + cb(true); + } + }); + + socket.on('uploadsAddFiles', (data, cb) => { + cb = cb || _.noop + if(internalAuth.validateKey(data.auth)) { + lcdata.addUploadsFiles(data.content); + cb(true); } }); socket.on('uploadsGetImages', (data, cb) => { + cb = cb || _.noop cb(lcdata.getUploadsFiles('image', data.folder)); });