Example #1
0
	def do_GET(self):
		if self.path == extbase+'/':
			self.path= extbase+'/demo.html'
		try:	
			#If the path contains a ? then we should parse it and scan and stuff
			if self.path.find('?')!=-1:
				pathlist, values = self.urlParse(self.path)
				if not values.has_key('filename'):
					values['filename']='untitled'+str(int(time()))+'.'+string.lower(values['filetype'])
				self.handleActions(values)

			elif self.path.startswith(extbase+'/viewfile/'):
				path, values=self.urlParse(self.path)
				print "Looking for stored file"
				if filehandler.exists(unquote(path[-1])):
					print 'It seems the stored file exists'
					self.sendHeaders(self.getContentType(unquote(path[-1])))
					self.wfile.write( filehandler.loadFile(unquote(path[-1])).read() )
					print 'File sent'
				else:
					print "stored file not found"
					raise IOError

			elif self.path.startswith(extbase+'/storedfiles/'):
				path, values=self.urlParse(self.path)
				print "Looking for stored file"
				if filehandler.exists(unquote(path[-1])):
					print 'It seems the stored file exists'
					self.sendHeaders('application/octet-stream')
					self.wfile.write( filehandler.loadFile(unquote(path[-1])).read() )
					print 'File sent'
				else:
					print "stored file not found"
					raise IOError

			elif self.path==extbase+'/demo.html':
				self.sendHeaders('text/html; charset="UTF-8"')
				self.wfile.write(xmlhandler.getDocument())
							
			#Snap a preview image and send it directly to the browser
			elif self.path==extbase+'/snap':
				print "Taking snapshot"
				self.sendHeaders('image/png')
				scanhandler.update_preview(self.wfile)

			#Do a scan and return the image directly to the browser
			elif self.path==extbase+'/scan':
				self.sendHeaders('image/png')
				scanhandler.set_resolution(300.0)
				scanhandler.scan_and_save(self.wfile, 'PNG')
				
			#Used for debugging. Displays info about scanner.
			elif self.path==extbase+'/info':
				self.sendHeaders('text/plain')
				scanhandler.write_info(self.wfile)
			
			#We replace preview.png with the preview file.
			elif self.path==extbase+'/preview.png':
				try: 
					f=filehandler.getPreviewFile()
					close=False
				except IOError:
					f=open(basepath+self.path)
					close=True
				self.sendHeaders('image/png')
				self.wfile.write(f.read())
				if close:
					f.close()
			#If nothing special was aksed, just serve the file of the specified name
			else:
				print basepath+self.path
				f=open(basepath+self.path)
				
				if self.getContentType() == None:
					#Let's not serve unknown stuff
					self.sendHeaders('text/plain')
					f.close()
					return
					
				self.sendHeaders( self.getContentType())
				self.wfile.write(f.read())
				f.close()
		#If we cant open the file (=can't find the file) we send a 404
		except IOError:
			self.send_error(404, 'IOError')
Example #2
0
	def handleActions(self,values):
		#SNAP
		if values['action'] == 'snap':
			scanhandler.reset_settings()
			scanhandler.set_mode(values['imgtype'])
			
			scanhandler.set_brightness_and_contrast(
				string.atoi(values['brightness']),
				string.atoi(values['contrast']) )
			
			
			scanhandler.set_preview_rotation(string.atoi(values['rotation']))
			scanhandler.update_preview(filehandler.createPreviewFile())
			filehandler.doneUpdatingPreviewFile()
			self.redirect('demo.html')
		#SCAN
		elif values['action'] == 'scan':
			scanhandler.reset_settings()
			
			scanhandler.set_mode(values['imgtype'])
			
			scanhandler.set_brightness_and_contrast(
				string.atoi(values['brightness']),
				string.atoi(values['contrast']) )
			
			if values['resolution'] == 'OTHER':
				scanhandler.set_resolution(string.atof(values['custom_resolution']))
			else:
				scanhandler.set_resolution(string.atof(values['resolution']))
			
			scanhandler.set_scan_bounds_from_preview(values['left'],values['top'],values['width'],values['height'],values['rotation'])
			
			if values.has_key('before_save'):
				if values['before_save']=='view':
					scanhandler.scan_and_save(filehandler.createFile(values['filename']), values['filetype'])
					xmlhandler.setFiles(filehandler.getFilenames())
					self.redirect('viewfile/'+values['filename'])	
					return			

			scanhandler.scan_and_save(filehandler.createFile(values['filename']), values['filetype'])	
			xmlhandler.setFiles(filehandler.getFilenames())
			self.redirect('demo.html')
		#DELETE_ALL
		elif values['action']=='delete_all':
			filehandler.deleteAllFiles()
			xmlhandler.setFiles(filehandler.getFilenames())
			self.redirect('demo.html')
		#DELETE
		elif values['action']=='delete':
			filehandler.deleteFile(values['selected_file'])
			xmlhandler.setFiles(filehandler.getFilenames())
			self.redirect('demo.html')
		#VIEW
		elif values['action']=='view':
			self.redirect('viewfile/'+values['selected_file'])
		#DOWNLOAD
		elif values['action']=='download':
			self.redirect('storedfiles/'+values['selected_file'])
		#Error, print some debugging info
		else:
			print "No/unknown action value returned:",str(values)
			raise IOError
		
		xmlhandler.updateValues(values)