diff options
Diffstat (limited to '')
-rw-r--r-- | etherpad/src/plugins/fileUpload/controllers/fileUpload.js | 87 | ||||
-rw-r--r-- | etherpad/src/plugins/fileUpload/hooks.js | 11 | ||||
-rw-r--r-- | etherpad/src/plugins/fileUpload/main.js | 19 | ||||
-rw-r--r-- | etherpad/src/plugins/fileUpload/models.js | 95 | ||||
-rw-r--r-- | etherpad/src/plugins/fileUpload/templates/fileUpload.ejs | 32 | ||||
-rw-r--r-- | etherpad/src/plugins/fileUpload/templates/fileUploaded.ejs | 5 |
6 files changed, 249 insertions, 0 deletions
diff --git a/etherpad/src/plugins/fileUpload/controllers/fileUpload.js b/etherpad/src/plugins/fileUpload/controllers/fileUpload.js new file mode 100644 index 0000000..d6585f1 --- /dev/null +++ b/etherpad/src/plugins/fileUpload/controllers/fileUpload.js @@ -0,0 +1,87 @@ +/** + * Copyright 2009 RedHog, Egil Möller <egil.moller@piratpartiet.se> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import("faststatic"); +import("dispatch.{Dispatcher,PrefixMatcher,forward}"); + +import("etherpad.utils.*"); +import("etherpad.collab.server_utils"); +import("etherpad.globals.*"); +import("etherpad.log"); +import("etherpad.pad.padusers"); +import("etherpad.pro.pro_utils"); +import("etherpad.helpers"); +import("etherpad.pro.pro_accounts.getSessionProAccount"); +import("sqlbase.sqlbase"); +import("sqlbase.sqlcommon"); +import("sqlbase.sqlobj"); +import("plugins.fileUpload.models"); +jimport("org.apache.commons.fileupload"); + +function onRequest() { + var isPro = pro_utils.isProDomainRequest(); + var userId = padusers.getUserId(); + + + helpers.addClientVars({ + userAgent: request.headers["User-Agent"], + debugEnabled: request.params.djs, + clientIp: request.clientAddr, + colorPalette: COLOR_PALETTE, + serverTimestamp: +(new Date), + isProPad: isPro, + userIsGuest: padusers.isGuest(userId), + userId: userId, + }); + + var isProUser = (isPro && ! padusers.isGuest(userId)); + + if (request.isPost) { + var uploads = []; + var itemFactory = new fileupload.disk.DiskFileItemFactory(); + var handler = new fileupload.servlet.ServletFileUpload(itemFactory); + var items = handler.parseRequest(request.underlying).toArray(); + for (var i = 0; i < items.length; i++){ + if (!items[i].isFormField()) + uploads.push('/up/' + models.storeFile(items[i])); + } + + response.setContentType("text/json; charset=utf-8"); + response.write( + renderTemplateAsString( + "fileUploaded.ejs", + { + uploads: uploads, + isPro: isPro, + isProAccountHolder: isProUser, + account: getSessionProAccount(), // may be falsy + }, + 'fileUpload')); + if (request.acceptsGzip) { + response.setGzip(true); + } + } else { + renderHtml( + "fileUpload.ejs", + { + isPro: isPro, + isProAccountHolder: isProUser, + account: getSessionProAccount(), // may be falsy + }, + 'fileUpload'); + } + return true; +} diff --git a/etherpad/src/plugins/fileUpload/hooks.js b/etherpad/src/plugins/fileUpload/hooks.js new file mode 100644 index 0000000..0948c17 --- /dev/null +++ b/etherpad/src/plugins/fileUpload/hooks.js @@ -0,0 +1,11 @@ +import("etherpad.log"); +import("faststatic"); +import("etherpad.utils.*"); +import("etherpad.globals.*"); +import("dispatch.{Dispatcher,PrefixMatcher,forward}"); +import("plugins.fileUpload.controllers.fileUpload"); + +function handlePath() { + return [[PrefixMatcher('/ep/fileUpload/'), forward(fileUpload)], + [PrefixMatcher('/up/'), faststatic.directoryServer('/plugins/fileUpload/upload/', {cache: isProduction()})]]; +} diff --git a/etherpad/src/plugins/fileUpload/main.js b/etherpad/src/plugins/fileUpload/main.js new file mode 100644 index 0000000..5ff105f --- /dev/null +++ b/etherpad/src/plugins/fileUpload/main.js @@ -0,0 +1,19 @@ +import("etherpad.log"); +import("plugins.fileUpload.hooks"); + +function init() { + this.hooks = ['handlePath']; + this.description = 'File upload manager'; + this.handlePath = hooks.handlePath; + this.install = install; + this.uninstall = uninstall; +} + +function install() { + log.info("Installing fileUpload"); +} + +function uninstall() { + log.info("Uninstalling fileUpload"); +} + diff --git a/etherpad/src/plugins/fileUpload/models.js b/etherpad/src/plugins/fileUpload/models.js new file mode 100644 index 0000000..f8fb563 --- /dev/null +++ b/etherpad/src/plugins/fileUpload/models.js @@ -0,0 +1,95 @@ +/** + * Copyright 2009 RedHog, Egil Möller <egil.moller@piratpartiet.se> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import("etherpad.utils.*"); +import("etherpad.globals.*"); +import("etherpad.log"); +import("sqlbase.sqlbase"); +import("sqlbase.sqlcommon"); +import("sqlbase.sqlobj"); + +jimport("java.io.File", + "java.io.DataInputStream", + "java.io.FileInputStream", + "java.lang.Byte", + "java.io.FileReader", + "java.io.BufferedReader", + "java.security.MessageDigest", + "java.lang.Runtime"); + + +/* Normal base64 encoding, except we don't care about adding newlines */ +function base64Encode(stringArray) { + base64code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "+/"; + + /* Pad array to nearest three byte multiple */ + var padding = (3 - (stringArray.length % 3)) % 3; + var padded = java.lang.reflect.Array.newInstance(Byte.TYPE, stringArray.length + padding); + java.lang.System.arraycopy(stringArray, 0, padded, 0, stringArray.length); + stringArray = padded; + + var encoded = ""; + for (var i = 0; i < stringArray.length; i += 3) { + var j = (((stringArray[i] & 0xff) << 16) + + ((stringArray[i + 1] & 0xff) << 8) + + (stringArray[i + 2] & 0xff)); + encoded = (encoded + + base64code.charAt((j >> 18) & 0x3f) + + base64code.charAt((j >> 12) & 0x3f) + + base64code.charAt((j >> 6) & 0x3f) + + base64code.charAt(j & 0x3f)); + } + /* replace padding with "=" */ + return encoded.substring(0, encoded.length - padding) + "==".substring(0, padding); +} + + +function makeSymlink(destination, source) { + return Runtime.getRuntime().exec(['ln', '-s', source.getPath(), destination.getPath()]).waitFor(); +} + + +/* Reads a File and updates a digest with its content */ +function updateDigestFromFile(digest, file) { + var handle = new java.io.FileInputStream(file); + + var bytes = java.lang.reflect.Array.newInstance(Byte.TYPE, 512); + var nbytes = 0; + + while ((nbytes = handle.read(bytes, 0, 512)) != -1) + digest.update(bytes, 0, nbytes); + + handle.close(); +} + + +/* Stores a org.apache.commons.fileupload.disk.DiskFileItem permanently and returns a filename. */ +function storeFile(fileItem) { + var nameParts = fileItem.name.split('.'); + var extension = nameParts[nameParts.length-1]; + + var digest = MessageDigest.getInstance("SHA1"); + updateDigestFromFile(digest, fileItem.getStoreLocation()); + var checksum = base64Encode(digest.digest()); + + fileItem.write(File("src/plugins/fileUpload/upload/" + checksum)); + + makeSymlink( + File("src/plugins/fileUpload/upload/" + checksum + '.' + extension), + File(checksum)); + + return checksum + '.' + extension; +} diff --git a/etherpad/src/plugins/fileUpload/templates/fileUpload.ejs b/etherpad/src/plugins/fileUpload/templates/fileUpload.ejs new file mode 100644 index 0000000..57f33e6 --- /dev/null +++ b/etherpad/src/plugins/fileUpload/templates/fileUpload.ejs @@ -0,0 +1,32 @@ +<% /* Copyright 2009 Google Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS-IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ %> +<% + helpers.setHtmlTitle("Test plugin"); + helpers.setBodyId("padbody"); + helpers.addBodyClass("limwidth nonpropad nonprouser"); + helpers.includeCss("pad2_ejs.css"); + helpers.setRobotsPolicy({index: false, follow: false}) + helpers.includeJQuery(); + helpers.includeCometJs(); + helpers.includeJs("json2.js"); + helpers.addToHead('\n<style type="text/css" title="dynamicsyntax"></style>\n'); +%> + +<div id="padpage"> + <form method="POST" enctype="multipart/form-data"> + <input type="file" name="fileParam"> + <button type="submit">Submit</button> + </form> + +</div> diff --git a/etherpad/src/plugins/fileUpload/templates/fileUploaded.ejs b/etherpad/src/plugins/fileUpload/templates/fileUploaded.ejs new file mode 100644 index 0000000..5a62f7e --- /dev/null +++ b/etherpad/src/plugins/fileUpload/templates/fileUploaded.ejs @@ -0,0 +1,5 @@ +[ +<% for (var i = 0; i < uploads.length; i++) { %> + '<%= uploads[i] %>', +<% } %> +] |