123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- # -*- 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('<div id="pf', '<div style="width:100%" id="pf')
- #content = content.replace('<div class="pc', '<div style="width:100%" class="pc')
- return content
- def getContent(self, file, limit, uid):
- if limit < 0:
- limit = int(file['page'])
- url = Common.getHost(self);
- style = ['document.css']
- script = ['jquery.js', 'layer.js']
- content = ''
- static = self.static(url)
- assets = self.assets(url, file['path'])
- for i in style:
- content = content + '<link rel="stylesheet" href="'+static+i+'"/>'
- #content = content + '<link rel="stylesheet" href="'+assets+file['key']+'.css"/>'
- #content = content + '<style>' + File.readContent(file['path'], file['key']+'.css') + '</style>'
- for i in script:
- content = content + '<script src="'+static+i+'"></script>'
- content = content + self.style()
- content = content + '<div id="page-container" load-css="'+assets+file['key']+'.css">'
- page_url = self.url(url, file['path'], file['key'], uid)
- for i in range(limit):
- n = str(i+1)
- #content = content + '<div id="pf'+n+'" style="display:none;" class="demeter_document pf w0 h0" data-page-no="'+n+'" data-page-url="'+page_url+n+'.page"></div>'
- content = content + '<div id="demeter_document_'+n+'" style="display:none;" class="demeter_document" data-page-no="'+n+'" data-page-url="'+page_url+n+'.page"></div>'
- content = content + '</div>'
- #content = content.replace('img class', 'img style="width:auto" class')
- #content = content + '<script>try{pdf2htmlEX.defaultViewer = new pdf2htmlEX.Viewer({});}catch(e){}</script>'
- return content
- def getSource(self, file, limit, uid):
- view = file['html'] + '_p' + str(limit) + '.html'
- if File.exists(view):
- content = File.readContent(view)
- File.write(view, content)
- else:
- content = File.readContent(file['html'])
- url = Common.getHost(self);
- static = self.static(url) + 'pdf/'
- content = content.replace('src="', 'src="' + static)
- content = content.replace('href="', 'href="' + self.assets(url, file['path']))
- if limit > 0:
- content = self.pattern(limit,content)
- content = content.replace('data-page-url="', 'data-page-url="' + self.url(url, file['path'], file['key'], uid))
- #content = self.domain(content)
-
- content = content.replace('</head>', self.style() + '<head>')
- return content
- def static(self, url):
- return url + '/static/'
- def assets(self, url, path):
- if 'save' not in Demeter.config['setting']:
- path = path.replace(Demeter.path + 'runtime', '')
- return url + path
- def pattern(self, limit, content):
- import re
- pattern = '<div id="pf([0-9][a-zA-Z]+|[a-zA-Z0]+|['+str(limit+1)+'-9]|([1-9][0-9]+))"(.*?)<\/div>'
- content = re.sub(pattern, '', content)
- return content
- def domain(self, content):
- script = '<script>document.domain="'+self.host+'";</script>'
- content = content.replace('</body>', script + '</body>')
- return content
- def style(self):
- return '<style>body{zoom:100%;}#page-container{position: static;background-color: white;background-image: none;overflow: hidden;}#sidebar{background-color: white;background-image: none;}.pf{border:1px solid #d3d3d3;box-shadow:none;width:98%}</style>'
- def url(self, url, path, key, uid):
- appid = self.input('appid')
- nonce = self.input('nonce')
- timestamp = self.input('timestamp')
- signature = self.input('signature')
- file_id = self.input('file_id')
- if 'save' not in Demeter.config['setting']:
- path = path.replace(Demeter.path + 'runtime', '')
- url = url + '/main/view?path=' + str(path) + '&file=' + key + '&file_id=' + file_id + '×tamp=' + timestamp + '&nonce=' + nonce + '&appid=' + appid + '&signature=' + signature + '&uid=' + uid + '&page='
- return url
- # 下载源文件
- class down_path(Load):
- @Web.setting
- def get(self):
- appid = self.input('appid')
- nonce = self.input('nonce')
- signature = self.input('signature')
- file = self.input('file')
- uid = self.input('uid')
- self.data = {}
- file = Common.signature(self, 2)
- user = self.service.getAuth(self.data['site']['id'], uid, file['id'])
- if not user and file['file_type'] == 1:
- # 没有文件信息,则不允许下载
- self.out('没有权限下载')
- self.set_header ('Content-Type', 'application/octet-stream')
- self.set_header ('Content-Disposition', 'attachment; filename='+file['name']+file['ext'])
- content = File.read(file['local'])
- self.write(content)
|