Example #1
0
	def doPost(self, request, response):

		#
		# HACK: this is necessary because the flash player upload sends TWO REQUESTS
		# the first one is completely empty!!!
		# The 2nd one is good!
		#
			
		if request.getContentLength() > 0:
			page = response.getWriter()
			senderId = request.getParameter("id")
			senderNick = request.getParameter("nick")
			
			# debug only
			# print "id: %s, nick: %s, ip: %s" % (senderId, senderNick, request.getRemoteAddr())
			
			# Verify user
			isValidUser = WebHelper.verifyUploadUser(senderId, senderNick, request.getRemoteAddr())
			
			if isValidUser:
				# pass the request object and the target folder
				# if the size exceeds the passed MAX SIZE
				# the upfile will be completely ignored
				parser = ServletMultipartRequest(request, 
												self.tempUploadDir, 
												self.MAX_FILE_SIZE, 
												MultipartRequest.IGNORE_FILES_IF_MAX_BYES_EXCEEDED, 
												None)
				
				# get all file-upload fields in the request
				files = parser.getFileParameterNames()
				
				# Random generator
				rnd = Random()
				
				# List of files successfully transferred
				transFiles = HashMap()
				
				for file in files:
					fileRef = parser.getFile(file)
					
					# Get original file name
					fileOriginalName = parser.getBaseFilename(file)
					
					# Extract extension
					fileExt = fileOriginalName[fileOriginalName.rfind("."):]
					
					# Create the unique filename keeping the original extension
					# Also adds a random at the end to avoid name collisions
					fileName = "file_" + str(System.currentTimeMillis()) + "_" + str(rnd.randint(0, 1000)) + fileExt # before:  parser.getBaseFilename(file)
					
					# Info data, needed for debug only
					contentType = parser.getContentType(file)
					fileSize = parser.getFileSize(file)
					
					# Let's copy the file
					if not fileRef == None:
						#print "File: %s, type: %s, size: %s, senderId: %s" % (fileName, contentType, fileSize, senderId)
						self.logger.fine(("File upload: %s, type: %s, size: %s, senderId: %s" % (fileName, contentType, fileSize, senderId)))
						wasMoved = self.moveFile(fileRef.getName(), fileName)
						
						if wasMoved == False:
							self.logger.warning(("Failed moving uploaded file: %s, userId: %s " % (fileName, senderId)))
						else:
							transFiles.put(fileName, fileOriginalName)

				# Fire event
				sfs = SmartFoxServer.getInstance()
				sfs.getSysHandler().broadcastFileUploaded(senderId, transFiles)
			
			else:
				SmartFoxServer.log.warning("Invalid user upload from: " + request.getRemoteHost())
Example #2
0
	def __init__(self):
		self.sfsVersion = SmartFoxServer.getInstance().getVersion()