Put in client side check for document getting too long

This commit is contained in:
James Allen 2015-11-06 12:51:43 +00:00
parent 9dcc251017
commit a153c6682a
8 changed files with 33 additions and 14 deletions

View file

@ -243,7 +243,8 @@ module.exports = ProjectController =
anonymous: anonymous
languages: Settings.languages
themes: THEME_LIST
timer.done()
maxDocLength: Settings.max_doc_length
timer.done()
_buildProjectList: (ownedProjects, sharedProjects, readOnlyProjects)->
projects = []

View file

@ -58,7 +58,8 @@ Modules.loadViewIncludes app
app.use bodyParser.urlencoded({ extended: true, limit: "2mb"})
app.use bodyParser.json({limit: "2mb"})
# Make sure we can process the max doc length plus some overhead for JSON encoding
app.use bodyParser.json({limit: Settings.max_doc_length + 16 * 1024}) # 16kb overhead
app.use multer(dest: Settings.path.uploadFolder)
app.use methodOverride()

View file

@ -90,6 +90,7 @@ block content
window.user = !{JSON.stringify(user).replace(/\//g, '\\/')};
window.csrfToken = "!{csrfToken}";
window.anonymous = #{anonymous};
window.maxDocLength = #{maxDocLength};
window.requirejs = {
"paths" : {
"mathjax": "/js/libs/mathjax/MathJax.js?config=TeX-AMS_HTML",

View file

@ -249,7 +249,10 @@ module.exports =
# Should we allow access to any page without logging in? This includes
# public projects, /learn, /templates, about pages, etc.
allowPublicAccess: false
# Maximum size of text documents in the real-time editing system.
max_doc_length: 2 * 1024 * 1024 # 2mb
# Internal configs
# ----------------
path:

View file

@ -250,11 +250,9 @@ define [
v: version
_onError: (error, meta = {}) ->
meta.doc_id = @doc_id
console.error "ShareJS error", error, meta
ga?('send', 'event', 'error', "shareJsError", "#{error.message} - #{@ide.socket.socket.transport.name}" )
@ide.socket.disconnect()
meta.doc_id = @doc_id
@ide.reportError(error, meta)
@doc?.clearInflightAndPendingOps()
@_cleanUp()
@trigger "error", error
@trigger "error", error, meta

View file

@ -87,12 +87,20 @@ define [
callback null, new_sharejs_doc
_bindToDocumentEvents: (doc, sharejs_doc) ->
sharejs_doc.on "error", (error) =>
sharejs_doc.on "error", (error, meta) =>
if error?.message?.match "maxDocLength"
@ide.showGenericMessageModal(
"Document Too Long"
"Sorry, this file is too long to be edited manually. Please upload it directly."
)
else
@ide.socket.disconnect()
@ide.reportError(error, meta)
@ide.showGenericMessageModal(
"Out of sync"
"Sorry, this file has gone out of sync and we need to do a full refresh. Please let us know if this happens frequently."
)
@openDoc(doc, forceReopen: true)
@ide.showGenericMessageModal(
"Out of sync"
"Sorry, this file has gone out of sync and we need to do a full refresh. Please let us know if this happens frequently."
)
sharejs_doc.on "externalUpdate", (update) =>
return if @_ignoreExternalUpdates

View file

@ -45,6 +45,8 @@ define [
# ops as quickly as possible for low latency.
@_doc.setFlushDelay(0)
@trigger "remoteop"
@_doc.on "error", (e) =>
@_handleError(e)
@_bindToDocChanges(@_doc)
@ -98,7 +100,7 @@ define [
getInflightOp: () -> @_doc.inflightOp
getPendingOp: () -> @_doc.pendingOp
attachToAce: (ace) -> @_doc.attach_ace(ace)
attachToAce: (ace) -> @_doc.attach_ace(ace, false, window.maxDocLength)
detachFromAce: () -> @_doc.detach_ace?()
INFLIGHT_OP_TIMEOUT: 10000

View file

@ -40,7 +40,7 @@ applyToShareJS = (editorDoc, delta, doc) ->
# Attach an ace editor to the document. The editor's contents are replaced
# with the document's contents unless keepEditorContents is true. (In which case the document's
# contents are nuked and replaced with the editor's).
window.sharejs.extendDoc 'attach_ace', (editor, keepEditorContents) ->
window.sharejs.extendDoc 'attach_ace', (editor, keepEditorContents, maxDocLength) ->
throw new Error 'Only text documents can be attached to ace' unless @provides['text']
doc = this
@ -74,6 +74,11 @@ window.sharejs.extendDoc 'attach_ace', (editor, keepEditorContents) ->
# Listen for edits in ace
editorListener = (change) ->
return if suppress
if maxDocLength? and editorDoc.getValue().length > maxDocLength
doc.emit "error", new Error("document length is greater than maxDocLength")
return
applyToShareJS editorDoc, change, doc
check()