Просмотр исходного кода

Put in client side check for document getting too long

James Allen 11 лет назад
Родитель
Сommit
a153c6682a

+ 2 - 1
services/web/app/coffee/Features/Project/ProjectController.coffee

@@ -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 = []

+ 2 - 1
services/web/app/coffee/infrastructure/Server.coffee

@@ -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()
 

+ 1 - 0
services/web/app/views/project/editor.jade

@@ -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",

+ 4 - 1
services/web/config/settings.defaults.coffee

@@ -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:

+ 2 - 4
services/web/public/coffee/ide/editor/Document.coffee

@@ -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

+ 13 - 5
services/web/public/coffee/ide/editor/EditorManager.coffee

@@ -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

+ 3 - 1
services/web/public/coffee/ide/editor/ShareJsDoc.coffee

@@ -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

+ 6 - 1
services/web/public/coffee/ide/editor/sharejs/vendor/client/ace.coffee

@@ -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()