def runEyeFi(configfile): # configfile = sys.argv[2] eyeFiLogger.info("Reading config " + configfile) config = ConfigParser.SafeConfigParser({'geotag_enable': '0'}) config.read(configfile) server_address = (config.get('EyeFiServer', 'host_name'), config.getint('EyeFiServer', 'host_port')) # Create an instance of an HTTP server. Requests will be handled # by the class EyeFiRequestHandler eyeFiServer = EyeFiServer(server_address, EyeFiRequestHandler) eyeFiServer.config = config eyeFiLogger.info("Eye-Fi server started listening on port " + str(server_address[1])) send_event(create_service_status_event(SERVICE_NAME_EYEFISERVER2, SERVICE_STATUS_UP)) eyeFiServer.serve_forever()
def uploadPhoto(self, postData): # Take the postData string and work with it as if it were a file object postDataInMemoryFile = StringIO.StringIO(postData) # Get the content-type header which looks something like this # content-type: multipart/form-data; boundary=---------------------------02468ace13579bdfcafebabef00d contentTypeHeader = self.headers.getheaders('content-type').pop() eyeFiLogger.debug(contentTypeHeader) # Extract the boundary parameter in the content-type header headerParameters = contentTypeHeader.split(";") eyeFiLogger.debug(headerParameters) boundary = headerParameters[1].split("=") boundary = boundary[1].strip() eyeFiLogger.debug("Extracted boundary: " + boundary) # eyeFiLogger.debug("uploadPhoto postData: " + postData) # Parse the multipart/form-data form = cgi.parse_multipart(postDataInMemoryFile, {"boundary": boundary, "content-disposition": self.headers.getheaders('content-disposition')}) eyeFiLogger.debug("Available multipart/form-data: " + str(form.keys())) # Parse the SOAPENVELOPE using the EyeFiContentHandler() soapEnvelope = form['SOAPENVELOPE'][0] eyeFiLogger.debug("SOAPENVELOPE: " + soapEnvelope) handler = EyeFiContentHandler() parser = xml.sax.parseString(soapEnvelope, handler) eyeFiLogger.debug("Extracted elements: " + str(handler.extractedElements)) imageTarfileName = handler.extractedElements["filename"] #pike uid = self.server.config.getint('EyeFiServer', 'upload_uid') gid = self.server.config.getint('EyeFiServer', 'upload_gid') file_mode = self.server.config.get('EyeFiServer', 'upload_file_mode') dir_mode = self.server.config.get('EyeFiServer', 'upload_dir_mode') eyeFiLogger.debug("Using uid/gid %d/%d" % (uid, gid)) eyeFiLogger.debug("Using file_mode " + file_mode) eyeFiLogger.debug("Using dir_mode " + dir_mode) geotag_enable = int(self.server.config.getint('EyeFiServer', 'geotag_enable')) if geotag_enable: geotag_accuracy = int(self.server.config.get('EyeFiServer', 'geotag_accuracy')) tempDir = os.path.normpath(self.server.config.get('EyeFiServer', 'upload_dir')) imageTarPath = os.path.join(tempDir, imageTarfileName) eyeFiLogger.debug("Generated path " + imageTarPath) fileHandle = open(imageTarPath, 'wb') eyeFiLogger.debug("Opened file " + imageTarPath + " for binary writing") fileHandle.write(form['FILENAME'][0]) eyeFiLogger.debug("Wrote file " + imageTarPath) fileHandle.close() eyeFiLogger.debug("Closed file " + imageTarPath) eyeFiLogger.debug("Extracting TAR file " + imageTarPath) imageTarfile = tarfile.open(imageTarPath) for member in imageTarfile.getmembers(): # If timezone is a daylight savings timezone, and we are # currently in daylight savings time, then use the altzone if time.daylight != 0 and time.localtime().tm_isdst != 0: timeoffset = time.altzone else: timeoffset = time.timezone timezone = timeoffset / 60 / 60 * -1 imageDate = datetime.fromtimestamp(member.mtime) - timedelta(hours=timezone) uploadDir = imageDate.strftime(self.server.config.get('EyeFiServer', 'upload_dir')) eyeFiLogger.debug("Creating folder " + uploadDir) if not os.path.isdir(uploadDir): os.makedirs(uploadDir) if uid != 0 and gid != 0: os.chown(uploadDir, uid, gid) if file_mode != "": os.chmod(uploadDir, int(dir_mode)) f = imageTarfile.extract(member, uploadDir) imagePath = os.path.join(uploadDir, member.name) send_event(create_upload_event('cli', imagePath)) eyeFiLogger.debug("imagePath " + imagePath) os.utime(imagePath, (member.mtime + timeoffset, member.mtime + timeoffset)) if uid != 0 and gid != 0: os.chown(imagePath, uid, gid) if file_mode != "": os.chmod(imagePath, int(file_mode)) if geotag_enable > 0 and member.name.lower().endswith(".log"): eyeFiLogger.debug("Processing LOG file " + imagePath) try: imageName = member.name[:-4] shottime, aps = list(self.parselog(imagePath, imageName)) aps = self.getphotoaps(shottime, aps) loc = self.getlocation(aps) if loc['status'] == 'OK' and float(loc['accuracy']) <= geotag_accuracy: xmpName = imageName + ".xmp" xmpPath = os.path.join(uploadDir, xmpName) eyeFiLogger.debug("Writing XMP file " + xmpPath) self.writexmp(xmpPath, float(loc['location']['lat']), float(loc['location']['lng'])) if uid != 0 and gid != 0: os.chown(xmpPath, uid, gid) if file_mode != "": os.chmod(xmpPath, int(file_mode)) except: eyeFiLogger.exception("Error processing LOG file " + imagePath) eyeFiLogger.debug("Closing TAR file " + imageTarPath) imageTarfile.close() eyeFiLogger.debug("Deleting TAR file " + imageTarPath) os.remove(imageTarPath) # Create the XML document to send back doc = xml.dom.minidom.Document() SOAPElement = doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "SOAP-ENV:Envelope") SOAPElement.setAttribute("xmlns:SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/") SOAPBodyElement = doc.createElement("SOAP-ENV:Body") uploadPhotoResponseElement = doc.createElement("UploadPhotoResponse") successElement = doc.createElement("success") successElementText = doc.createTextNode("true") successElement.appendChild(successElementText) uploadPhotoResponseElement.appendChild(successElement) SOAPBodyElement.appendChild(uploadPhotoResponseElement) SOAPElement.appendChild(SOAPBodyElement) doc.appendChild(SOAPElement) return doc.toxml(encoding="UTF-8")
def main(): print "Sending event for file {0}".format(sys.argv[1]) assert os.path.exists(sys.argv[1]) assert os.path.isfile(sys.argv[1]) send_event(create_upload_event('cli', sys.argv[1]))