def dispatch(self, data, host, port): retCode, result = self.msgManager.resRequest(data) if retCode != 0: self.logger.logger('Error Request') else: responseData = {} if result['request'] == 'scan': self.logger.logger('Request: SCAN') if not os.path.isfile(result['path']): responseData['response'] = 1 else: hashval = ndutil.getMd5(result['path']) reqTime = ndutil.getCreated() fileSize = ndutil.getSize(result['path']) if (fileSize/1024/1024) >= 32: responseData['response'] = 1 else: scanCode, scanResult = self.dbManager.scan(reqTime, result['path'], hashval) if scanCode == 0: responseData['response'] = 0 nums = scanResult.split('/', 1) responseData['hashval'] = hashval responseData['positive'] = nums[0] responseData['total'] = nums[1] if scanCode == 1: responseData['response'] = 2 if scanCode == 2: responseData['response'] = 2 self.fsLock.acquire() self.fsQueue.put('%s,%s' % (hashval, result['path']), 1) self.fsLock.release() msg = self.msgManager.genResponse(responseData) self.transport.write(msg, (host, port))
def dispatch_create_item(self, path): responseData = {} if not os.path.isfile(path): responseData['response'] = 1 else: responseData['response'] = 0 uid = ndutil.getUid(path) if self.dbManager.exists(uid): responseData['uid'] = uid if self.fileMode == 'move': os.remove(path) else: size = ndutil.getSize(path) / 1024 createTime = ndutil.getCreated() fileExt = os.path.basename(path).split('.', 1) dirPath = self.enable_storage_dir(self.storageDir, uid) newPath = '%s/%s.%s' % (dirPath, uid, fileExt[1]) if self.fileMode == 'move': shutil.move(path, newPath) elif self.fileMode == 'copy': shutil.copyfile(path, newPath) path = ndutil.getAbstractPath(newPath) self.dbManager.create_item(uid, path, createTime, size) responseData['uid'] = uid return responseData
def run(self): self.rpcMonitor.setDownloadedTotal(0) self.rpcMonitor.setDuplicatedTotal(0) while True: data, fileName = self.dpQueue.get(1) # filte 0KB file if ndutil.getSize(fileName) == 0: self.pdLock.acquire() self.pdQueue.put(data, 1) self.rpcMonitor.incPdQueueSize() self.pdLock.release() continue # duplicate? md5Value = ndutil.getMd5(fileName) path = self.dbManager.get_path_by_hashval(md5Value) if path is not None: self.dbManager.update_item(data, "NULL", path, md5Value, 1) self.rpcMonitor.incDuplicatedTotal() self.logger.logger("Duplicated %s" % path) os.remove(fileName) continue newFileName = "%s/%s.apk" % (self.dirStore, md5Value) shutil.move(fileName, newFileName) self.dbManager.update_item(data, "NULL", newFileName, md5Value, 1) self.rpcMonitor.incDownloadedTotal() self.logger.logger("Downloaded %s" % data)