# -*- coding: utf-8 -*- """ demeter web page name:work.py author:rabin """ from .__load__ import * class Common(object): @staticmethod def signature(self, status): appid = self.input('appid') timestamp = self.input('timestamp') nonce = self.input('nonce') signature = self.input('signature') file = self.input('file') file_id = self.input('file_id') uid = self.input('uid') if not appid: self.out('参数错误:appid') if not timestamp: self.out('参数错误:timestamp') if not nonce: self.out('参数错误:nonce') if not signature: self.out('参数错误:signature') if not file: self.out('参数错误:file') if not file_id: self.out('参数错误:file_id') if not uid: self.out('参数错误:uid') site = Demeter.model('site') site.appid = appid self.data['site'] = site.select(type='fetchone') if not self.data['site']: self.out('站点信息不存在') time = Demeter.time() if time < self.data['site']['sdate'] or time > self.data['site']['edate']: self.out('授权已失效') self.service = Demeter.service('convert') self.referer = self.request.headers.get("Referer") self.host = Demeter.host(self.data['site']['link']) ''' uri = Common.getHost(self) + self.request.uri if not self.referer: self.out('验证失败') if self.host != Demeter.host(self.referer): self.out('验证失败') if self.referer == uri: self.out('验证失败') ''' self.param = self.service.signature(self.data['site']['id'], appid, self.data['site']['appsecret'], timestamp, nonce, file, file_id, uid, status) if not self.param: self.out('验证失败') if self.param['signature'] != signature: self.out('验证失败') if status == 1: file_type = self.input('file_type') file = self.service.update(self.data['site']['id'], self.data['site']['appid'], file, file_id, file_type, uid) else: file = self.service.get(self.data['site']['id'], self.data['site']['appid'], file); if not file: self.out('未生成文件信息') return file @staticmethod def getHost(self): host = self.request.host.replace(':8088', '') host = self.request.protocol + "://" + host return host class test_path(Load): @Web.setting def get(self): from wand.image import Image filename = Demeter.path + 'runtime/upload/d1c6fd2a4b2941e083471c2ae3d97a37.pdf' i = 0 pdf = Image(filename=filename, resolution=50) jpg = pdf.convert('jpg') req_image = [] for img in jpg.sequence: if i == 0: img_page = Image(image=img) req_image.append(img_page.make_blob('jpg')) i = i+1 for img in req_image: ff = open(filename + '.jpg','wb') ff.write(img) ff.close() self.out('yes', Demeter.config) return import csv,codecs from datetime import * file_name = 'test.csv' day = str(date.today()) day = day.split('-') file_path = day[0] + '/' + day[1] + '/' + day[2] file_path = File.mkdirs(os.path.join(Demeter.path, 'runtime','upload', file_path)) + '/' + file_name data = [{'data': {'rows': [['2017-11-01 00:02:27', 10.95], ['2017-11-01 00:07:27', 9.45], ['2017-11-01 00:12:27', 9.4], ['2017-11-01 00:17:27', 9.45], ['2017-11-01 00:22:27', 9.45], ['2017-11-01 00:27:27', 9.35], ['2017-11-01 00:32:27', 9.35], ['2017-11-01 00:37:27', 9.25], ['2017-11-01 00:42:27', 9.3], ['2017-11-01 00:47:27', 10.55], ['2017-11-01 00:52:27', 9.55], ['2017-11-01 00:57:31', 9.25]], 'value': '[10.95, 9.45, 9.4, 9.45, 9.45, 9.35, 9.35, 9.25, 9.3, 10.55, 9.55, 9.25]', 'time': '["2017-11-01 00:02:27", "2017-11-01 00:07:27", "2017-11-01 00:12:27", "2017-11-01 00:17:27", "2017-11-01 00:22:27", "2017-11-01 00:27:27", "2017-11-01 00:32:27", "2017-11-01 00:37:27", "2017-11-01 00:42:27", "2017-11-01 00:47:27", "2017-11-01 00:52:27", "2017-11-01 00:57:31"]'}, 'name': '\xe6\x98\x8e\xe7\xbb\x86'}] data1 = [{'data': {'rows': [['2017-11-01 00:07:27', 3.45], ['2017-11-01 00:12:27', 4.4], ['2017-11-01 00:17:27', 6.45], ['2017-11-01 00:22:27', 5.45], ['2017-11-01 00:27:27', 5.35], ['2017-11-01 00:32:27', 3.35], ['2017-11-01 00:37:27', 3.25], ['2017-11-01 00:42:27', 4.3], ['2017-11-01 00:47:27', 3.55], ['2017-11-01 00:52:27', 4.55], ['2017-11-01 00:57:31', 5.25]], 'value': '[10.95, 9.45, 9.4, 9.45, 9.45, 9.35, 9.35, 9.25, 9.3, 10.55, 9.55, 9.25]', 'time': '["2017-11-01 00:02:27", "2017-11-01 00:07:27", "2017-11-01 00:12:27", "2017-11-01 00:17:27", "2017-11-01 00:22:27", "2017-11-01 00:27:27", "2017-11-01 00:32:27", "2017-11-01 00:37:27", "2017-11-01 00:42:27", "2017-11-01 00:47:27", "2017-11-01 00:52:27", "2017-11-01 00:57:31"]'}, 'name': '\xe6\x98\x8e\xe7\xbb\x86'}] datad = {} for t,v in data[0]['data']['rows']: if t not in datad: datad[t] = [t,v] else: datad[t].append(v) newdata = sorted(datad.keys()) newds = [] for i in newdata: newds.append(datad[i]) datad = newds header = ['日期', '温度', '湿度'] datad.insert(0, header) date = '日期' datas = [[date, 'dfdf'], ['Bob', 14], ['Tom', 23], ['Jerry', '18']] #self.set_header("Content-Type","text/csv,charset=UTF-8") #self.set_header('Content-Disposition', 'attachment; filename=' + file_name) with open(file_path, 'w') as f: f.write(codecs.BOM_UTF8) writer = csv.writer(f) for row in datad: writer.writerow(row) url = Common.getHost(self); file_path = url + file_path.replace(Demeter.path + 'runtime', '') self.redirect(file_path) #self.out('yes', datad) # 请求转换 /main/convert 接口必须后端获取,token不允许暴露 #http://192.168.33.10:8088/main/convert?signature=44e3cd684a9fe697792a235c8c57838211f5823a&appid=mo1209&nonce=1529659172&file=http%3A%2F%2F192.168.33.10%2Fsystem%2Fdata%2Fupload%2Fdoc_con%2F5b2cbf1064fd8.doc&uid=1 class convert_path(Load): @Web.setting def get(self): file = self.input('file') # 源文件id file_id = self.input('file_id') # 源文件类型 1为收费 2为不收费 file_type = self.input('file_type', 1) # 当前的更新类型,update=insert则为新入库 update=update则为更新,区别是insert要入队 update = self.input('update', 'insert') # 文件流 后续再增加这个吧,要把这个文件流先生成一个本地文件,暂时不做 #filedata = self.input('filedata') uid = self.input('uid') self.data = {} file = Common.signature(self, 1) # 推入到redis队列 if (file['status'] == 1 or file['status'] == 4) and update == 'insert': redis = Demeter.redis() config = Demeter.config['redis'] redis.rpush(config['name'], file['id']) url = Common.getHost(self); self.data['file'] = { 'status' : file['status'], 'url' : url + file['url'], 'ext' : file['ext'], 'name' : file['name'], 'file' : file['key'] } self.out('yes', self.data['file']) # 获取信息 后端接口,token不允许暴露 class get_path(Load): @Web.setting def get(self): file = self.input('file') self.data = {} file = Common.signature(self, 2) url = Common.getHost(self); self.data['file'] = { #'id' : file['id'], 'status' : file['status'], 'url' : url + file['url'], 'img' : url + file['url'] + '.jpg', 'page' : file['page'], 'ext' : file['ext'], 'name' : file['name'], 'key' : file['key'], 'size' : file['size'] } self.out('yes', self.data['file']) # 验证用户是否有效 class check_path(Load): @Web.setting def get(self): appid = self.input('appid') appsecret = self.input('appsecret') if not appid: self.out('参数错误:appid') if not appsecret: self.out('参数错误:appsecret') site = Demeter.model('site') site.appid = appid site.appsecret = appsecret self.data = {} self.data['site'] = site.select(type='fetchone') if not self.data['site']: self.out('站点信息不存在') time = Demeter.time() if time < self.data['site']['sdate'] or time > self.data['site']['edate']: self.out('授权已失效') self.out('yes', appid) # 授权用户可以访问html的接口 后端接口,token不允许暴露 class auth_path(Load): @Web.setting def get(self): file = self.input('file') uid = self.input('uid') self.data = {} file = Common.signature(self, 2) if file: self.service.auth(self.data['site']['id'], uid, file['id'], 2) self.out('yes', {'msg':1}) # 读取html 带有权限控制 该接口为前端接口 需要有授权接口 后续实现 class view_path(Load): @Web.setting def get(self): uid = self.input('uid') page = self.input('page') path = self.input('path') callback = self.input('callback') source = self.input('source') self.data = {} file = Common.signature(self, 11) # 读取源文件 user = self.service.getAuth(self.data['site']['id'], uid, file['id']) limit = -1 if not user and file['file_type'] == 1: # 没有文件信息,则有限制 limit = self.data['site']['page'] if page: content = self.getPage(path, page, limit) elif source: content = self.getSource(file, limit, uid) else: content = self.getContent(file, limit, uid) if callback: self.out(content) else: self.write(content) def getPage(self, path, page, limit): num = page.replace('.page', '') if limit > 0 and int(num) > limit: self.out('您没有权限查看') url = Common.getHost(self); static = url + path if 'save' in Demeter.config['setting']: page = path + page else: page = Demeter.path + 'runtime' + path + page content = File.readContent(page) content = content.replace('src="', 'src="' + str(static)) #content = content.replace('img class', 'img style="width:auto" class') #content = content.replace('