mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 12:05:57 +01:00
upload changes, show progres in composer
This commit is contained in:
@@ -90,6 +90,7 @@ define(['taskbar'], function(taskbar) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
composer.posts[uuid] = post;
|
composer.posts[uuid] = post;
|
||||||
|
composer.posts[uuid].uploadsInProgress = [];
|
||||||
|
|
||||||
composer.load(uuid);
|
composer.load(uuid);
|
||||||
}
|
}
|
||||||
@@ -118,7 +119,7 @@ define(['taskbar'], function(taskbar) {
|
|||||||
var postContainer = $(composerTemplate[0]);
|
var postContainer = $(composerTemplate[0]);
|
||||||
|
|
||||||
if(config.allowFileUploads || config.hasImageUploadPlugin) {
|
if(config.allowFileUploads || config.hasImageUploadPlugin) {
|
||||||
initializeFileReader(post_uuid);
|
initializeDragAndDrop(post_uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
var postData = composer.posts[post_uuid],
|
var postData = composer.posts[post_uuid],
|
||||||
@@ -235,11 +236,8 @@ define(['taskbar'], function(taskbar) {
|
|||||||
$('#files').on('change', function(e) {
|
$('#files').on('change', function(e) {
|
||||||
var files = e.target.files;
|
var files = e.target.files;
|
||||||
if(files) {
|
if(files) {
|
||||||
for (var i=0; i<files.length; i++) {
|
uploadSubmit(files, post_uuid, '/api/post/upload');
|
||||||
loadFile(post_uuid, files[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$('#fileForm')[0].reset();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
postContainer.find('.nav-tabs a').click(function (e) {
|
postContainer.find('.nav-tabs a').click(function (e) {
|
||||||
@@ -256,6 +254,8 @@ define(['taskbar'], function(taskbar) {
|
|||||||
|
|
||||||
bodyEl.on('blur', function() {
|
bodyEl.on('blur', function() {
|
||||||
socket.emit('modules.composer.renderPreview', bodyEl.val(), function(err, preview) {
|
socket.emit('modules.composer.renderPreview', bodyEl.val(), function(err, preview) {
|
||||||
|
preview = $(preview);
|
||||||
|
preview.find('img').addClass('img-responsive');
|
||||||
postContainer.find('.preview').html(preview);
|
postContainer.find('.preview').html(preview);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -488,134 +488,149 @@ define(['taskbar'], function(taskbar) {
|
|||||||
taskbar.minimize('composer', post_uuid);
|
taskbar.minimize('composer', post_uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
function initializeFileReader(post_uuid) {
|
function initializeDragAndDrop(post_uuid) {
|
||||||
|
|
||||||
if(jQuery.event.props.indexOf('dataTransfer') === -1) {
|
if(jQuery.event.props.indexOf('dataTransfer') === -1) {
|
||||||
jQuery.event.props.push('dataTransfer');
|
jQuery.event.props.push('dataTransfer');
|
||||||
}
|
}
|
||||||
|
|
||||||
var draggingDocument = false;
|
var draggingDocument = false;
|
||||||
|
|
||||||
if(window.FileReader) {
|
var postContainer = $('#cmp-uuid-' + post_uuid),
|
||||||
var postContainer = $('#cmp-uuid-' + post_uuid),
|
fileForm = postContainer.find('#fileForm');
|
||||||
drop = postContainer.find('.imagedrop'),
|
drop = postContainer.find('.imagedrop'),
|
||||||
tabContent = postContainer.find('.tab-content'),
|
tabContent = postContainer.find('.tab-content'),
|
||||||
textarea = postContainer.find('textarea');
|
textarea = postContainer.find('textarea');
|
||||||
|
|
||||||
$(document).off('dragstart').on('dragstart', function(e) {
|
$(document).off('dragstart').on('dragstart', function(e) {
|
||||||
draggingDocument = true;
|
draggingDocument = true;
|
||||||
}).off('dragend').on('dragend', function(e) {
|
}).off('dragend').on('dragend', function(e) {
|
||||||
draggingDocument = false;
|
draggingDocument = false;
|
||||||
});
|
|
||||||
|
|
||||||
textarea.on('dragenter', function(e) {
|
|
||||||
if(draggingDocument) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
drop.css('top', tabContent.position().top + 'px');
|
|
||||||
drop.css('height', textarea.height());
|
|
||||||
drop.css('line-height', textarea.height() + 'px');
|
|
||||||
drop.show();
|
|
||||||
|
|
||||||
drop.on('dragleave', function(ev) {
|
|
||||||
drop.hide();
|
|
||||||
drop.off('dragleave');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
function cancel(e) {
|
|
||||||
e.preventDefault();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
drop.on('dragover', cancel);
|
|
||||||
drop.on('dragenter', cancel);
|
|
||||||
|
|
||||||
drop.on('drop', function(e) {
|
|
||||||
e.preventDefault();
|
|
||||||
var dt = e.dataTransfer,
|
|
||||||
files = dt.files;
|
|
||||||
|
|
||||||
for (var i=0; i<files.length; i++) {
|
|
||||||
loadFile(post_uuid, files[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!files.length) {
|
|
||||||
drop.hide();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
|
|
||||||
$(window).off('paste').on('paste', function(event) {
|
|
||||||
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
|
|
||||||
if(items && items.length) {
|
|
||||||
var blob = items[0].getAsFile();
|
|
||||||
loadFile(post_uuid, blob);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadFile(post_uuid, file) {
|
|
||||||
var reader = new FileReader(),
|
|
||||||
dropDiv = $('#cmp-uuid-' + post_uuid).find('.imagedrop');
|
|
||||||
|
|
||||||
$(reader).on('loadend', function(e) {
|
|
||||||
var regex = /^data:.*;base64,(.*)$/;
|
|
||||||
var matches = this.result.match(regex);
|
|
||||||
|
|
||||||
var fileData = {
|
|
||||||
name: file.name || '',
|
|
||||||
data: matches[1]
|
|
||||||
};
|
|
||||||
|
|
||||||
dropDiv.hide();
|
|
||||||
|
|
||||||
if(file.type.match('image.*')) {
|
|
||||||
uploadFile('posts.uploadImage', post_uuid, fileData);
|
|
||||||
} else {
|
|
||||||
if(file.size > parseInt(config.maximumFileSize, 10) * 1024) {
|
|
||||||
return composerAlert('File too big', 'Maximum allowed file size is ' + config.maximumFileSize + 'kbs');
|
|
||||||
}
|
|
||||||
uploadFile('posts.uploadFile', post_uuid, fileData);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
reader.readAsDataURL(file);
|
textarea.on('dragenter', function(e) {
|
||||||
|
if(draggingDocument) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
drop.css('top', tabContent.position().top + 'px');
|
||||||
|
drop.css('height', textarea.height());
|
||||||
|
drop.css('line-height', textarea.height() + 'px');
|
||||||
|
drop.show();
|
||||||
|
|
||||||
|
drop.on('dragleave', function(ev) {
|
||||||
|
drop.hide();
|
||||||
|
drop.off('dragleave');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function cancel(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
drop.on('dragover', cancel);
|
||||||
|
drop.on('dragenter', cancel);
|
||||||
|
|
||||||
|
drop.on('drop', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
var dt = e.dataTransfer,
|
||||||
|
files = dt.files;
|
||||||
|
|
||||||
|
if(files.length) {
|
||||||
|
fileForm[0].reset();
|
||||||
|
fileForm.find('#files')[0].files = files;
|
||||||
|
}
|
||||||
|
|
||||||
|
drop.hide();
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
$(window).off('paste').on('paste', function(event) {
|
||||||
|
|
||||||
|
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
|
||||||
|
|
||||||
|
if(items && items.length) {
|
||||||
|
|
||||||
|
var blob = items[0].getAsFile();
|
||||||
|
blob.name = 'upload-'+ utils.generateUUID();
|
||||||
|
|
||||||
|
var fd = new FormData();
|
||||||
|
fd.append('files[]', blob, blob.name);
|
||||||
|
|
||||||
|
fileForm[0].reset();
|
||||||
|
uploadSubmit([blob], post_uuid, '/api/post/upload', fd);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function uploadSubmit(files, post_uuid, route, formData, callback) {
|
||||||
function uploadFile(method, post_uuid, img) {
|
var postContainer = $('#cmp-uuid-' + post_uuid),
|
||||||
var linkStart = method === 'posts.uploadImage' ? '!' : '',
|
|
||||||
postContainer = $('#cmp-uuid-' + post_uuid),
|
|
||||||
textarea = postContainer.find('textarea'),
|
textarea = postContainer.find('textarea'),
|
||||||
text = textarea.val(),
|
text = textarea.val(),
|
||||||
imgText = linkStart + '[' + img.name + '](uploading...)';
|
uploadForm = postContainer.find('#fileForm');
|
||||||
|
|
||||||
text += imgText;
|
uploadForm.attr('action', route);
|
||||||
textarea.val(text + " ");
|
|
||||||
|
|
||||||
if(!composer.posts[post_uuid].uploadsInProgress) {
|
for(var i=0; i<files.length; ++i) {
|
||||||
composer.posts[post_uuid].uploadsInProgress = [];
|
var isImage = files[i].type.match('image.*');
|
||||||
|
text += (isImage ? '!' : '') + '[' + files[i].name + '](uploading...) ';
|
||||||
|
|
||||||
|
if(files[i].size > parseInt(config.maximumFileSize, 10) * 1024) {
|
||||||
|
uploadForm[0].reset();
|
||||||
|
return composerAlert('File too big', 'Maximum allowed file size is ' + config.maximumFileSize + 'kbs');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
composer.posts[post_uuid].uploadsInProgress.push(1);
|
textarea.val(text);
|
||||||
|
|
||||||
socket.emit(method, img, function(err, data) {
|
uploadForm.off('submit').submit(function() {
|
||||||
|
|
||||||
var currentText = textarea.val();
|
$(this).find('#postUploadCsrf').val($('#csrf_token').val());
|
||||||
|
if(formData) {
|
||||||
if(err) {
|
formData.append('_csrf', $('#csrf_token').val());
|
||||||
textarea.val(currentText.replace(imgText, linkStart + '[' + img.name + ']( Error : ' + err.message + ')'));
|
|
||||||
return app.alertError(err.message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
textarea.val(currentText.replace(imgText, linkStart + '[' + data.name + '](' + data.url + ')'));
|
composer.posts[post_uuid].uploadsInProgress.push(1);
|
||||||
|
|
||||||
composer.posts[post_uuid].uploadsInProgress.pop();
|
$(this).ajaxSubmit({
|
||||||
textarea.focus();
|
resetForm: true,
|
||||||
|
clearForm: true,
|
||||||
|
formData: formData,
|
||||||
|
error: function(xhr) {
|
||||||
|
app.alertError('Error uploading file! ' + xhr.status);
|
||||||
|
composer.posts[post_uuid].uploadsInProgress.pop();
|
||||||
|
},
|
||||||
|
|
||||||
|
uploadProgress: function(event, position, total, percent) {
|
||||||
|
var current = textarea.val();
|
||||||
|
for(var i=0; i<files.length; ++i) {
|
||||||
|
var re = new RegExp(files[i].name + "]\\([^)]+\\)", 'g');
|
||||||
|
textarea.val(current.replace(re, files[i].name+'](uploading ' + percent + '%)'));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
success: function(uploads) {
|
||||||
|
|
||||||
|
if(uploads && uploads.length) {
|
||||||
|
for(var i=0; i<uploads.length; ++i) {
|
||||||
|
var current = textarea.val();
|
||||||
|
var re = new RegExp(uploads[i].name + "]\\([^)]+\\)", 'g');
|
||||||
|
textarea.val(current.replace(re, uploads[i].name + '](' + uploads[i].url + ')'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
composer.posts[post_uuid].uploadsInProgress.pop();
|
||||||
|
textarea.focus();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
uploadForm.submit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
newTopic: composer.newTopic,
|
newTopic: composer.newTopic,
|
||||||
newReply: composer.newReply,
|
newReply: composer.newReply,
|
||||||
|
|||||||
@@ -13,8 +13,10 @@
|
|||||||
<span class="btn btn-link file-upload-btn hide" tabindex="-1">
|
<span class="btn btn-link file-upload-btn hide" tabindex="-1">
|
||||||
<i class="fa fa-upload"></i>
|
<i class="fa fa-upload"></i>
|
||||||
</span>
|
</span>
|
||||||
<form id="fileForm">
|
|
||||||
<input type="file" id="files" name="files[]" multiple class="hide"/>
|
<form id="fileForm" method="post" enctype="multipart/form-data">
|
||||||
|
<input type="file" id="files" name="files[]" multiple class="hide"/>
|
||||||
|
<input id="postUploadCsrf" type="hidden" name="_csrf">
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
237
public/vendor/jquery/js/jquery.form.js
vendored
237
public/vendor/jquery/js/jquery.form.js
vendored
@@ -1,7 +1,7 @@
|
|||||||
/*!
|
/*!
|
||||||
* jQuery Form Plugin
|
* jQuery Form Plugin
|
||||||
* version: 3.32.0-2013.04.09
|
* version: 3.49.0-2014.02.05
|
||||||
* @requires jQuery v1.5 or later
|
* Requires jQuery v1.5 or later
|
||||||
* Copyright (c) 2013 M. Alsup
|
* Copyright (c) 2013 M. Alsup
|
||||||
* Examples and documentation at: http://malsup.com/jquery/form/
|
* Examples and documentation at: http://malsup.com/jquery/form/
|
||||||
* Project repository: https://github.com/malsup/form
|
* Project repository: https://github.com/malsup/form
|
||||||
@@ -9,7 +9,20 @@
|
|||||||
* https://github.com/malsup/form#copyright-and-license
|
* https://github.com/malsup/form#copyright-and-license
|
||||||
*/
|
*/
|
||||||
/*global ActiveXObject */
|
/*global ActiveXObject */
|
||||||
;(function($) {
|
|
||||||
|
// AMD support
|
||||||
|
(function (factory) {
|
||||||
|
"use strict";
|
||||||
|
if (typeof define === 'function' && define.amd) {
|
||||||
|
// using AMD; register as anon module
|
||||||
|
define(['jquery'], factory);
|
||||||
|
} else {
|
||||||
|
// no AMD; invoke directly
|
||||||
|
factory( (typeof(jQuery) != 'undefined') ? jQuery : window.Zepto );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(function($) {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -63,11 +76,13 @@ var hasProp = !!$.fn.prop;
|
|||||||
// contains inputs with names like "action" or "method"; in those
|
// contains inputs with names like "action" or "method"; in those
|
||||||
// cases "prop" returns the element
|
// cases "prop" returns the element
|
||||||
$.fn.attr2 = function() {
|
$.fn.attr2 = function() {
|
||||||
if ( ! hasProp )
|
if ( ! hasProp ) {
|
||||||
return this.attr.apply(this, arguments);
|
return this.attr.apply(this, arguments);
|
||||||
|
}
|
||||||
var val = this.prop.apply(this, arguments);
|
var val = this.prop.apply(this, arguments);
|
||||||
if ( ( val && val.jquery ) || typeof val === 'string' )
|
if ( ( val && val.jquery ) || typeof val === 'string' ) {
|
||||||
return val;
|
return val;
|
||||||
|
}
|
||||||
return this.attr.apply(this, arguments);
|
return this.attr.apply(this, arguments);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -89,9 +104,12 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
if (typeof options == 'function') {
|
if (typeof options == 'function') {
|
||||||
options = { success: options };
|
options = { success: options };
|
||||||
}
|
}
|
||||||
|
else if ( options === undefined ) {
|
||||||
|
options = {};
|
||||||
|
}
|
||||||
|
|
||||||
method = this.attr2('method');
|
method = options.type || this.attr2('method');
|
||||||
action = this.attr2('action');
|
action = options.url || this.attr2('action');
|
||||||
|
|
||||||
url = (typeof action === 'string') ? $.trim(action) : '';
|
url = (typeof action === 'string') ? $.trim(action) : '';
|
||||||
url = url || window.location.href || '';
|
url = url || window.location.href || '';
|
||||||
@@ -103,7 +121,7 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
options = $.extend(true, {
|
options = $.extend(true, {
|
||||||
url: url,
|
url: url,
|
||||||
success: $.ajaxSettings.success,
|
success: $.ajaxSettings.success,
|
||||||
type: method || 'GET',
|
type: method || $.ajaxSettings.type,
|
||||||
iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank'
|
iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank'
|
||||||
}, options);
|
}, options);
|
||||||
|
|
||||||
@@ -186,11 +204,27 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (options.error) {
|
||||||
|
var oldError = options.error;
|
||||||
|
options.error = function(xhr, status, error) {
|
||||||
|
var context = options.context || this;
|
||||||
|
oldError.apply(context, [xhr, status, error, $form]);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.complete) {
|
||||||
|
var oldComplete = options.complete;
|
||||||
|
options.complete = function(xhr, status) {
|
||||||
|
var context = options.context || this;
|
||||||
|
oldComplete.apply(context, [xhr, status, $form]);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// are there files to upload?
|
// are there files to upload?
|
||||||
|
|
||||||
// [value] (issue #113), also see comment:
|
// [value] (issue #113), also see comment:
|
||||||
// https://github.com/malsup/form/commit/588306aedba1de01388032d5f42a60159eea9228#commitcomment-2180219
|
// https://github.com/malsup/form/commit/588306aedba1de01388032d5f42a60159eea9228#commitcomment-2180219
|
||||||
var fileInputs = $('input[type=file]:enabled[value!=""]', this);
|
var fileInputs = $('input[type=file]:enabled', this).filter(function() { return $(this).val() !== ''; });
|
||||||
|
|
||||||
var hasFileInputs = fileInputs.length > 0;
|
var hasFileInputs = fileInputs.length > 0;
|
||||||
var mp = 'multipart/form-data';
|
var mp = 'multipart/form-data';
|
||||||
@@ -226,8 +260,9 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
$form.removeData('jqxhr').data('jqxhr', jqxhr);
|
$form.removeData('jqxhr').data('jqxhr', jqxhr);
|
||||||
|
|
||||||
// clear element array
|
// clear element array
|
||||||
for (var k=0; k < elements.length; k++)
|
for (var k=0; k < elements.length; k++) {
|
||||||
elements[k] = null;
|
elements[k] = null;
|
||||||
|
}
|
||||||
|
|
||||||
// fire 'notify' event
|
// fire 'notify' event
|
||||||
this.trigger('form-submit-notify', [this, options]);
|
this.trigger('form-submit-notify', [this, options]);
|
||||||
@@ -235,7 +270,7 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
|
|
||||||
// utility fn for deep serialization
|
// utility fn for deep serialization
|
||||||
function deepSerialize(extraData){
|
function deepSerialize(extraData){
|
||||||
var serialized = $.param(extraData).split('&');
|
var serialized = $.param(extraData, options.traditional).split('&');
|
||||||
var len = serialized.length;
|
var len = serialized.length;
|
||||||
var result = [];
|
var result = [];
|
||||||
var i, part;
|
var i, part;
|
||||||
@@ -259,9 +294,11 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
|
|
||||||
if (options.extraData) {
|
if (options.extraData) {
|
||||||
var serializedData = deepSerialize(options.extraData);
|
var serializedData = deepSerialize(options.extraData);
|
||||||
for (i=0; i < serializedData.length; i++)
|
for (i=0; i < serializedData.length; i++) {
|
||||||
if (serializedData[i])
|
if (serializedData[i]) {
|
||||||
formdata.append(serializedData[i][0], serializedData[i][1]);
|
formdata.append(serializedData[i][0], serializedData[i][1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
options.data = null;
|
options.data = null;
|
||||||
@@ -276,7 +313,7 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
if (options.uploadProgress) {
|
if (options.uploadProgress) {
|
||||||
// workaround because jqXHR does not expose upload property
|
// workaround because jqXHR does not expose upload property
|
||||||
s.xhr = function() {
|
s.xhr = function() {
|
||||||
var xhr = jQuery.ajaxSettings.xhr();
|
var xhr = $.ajaxSettings.xhr();
|
||||||
if (xhr.upload) {
|
if (xhr.upload) {
|
||||||
xhr.upload.addEventListener('progress', function(event) {
|
xhr.upload.addEventListener('progress', function(event) {
|
||||||
var percent = 0;
|
var percent = 0;
|
||||||
@@ -293,11 +330,18 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
s.data = null;
|
s.data = null;
|
||||||
var beforeSend = s.beforeSend;
|
var beforeSend = s.beforeSend;
|
||||||
s.beforeSend = function(xhr, o) {
|
s.beforeSend = function(xhr, o) {
|
||||||
|
//Send FormData() provided by user
|
||||||
|
if (options.formData) {
|
||||||
|
o.data = options.formData;
|
||||||
|
}
|
||||||
|
else {
|
||||||
o.data = formdata;
|
o.data = formdata;
|
||||||
if(beforeSend)
|
}
|
||||||
beforeSend.call(this, xhr, o);
|
if(beforeSend) {
|
||||||
|
beforeSend.call(this, xhr, o);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
return $.ajax(s);
|
return $.ajax(s);
|
||||||
}
|
}
|
||||||
@@ -307,14 +351,21 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
var form = $form[0], el, i, s, g, id, $io, io, xhr, sub, n, timedOut, timeoutHandle;
|
var form = $form[0], el, i, s, g, id, $io, io, xhr, sub, n, timedOut, timeoutHandle;
|
||||||
var deferred = $.Deferred();
|
var deferred = $.Deferred();
|
||||||
|
|
||||||
|
// #341
|
||||||
|
deferred.abort = function(status) {
|
||||||
|
xhr.abort(status);
|
||||||
|
};
|
||||||
|
|
||||||
if (a) {
|
if (a) {
|
||||||
// ensure that every serialized input is still enabled
|
// ensure that every serialized input is still enabled
|
||||||
for (i=0; i < elements.length; i++) {
|
for (i=0; i < elements.length; i++) {
|
||||||
el = $(elements[i]);
|
el = $(elements[i]);
|
||||||
if ( hasProp )
|
if ( hasProp ) {
|
||||||
el.prop('disabled', false);
|
el.prop('disabled', false);
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
el.removeAttr('disabled');
|
el.removeAttr('disabled');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -324,10 +375,12 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
if (s.iframeTarget) {
|
if (s.iframeTarget) {
|
||||||
$io = $(s.iframeTarget);
|
$io = $(s.iframeTarget);
|
||||||
n = $io.attr2('name');
|
n = $io.attr2('name');
|
||||||
if (!n)
|
if (!n) {
|
||||||
$io.attr2('name', id);
|
$io.attr2('name', id);
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
id = n;
|
id = n;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$io = $('<iframe name="' + id + '" src="'+ s.iframeSrc +'" />');
|
$io = $('<iframe name="' + id + '" src="'+ s.iframeSrc +'" />');
|
||||||
@@ -359,12 +412,15 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
|
|
||||||
$io.attr('src', s.iframeSrc); // abort op in progress
|
$io.attr('src', s.iframeSrc); // abort op in progress
|
||||||
xhr.error = e;
|
xhr.error = e;
|
||||||
if (s.error)
|
if (s.error) {
|
||||||
s.error.call(s.context, xhr, e, status);
|
s.error.call(s.context, xhr, e, status);
|
||||||
if (g)
|
}
|
||||||
|
if (g) {
|
||||||
$.event.trigger("ajaxError", [xhr, s, e]);
|
$.event.trigger("ajaxError", [xhr, s, e]);
|
||||||
if (s.complete)
|
}
|
||||||
|
if (s.complete) {
|
||||||
s.complete.call(s.context, xhr, e);
|
s.complete.call(s.context, xhr, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -451,11 +507,14 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
// take a breath so that pending repaints get some cpu time before the upload starts
|
// take a breath so that pending repaints get some cpu time before the upload starts
|
||||||
function doSubmit() {
|
function doSubmit() {
|
||||||
// make sure form attrs are set
|
// make sure form attrs are set
|
||||||
var t = $form.attr2('target'), a = $form.attr2('action');
|
var t = $form.attr2('target'),
|
||||||
|
a = $form.attr2('action'),
|
||||||
|
mp = 'multipart/form-data',
|
||||||
|
et = $form.attr('enctype') || $form.attr('encoding') || mp;
|
||||||
|
|
||||||
// update form attrs in IE friendly way
|
// update form attrs in IE friendly way
|
||||||
form.setAttribute('target',id);
|
form.setAttribute('target',id);
|
||||||
if (!method) {
|
if (!method || /post/i.test(method) ) {
|
||||||
form.setAttribute('method', 'POST');
|
form.setAttribute('method', 'POST');
|
||||||
}
|
}
|
||||||
if (a != s.url) {
|
if (a != s.url) {
|
||||||
@@ -480,14 +539,16 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
try {
|
try {
|
||||||
var state = getDoc(io).readyState;
|
var state = getDoc(io).readyState;
|
||||||
log('state = ' + state);
|
log('state = ' + state);
|
||||||
if (state && state.toLowerCase() == 'uninitialized')
|
if (state && state.toLowerCase() == 'uninitialized') {
|
||||||
setTimeout(checkState,50);
|
setTimeout(checkState,50);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch(e) {
|
catch(e) {
|
||||||
log('Server abort: ' , e, ' (', e.name, ')');
|
log('Server abort: ' , e, ' (', e.name, ')');
|
||||||
cb(SERVER_ABORT);
|
cb(SERVER_ABORT);
|
||||||
if (timeoutHandle)
|
if (timeoutHandle) {
|
||||||
clearTimeout(timeoutHandle);
|
clearTimeout(timeoutHandle);
|
||||||
|
}
|
||||||
timeoutHandle = undefined;
|
timeoutHandle = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -515,10 +576,12 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
if (!s.iframeTarget) {
|
if (!s.iframeTarget) {
|
||||||
// add iframe to doc and submit the form
|
// add iframe to doc and submit the form
|
||||||
$io.appendTo('body');
|
$io.appendTo('body');
|
||||||
if (io.attachEvent)
|
}
|
||||||
io.attachEvent('onload', cb);
|
if (io.attachEvent) {
|
||||||
else
|
io.attachEvent('onload', cb);
|
||||||
io.addEventListener('load', cb, false);
|
}
|
||||||
|
else {
|
||||||
|
io.addEventListener('load', cb, false);
|
||||||
}
|
}
|
||||||
setTimeout(checkState,15);
|
setTimeout(checkState,15);
|
||||||
|
|
||||||
@@ -533,6 +596,7 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
finally {
|
finally {
|
||||||
// reset attrs and remove "extra" input elements
|
// reset attrs and remove "extra" input elements
|
||||||
form.setAttribute('action',a);
|
form.setAttribute('action',a);
|
||||||
|
form.setAttribute('enctype', et); // #380
|
||||||
if(t) {
|
if(t) {
|
||||||
form.setAttribute('target', t);
|
form.setAttribute('target', t);
|
||||||
} else {
|
} else {
|
||||||
@@ -574,13 +638,16 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
|
|
||||||
if (!doc || doc.location.href == s.iframeSrc) {
|
if (!doc || doc.location.href == s.iframeSrc) {
|
||||||
// response not received yet
|
// response not received yet
|
||||||
if (!timedOut)
|
if (!timedOut) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (io.detachEvent)
|
if (io.detachEvent) {
|
||||||
io.detachEvent('onload', cb);
|
io.detachEvent('onload', cb);
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
io.removeEventListener('load', cb, false);
|
io.removeEventListener('load', cb, false);
|
||||||
|
}
|
||||||
|
|
||||||
var status = 'success', errMsg;
|
var status = 'success', errMsg;
|
||||||
try {
|
try {
|
||||||
@@ -607,11 +674,12 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
var docRoot = doc.body ? doc.body : doc.documentElement;
|
var docRoot = doc.body ? doc.body : doc.documentElement;
|
||||||
xhr.responseText = docRoot ? docRoot.innerHTML : null;
|
xhr.responseText = docRoot ? docRoot.innerHTML : null;
|
||||||
xhr.responseXML = doc.XMLDocument ? doc.XMLDocument : doc;
|
xhr.responseXML = doc.XMLDocument ? doc.XMLDocument : doc;
|
||||||
if (isXml)
|
if (isXml) {
|
||||||
s.dataType = 'xml';
|
s.dataType = 'xml';
|
||||||
|
}
|
||||||
xhr.getResponseHeader = function(header){
|
xhr.getResponseHeader = function(header){
|
||||||
var headers = {'content-type': s.dataType};
|
var headers = {'content-type': s.dataType};
|
||||||
return headers[header];
|
return headers[header.toLowerCase()];
|
||||||
};
|
};
|
||||||
// support for XHR 'status' & 'statusText' emulation :
|
// support for XHR 'status' & 'statusText' emulation :
|
||||||
if (docRoot) {
|
if (docRoot) {
|
||||||
@@ -671,40 +739,52 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
|
|
||||||
// ordering of these callbacks/triggers is odd, but that's how $.ajax does it
|
// ordering of these callbacks/triggers is odd, but that's how $.ajax does it
|
||||||
if (status === 'success') {
|
if (status === 'success') {
|
||||||
if (s.success)
|
if (s.success) {
|
||||||
s.success.call(s.context, data, 'success', xhr);
|
s.success.call(s.context, data, 'success', xhr);
|
||||||
|
}
|
||||||
deferred.resolve(xhr.responseText, 'success', xhr);
|
deferred.resolve(xhr.responseText, 'success', xhr);
|
||||||
if (g)
|
if (g) {
|
||||||
$.event.trigger("ajaxSuccess", [xhr, s]);
|
$.event.trigger("ajaxSuccess", [xhr, s]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (status) {
|
else if (status) {
|
||||||
if (errMsg === undefined)
|
if (errMsg === undefined) {
|
||||||
errMsg = xhr.statusText;
|
errMsg = xhr.statusText;
|
||||||
if (s.error)
|
}
|
||||||
|
if (s.error) {
|
||||||
s.error.call(s.context, xhr, status, errMsg);
|
s.error.call(s.context, xhr, status, errMsg);
|
||||||
|
}
|
||||||
deferred.reject(xhr, 'error', errMsg);
|
deferred.reject(xhr, 'error', errMsg);
|
||||||
if (g)
|
if (g) {
|
||||||
$.event.trigger("ajaxError", [xhr, s, errMsg]);
|
$.event.trigger("ajaxError", [xhr, s, errMsg]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (g)
|
if (g) {
|
||||||
$.event.trigger("ajaxComplete", [xhr, s]);
|
$.event.trigger("ajaxComplete", [xhr, s]);
|
||||||
|
}
|
||||||
|
|
||||||
if (g && ! --$.active) {
|
if (g && ! --$.active) {
|
||||||
$.event.trigger("ajaxStop");
|
$.event.trigger("ajaxStop");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (s.complete)
|
if (s.complete) {
|
||||||
s.complete.call(s.context, xhr, status);
|
s.complete.call(s.context, xhr, status);
|
||||||
|
}
|
||||||
|
|
||||||
callbackProcessed = true;
|
callbackProcessed = true;
|
||||||
if (s.timeout)
|
if (s.timeout) {
|
||||||
clearTimeout(timeoutHandle);
|
clearTimeout(timeoutHandle);
|
||||||
|
}
|
||||||
|
|
||||||
// clean up
|
// clean up
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
if (!s.iframeTarget)
|
if (!s.iframeTarget) {
|
||||||
$io.remove();
|
$io.remove();
|
||||||
|
}
|
||||||
|
else { //adding else to clean up existing iframe response.
|
||||||
|
$io.attr('src', s.iframeSrc);
|
||||||
|
}
|
||||||
xhr.responseXML = null;
|
xhr.responseXML = null;
|
||||||
}, 100);
|
}, 100);
|
||||||
}
|
}
|
||||||
@@ -732,8 +812,9 @@ $.fn.ajaxSubmit = function(options) {
|
|||||||
data = xml ? xhr.responseXML : xhr.responseText;
|
data = xml ? xhr.responseXML : xhr.responseText;
|
||||||
|
|
||||||
if (xml && data.documentElement.nodeName === 'parsererror') {
|
if (xml && data.documentElement.nodeName === 'parsererror') {
|
||||||
if ($.error)
|
if ($.error) {
|
||||||
$.error('parsererror');
|
$.error('parsererror');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (s && s.dataFilter) {
|
if (s && s.dataFilter) {
|
||||||
data = s.dataFilter(data, type);
|
data = s.dataFilter(data, type);
|
||||||
@@ -806,7 +887,7 @@ function doAjaxSubmit(e) {
|
|||||||
var options = e.data;
|
var options = e.data;
|
||||||
if (!e.isDefaultPrevented()) { // if event has been canceled, don't proceed
|
if (!e.isDefaultPrevented()) { // if event has been canceled, don't proceed
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
$(this).ajaxSubmit(options);
|
$(e.target).ajaxSubmit(options); // #365
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -865,8 +946,23 @@ $.fn.formToArray = function(semantic, elements) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var form = this[0];
|
var form = this[0];
|
||||||
|
var formId = this.attr('id');
|
||||||
var els = semantic ? form.getElementsByTagName('*') : form.elements;
|
var els = semantic ? form.getElementsByTagName('*') : form.elements;
|
||||||
if (!els) {
|
var els2;
|
||||||
|
|
||||||
|
if (els && !/MSIE 8/.test(navigator.userAgent)) { // #390
|
||||||
|
els = $(els).get(); // convert to standard array
|
||||||
|
}
|
||||||
|
|
||||||
|
// #386; account for inputs outside the form which use the 'form' attribute
|
||||||
|
if ( formId ) {
|
||||||
|
els2 = $(':input[form=' + formId + ']').get();
|
||||||
|
if ( els2.length ) {
|
||||||
|
els = (els || []).concat(els2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!els || !els.length) {
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -889,15 +985,17 @@ $.fn.formToArray = function(semantic, elements) {
|
|||||||
|
|
||||||
v = $.fieldValue(el, true);
|
v = $.fieldValue(el, true);
|
||||||
if (v && v.constructor == Array) {
|
if (v && v.constructor == Array) {
|
||||||
if (elements)
|
if (elements) {
|
||||||
elements.push(el);
|
elements.push(el);
|
||||||
|
}
|
||||||
for(j=0, jmax=v.length; j < jmax; j++) {
|
for(j=0, jmax=v.length; j < jmax; j++) {
|
||||||
a.push({name: n, value: v[j]});
|
a.push({name: n, value: v[j]});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (feature.fileapi && el.type == 'file') {
|
else if (feature.fileapi && el.type == 'file') {
|
||||||
if (elements)
|
if (elements) {
|
||||||
elements.push(el);
|
elements.push(el);
|
||||||
|
}
|
||||||
var files = el.files;
|
var files = el.files;
|
||||||
if (files.length) {
|
if (files.length) {
|
||||||
for (j=0; j < files.length; j++) {
|
for (j=0; j < files.length; j++) {
|
||||||
@@ -910,8 +1008,9 @@ $.fn.formToArray = function(semantic, elements) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (v !== null && typeof v != 'undefined') {
|
else if (v !== null && typeof v != 'undefined') {
|
||||||
if (elements)
|
if (elements) {
|
||||||
elements.push(el);
|
elements.push(el);
|
||||||
|
}
|
||||||
a.push({name: n, value: v, type: el.type, required: el.required});
|
a.push({name: n, value: v, type: el.type, required: el.required});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1007,10 +1106,12 @@ $.fn.fieldValue = function(successful) {
|
|||||||
if (v === null || typeof v == 'undefined' || (v.constructor == Array && !v.length)) {
|
if (v === null || typeof v == 'undefined' || (v.constructor == Array && !v.length)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (v.constructor == Array)
|
if (v.constructor == Array) {
|
||||||
$.merge(val, v);
|
$.merge(val, v);
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
val.push(v);
|
val.push(v);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return val;
|
return val;
|
||||||
};
|
};
|
||||||
@@ -1044,7 +1145,7 @@ $.fieldValue = function(el, successful) {
|
|||||||
if (op.selected) {
|
if (op.selected) {
|
||||||
var v = op.value;
|
var v = op.value;
|
||||||
if (!v) { // extra pain for IE...
|
if (!v) { // extra pain for IE...
|
||||||
v = (op.attributes && op.attributes['value'] && !(op.attributes['value'].specified)) ? op.text : op.value;
|
v = (op.attributes && op.attributes.value && !(op.attributes.value.specified)) ? op.text : op.value;
|
||||||
}
|
}
|
||||||
if (one) {
|
if (one) {
|
||||||
return v;
|
return v;
|
||||||
@@ -1087,21 +1188,22 @@ $.fn.clearFields = $.fn.clearInputs = function(includeHidden) {
|
|||||||
else if (tag == 'select') {
|
else if (tag == 'select') {
|
||||||
this.selectedIndex = -1;
|
this.selectedIndex = -1;
|
||||||
}
|
}
|
||||||
else if (t == "file") {
|
else if (t == "file") {
|
||||||
if (/MSIE/.test(navigator.userAgent)) {
|
if (/MSIE/.test(navigator.userAgent)) {
|
||||||
$(this).replaceWith($(this).clone(true));
|
$(this).replaceWith($(this).clone(true));
|
||||||
} else {
|
} else {
|
||||||
$(this).val('');
|
$(this).val('');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (includeHidden) {
|
else if (includeHidden) {
|
||||||
// includeHidden can be the value true, or it can be a selector string
|
// includeHidden can be the value true, or it can be a selector string
|
||||||
// indicating a special test; for example:
|
// indicating a special test; for example:
|
||||||
// $('#myForm').clearForm('.special:hidden')
|
// $('#myForm').clearForm('.special:hidden')
|
||||||
// the above would clean hidden inputs that have the class of 'special'
|
// the above would clean hidden inputs that have the class of 'special'
|
||||||
if ( (includeHidden === true && /hidden/.test(t)) ||
|
if ( (includeHidden === true && /hidden/.test(t)) ||
|
||||||
(typeof includeHidden == 'string' && $(this).is(includeHidden)) )
|
(typeof includeHidden == 'string' && $(this).is(includeHidden)) ) {
|
||||||
this.value = '';
|
this.value = '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -1160,8 +1262,9 @@ $.fn.ajaxSubmit.debug = false;
|
|||||||
|
|
||||||
// helper fn for console logging
|
// helper fn for console logging
|
||||||
function log() {
|
function log() {
|
||||||
if (!$.fn.ajaxSubmit.debug)
|
if (!$.fn.ajaxSubmit.debug) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
var msg = '[jquery.form] ' + Array.prototype.join.call(arguments,'');
|
var msg = '[jquery.form] ' + Array.prototype.join.call(arguments,'');
|
||||||
if (window.console && window.console.log) {
|
if (window.console && window.console.log) {
|
||||||
window.console.log(msg);
|
window.console.log(msg);
|
||||||
@@ -1171,4 +1274,4 @@ function log() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
})(jQuery);
|
}));
|
||||||
|
|||||||
17
src/posts.js
17
src/posts.js
@@ -397,7 +397,7 @@ var db = require('./database'),
|
|||||||
Posts.uploadPostImage = function(image, callback) {
|
Posts.uploadPostImage = function(image, callback) {
|
||||||
|
|
||||||
if(plugins.hasListeners('filter:uploadImage')) {
|
if(plugins.hasListeners('filter:uploadImage')) {
|
||||||
plugins.fireHook('filter:uploadImage', {base64: image.data, name: image.name}, callback);
|
plugins.fireHook('filter:uploadImage', image, callback);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if (meta.config.allowFileUploads) {
|
if (meta.config.allowFileUploads) {
|
||||||
@@ -411,39 +411,36 @@ var db = require('./database'),
|
|||||||
Posts.uploadPostFile = function(file, callback) {
|
Posts.uploadPostFile = function(file, callback) {
|
||||||
|
|
||||||
if(plugins.hasListeners('filter:uploadFile')) {
|
if(plugins.hasListeners('filter:uploadFile')) {
|
||||||
plugins.fireHook('filter:uploadFile', {base64: file.data, name: file.name}, callback);
|
plugins.fireHook('filter:uploadFile', file, callback);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if(!meta.config.allowFileUploads) {
|
if(!meta.config.allowFileUploads) {
|
||||||
return callback(new Error('File uploads are not allowed'));
|
return callback(new Error('File uploads are not allowed'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!file || !file.data) {
|
if(!file) {
|
||||||
return callback(new Error('invalid file'));
|
return callback(new Error('invalid file'));
|
||||||
}
|
}
|
||||||
|
|
||||||
var buffer = new Buffer(file.data, 'base64');
|
if(file.size > parseInt(meta.config.maximumFileSize, 10) * 1024) {
|
||||||
|
|
||||||
if(buffer.length > parseInt(meta.config.maximumFileSize, 10) * 1024) {
|
|
||||||
return callback(new Error('File too big'));
|
return callback(new Error('File too big'));
|
||||||
}
|
}
|
||||||
|
|
||||||
var filename = 'upload-' + utils.generateUUID() + path.extname(file.name);
|
var filename = 'upload-' + utils.generateUUID() + path.extname(file.name);
|
||||||
var uploadPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), filename);
|
require('./file').saveFileToLocal(filename, file.path, function(err, upload) {
|
||||||
|
|
||||||
fs.writeFile(uploadPath, buffer, function (err) {
|
|
||||||
if(err) {
|
if(err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(null, {
|
callback(null, {
|
||||||
url: nconf.get('upload_url') + filename,
|
url: upload.url,
|
||||||
name: file.name
|
name: file.name
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Posts.reIndexPids = function(pids, callback) {
|
Posts.reIndexPids = function(pids, callback) {
|
||||||
|
|
||||||
function reIndex(pid, next) {
|
function reIndex(pid, next) {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ var nconf = require('nconf'),
|
|||||||
meta = require('../meta'),
|
meta = require('../meta'),
|
||||||
plugins = require('../plugins'),
|
plugins = require('../plugins'),
|
||||||
image = require('./../image'),
|
image = require('./../image'),
|
||||||
|
file = require('./../file'),
|
||||||
Languages = require('../languages'),
|
Languages = require('../languages'),
|
||||||
events = require('./../events'),
|
events = require('./../events'),
|
||||||
utils = require('./../../public/src/utils'),
|
utils = require('./../../public/src/utils'),
|
||||||
@@ -102,8 +103,9 @@ var nconf = require('nconf'),
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.post('/category/uploadpicture', function(req, res) {
|
app.post('/category/uploadpicture', function(req, res) {
|
||||||
if (!req.user)
|
if (!req.user) {
|
||||||
return res.redirect('/403');
|
return res.redirect('/403');
|
||||||
|
}
|
||||||
|
|
||||||
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
|
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
|
||||||
var params = null;
|
var params = null;
|
||||||
@@ -128,8 +130,9 @@ var nconf = require('nconf'),
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.post('/uploadfavicon', function(req, res) {
|
app.post('/uploadfavicon', function(req, res) {
|
||||||
if (!req.user)
|
if (!req.user) {
|
||||||
return res.redirect('/403');
|
return res.redirect('/403');
|
||||||
|
}
|
||||||
|
|
||||||
var allowedTypes = ['image/x-icon', 'image/vnd.microsoft.icon'];
|
var allowedTypes = ['image/x-icon', 'image/vnd.microsoft.icon'];
|
||||||
|
|
||||||
@@ -140,7 +143,9 @@ var nconf = require('nconf'),
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
saveFileToLocal('favicon.ico', req, function(err, image) {
|
file.saveFileToLocal('favicon.ico', req.files.userPhoto.path, function(err, image) {
|
||||||
|
fs.unlink(req.files.userPhoto.path);
|
||||||
|
|
||||||
if(err) {
|
if(err) {
|
||||||
return res.send({
|
return res.send({
|
||||||
error: err.message
|
error: err.message
|
||||||
@@ -155,8 +160,9 @@ var nconf = require('nconf'),
|
|||||||
|
|
||||||
app.post('/uploadlogo', function(req, res) {
|
app.post('/uploadlogo', function(req, res) {
|
||||||
|
|
||||||
if (!req.user)
|
if (!req.user) {
|
||||||
return res.redirect('/403');
|
return res.redirect('/403');
|
||||||
|
}
|
||||||
|
|
||||||
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
|
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
|
||||||
|
|
||||||
@@ -183,6 +189,8 @@ var nconf = require('nconf'),
|
|||||||
|
|
||||||
function uploadImage(filename, req, res) {
|
function uploadImage(filename, req, res) {
|
||||||
function done(err, image) {
|
function done(err, image) {
|
||||||
|
fs.unlink(req.files.userPhoto.path);
|
||||||
|
|
||||||
if(err) {
|
if(err) {
|
||||||
return res.send({
|
return res.send({
|
||||||
error: err.message
|
error: err.message
|
||||||
@@ -195,41 +203,12 @@ var nconf = require('nconf'),
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(plugins.hasListeners('filter:uploadImage')) {
|
if(plugins.hasListeners('filter:uploadImage')) {
|
||||||
plugins.fireHook('filter:uploadImage', {file: req.files.userPhoto.path, name:filename}, done);
|
plugins.fireHook('filter:uploadImage', req.files.userPhoto, done);
|
||||||
} else {
|
} else {
|
||||||
saveFileToLocal(filename, req, done);
|
file.saveFileToLocal(filename, req.files.userPhoto.path, done);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveFileToLocal(filename, req, callback) {
|
|
||||||
|
|
||||||
var tempPath = req.files.userPhoto.path;
|
|
||||||
var extension = path.extname(req.files.userPhoto.name);
|
|
||||||
|
|
||||||
if (!extension) {
|
|
||||||
return callback(new Error('Error uploading file! Error : Invalid extension!'));
|
|
||||||
}
|
|
||||||
|
|
||||||
var uploadPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), filename);
|
|
||||||
|
|
||||||
winston.info('Attempting upload to: ' + uploadPath);
|
|
||||||
var is = fs.createReadStream(tempPath);
|
|
||||||
var os = fs.createWriteStream(uploadPath);
|
|
||||||
|
|
||||||
is.on('end', function () {
|
|
||||||
fs.unlinkSync(tempPath);
|
|
||||||
|
|
||||||
callback(null, {url: nconf.get('upload_url') + filename});
|
|
||||||
});
|
|
||||||
|
|
||||||
os.on('error', function (err) {
|
|
||||||
fs.unlinkSync(tempPath);
|
|
||||||
winston.err(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
is.pipe(os);
|
|
||||||
}
|
|
||||||
|
|
||||||
var custom_routes = {
|
var custom_routes = {
|
||||||
'routes': [],
|
'routes': [],
|
||||||
'api': []
|
'api': []
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
var path = require('path'),
|
var path = require('path'),
|
||||||
nconf = require('nconf'),
|
nconf = require('nconf'),
|
||||||
async = require('async'),
|
async = require('async'),
|
||||||
|
fs = require('fs'),
|
||||||
|
|
||||||
db = require('../database'),
|
db = require('../database'),
|
||||||
user = require('../user'),
|
user = require('../user'),
|
||||||
@@ -448,6 +449,44 @@ var path = require('path'),
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.post('/post/upload', function(req, res, next) {
|
||||||
|
if(!req.user) {
|
||||||
|
return res.json(403, {message:'not allowed'});
|
||||||
|
}
|
||||||
|
var files = req.files.files;
|
||||||
|
|
||||||
|
if(!Array.isArray(files)) {
|
||||||
|
return res.json(500, {message: 'invalid files'});
|
||||||
|
}
|
||||||
|
|
||||||
|
// multiple files
|
||||||
|
if(Array.isArray(files[0])) {
|
||||||
|
files = files[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
function deleteTempFiles() {
|
||||||
|
for(var i=0; i<files.length; ++i) {
|
||||||
|
fs.unlink(files[i].path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async.map(files, function(file, next) {
|
||||||
|
if(file.type.match('image.*')) {
|
||||||
|
posts.uploadPostImage(file, next);
|
||||||
|
} else {
|
||||||
|
posts.uploadPostFile(file, next);
|
||||||
|
}
|
||||||
|
}, function(err, images) {
|
||||||
|
deleteTempFiles();
|
||||||
|
|
||||||
|
if(err) {
|
||||||
|
return res.json(500, {message: err.message});
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json(200, images);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
app.get('/reset', function (req, res) {
|
app.get('/reset', function (req, res) {
|
||||||
res.json({});
|
res.json({});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ var fs = require('fs'),
|
|||||||
meta = require('./../meta'),
|
meta = require('./../meta'),
|
||||||
plugins = require('./../plugins'),
|
plugins = require('./../plugins'),
|
||||||
image = require('./../image'),
|
image = require('./../image'),
|
||||||
|
file = require('./../file'),
|
||||||
db = require('./../database');
|
db = require('./../database');
|
||||||
|
|
||||||
(function (User) {
|
(function (User) {
|
||||||
@@ -134,6 +135,8 @@ var fs = require('fs'),
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
app.post('/uploadpicture', function (req, res) {
|
app.post('/uploadpicture', function (req, res) {
|
||||||
if (!req.user) {
|
if (!req.user) {
|
||||||
return res.json(403, {
|
return res.json(403, {
|
||||||
@@ -174,6 +177,7 @@ var fs = require('fs'),
|
|||||||
}
|
}
|
||||||
], function(err, result) {
|
], function(err, result) {
|
||||||
function done(err, image) {
|
function done(err, image) {
|
||||||
|
fs.unlink(req.files.userPhoto.path);
|
||||||
if(err) {
|
if(err) {
|
||||||
return res.send({error: err.message});
|
return res.send({error: err.message});
|
||||||
}
|
}
|
||||||
@@ -190,12 +194,12 @@ var fs = require('fs'),
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(plugins.hasListeners('filter:uploadImage')) {
|
if(plugins.hasListeners('filter:uploadImage')) {
|
||||||
plugins.fireHook('filter:uploadImage', {file: req.files.userPhoto.path, name: filename}, done);
|
plugins.fireHook('filter:uploadImage', req.files.userPhoto, done);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
user.getUserField(req.user.uid, 'uploadedpicture', function (err, oldpicture) {
|
user.getUserField(req.user.uid, 'uploadedpicture', function (err, oldpicture) {
|
||||||
if (!oldpicture) {
|
if (!oldpicture) {
|
||||||
saveFileToLocal(filename, req.files.userPhoto.path, done);
|
file.saveFileToLocal(filename, req.files.userPhoto.path, done);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,7 +210,7 @@ var fs = require('fs'),
|
|||||||
winston.err(err);
|
winston.err(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
saveFileToLocal(filename, req.files.userPhoto.path, done);
|
file.saveFileToLocal(filename, req.files.userPhoto.path, done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -214,31 +218,6 @@ var fs = require('fs'),
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
function saveFileToLocal(filename, tempPath, callback) {
|
|
||||||
|
|
||||||
var uploadPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), filename);
|
|
||||||
|
|
||||||
winston.info('Saving file '+ filename +' to : ' + uploadPath);
|
|
||||||
|
|
||||||
var is = fs.createReadStream(tempPath);
|
|
||||||
var os = fs.createWriteStream(uploadPath);
|
|
||||||
|
|
||||||
is.on('end', function () {
|
|
||||||
fs.unlinkSync(tempPath);
|
|
||||||
|
|
||||||
callback(null, {url: nconf.get('upload_url') + filename});
|
|
||||||
});
|
|
||||||
|
|
||||||
os.on('error', function (err) {
|
|
||||||
fs.unlinkSync(tempPath);
|
|
||||||
winston.error(err.message);
|
|
||||||
});
|
|
||||||
|
|
||||||
is.pipe(os);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
app.get('/api/user/:userslug/following', function (req, res, next) {
|
app.get('/api/user/:userslug/following', function (req, res, next) {
|
||||||
var callerUID = req.user ? req.user.uid : '0';
|
var callerUID = req.user ? req.user.uid : '0';
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user