main.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. # -*- coding: utf-8 -*-
  2. """
  3. demeter web page
  4. name:work.py
  5. author:rabin
  6. """
  7. from .__load__ import *
  8. class Common(object):
  9. @staticmethod
  10. def signature(self, status):
  11. appid = self.input('appid')
  12. timestamp = self.input('timestamp')
  13. nonce = self.input('nonce')
  14. signature = self.input('signature')
  15. file = self.input('file')
  16. file_id = self.input('file_id')
  17. uid = self.input('uid')
  18. if not appid:
  19. self.out('参数错误:appid')
  20. if not timestamp:
  21. self.out('参数错误:timestamp')
  22. if not nonce:
  23. self.out('参数错误:nonce')
  24. if not signature:
  25. self.out('参数错误:signature')
  26. if not file:
  27. self.out('参数错误:file')
  28. if not file_id:
  29. self.out('参数错误:file_id')
  30. if not uid:
  31. self.out('参数错误:uid')
  32. site = Demeter.model('site')
  33. site.appid = appid
  34. self.data['site'] = site.select(type='fetchone')
  35. if not self.data['site']:
  36. self.out('站点信息不存在')
  37. time = Demeter.time()
  38. if time < self.data['site']['sdate'] or time > self.data['site']['edate']:
  39. self.out('授权已失效')
  40. self.service = Demeter.service('convert')
  41. self.referer = self.request.headers.get("Referer")
  42. self.host = Demeter.host(self.data['site']['link'])
  43. '''
  44. uri = Common.getHost(self) + self.request.uri
  45. if not self.referer:
  46. self.out('验证失败')
  47. if self.host != Demeter.host(self.referer):
  48. self.out('验证失败')
  49. if self.referer == uri:
  50. self.out('验证失败')
  51. '''
  52. self.param = self.service.signature(self.data['site']['id'], appid, self.data['site']['appsecret'], timestamp, nonce, file, file_id, uid, status)
  53. if not self.param:
  54. self.out('验证失败')
  55. if self.param['signature'] != signature:
  56. self.out('验证失败')
  57. if status == 1:
  58. file_type = self.input('file_type')
  59. file = self.service.update(self.data['site']['id'], self.data['site']['appid'], file, file_id, file_type, uid)
  60. else:
  61. file = self.service.get(self.data['site']['id'], self.data['site']['appid'], file);
  62. if not file:
  63. self.out('未生成文件信息')
  64. return file
  65. @staticmethod
  66. def getHost(self):
  67. host = self.request.host.replace(':8088', '')
  68. host = self.request.protocol + "://" + host
  69. return host
  70. class test_path(Load):
  71. @Web.setting
  72. def get(self):
  73. from wand.image import Image
  74. filename = Demeter.path + 'runtime/upload/d1c6fd2a4b2941e083471c2ae3d97a37.pdf'
  75. i = 0
  76. pdf = Image(filename=filename, resolution=50)
  77. jpg = pdf.convert('jpg')
  78. req_image = []
  79. for img in jpg.sequence:
  80. if i == 0:
  81. img_page = Image(image=img)
  82. req_image.append(img_page.make_blob('jpg'))
  83. i = i+1
  84. for img in req_image:
  85. ff = open(filename + '.jpg','wb')
  86. ff.write(img)
  87. ff.close()
  88. self.out('yes', Demeter.config)
  89. return
  90. import csv,codecs
  91. from datetime import *
  92. file_name = 'test.csv'
  93. day = str(date.today())
  94. day = day.split('-')
  95. file_path = day[0] + '/' + day[1] + '/' + day[2]
  96. file_path = File.mkdirs(os.path.join(Demeter.path, 'runtime','upload', file_path)) + '/' + file_name
  97. 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'}]
  98. 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'}]
  99. datad = {}
  100. for t,v in data[0]['data']['rows']:
  101. if t not in datad:
  102. datad[t] = [t,v]
  103. else:
  104. datad[t].append(v)
  105. newdata = sorted(datad.keys())
  106. newds = []
  107. for i in newdata:
  108. newds.append(datad[i])
  109. datad = newds
  110. header = ['日期', '温度', '湿度']
  111. datad.insert(0, header)
  112. date = '日期'
  113. datas = [[date, 'dfdf'],
  114. ['Bob', 14],
  115. ['Tom', 23],
  116. ['Jerry', '18']]
  117. #self.set_header("Content-Type","text/csv,charset=UTF-8")
  118. #self.set_header('Content-Disposition', 'attachment; filename=' + file_name)
  119. with open(file_path, 'w') as f:
  120. f.write(codecs.BOM_UTF8)
  121. writer = csv.writer(f)
  122. for row in datad:
  123. writer.writerow(row)
  124. url = Common.getHost(self);
  125. file_path = url + file_path.replace(Demeter.path + 'runtime', '')
  126. self.redirect(file_path)
  127. #self.out('yes', datad)
  128. # 请求转换 /main/convert 接口必须后端获取,token不允许暴露
  129. #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
  130. class convert_path(Load):
  131. @Web.setting
  132. def get(self):
  133. file = self.input('file')
  134. # 源文件id
  135. file_id = self.input('file_id')
  136. # 源文件类型 1为收费 2为不收费
  137. file_type = self.input('file_type', 1)
  138. # 当前的更新类型,update=insert则为新入库 update=update则为更新,区别是insert要入队
  139. update = self.input('update', 'insert')
  140. # 文件流 后续再增加这个吧,要把这个文件流先生成一个本地文件,暂时不做
  141. #filedata = self.input('filedata')
  142. uid = self.input('uid')
  143. self.data = {}
  144. file = Common.signature(self, 1)
  145. # 推入到redis队列
  146. if (file['status'] == 1 or file['status'] == 4) and update == 'insert':
  147. redis = Demeter.redis()
  148. config = Demeter.config['redis']
  149. redis.rpush(config['name'], file['id'])
  150. url = Common.getHost(self);
  151. self.data['file'] = {
  152. 'status' : file['status'],
  153. 'url' : url + file['url'],
  154. 'ext' : file['ext'],
  155. 'name' : file['name'],
  156. 'file' : file['key']
  157. }
  158. self.out('yes', self.data['file'])
  159. # 获取信息 后端接口,token不允许暴露
  160. class get_path(Load):
  161. @Web.setting
  162. def get(self):
  163. file = self.input('file')
  164. self.data = {}
  165. file = Common.signature(self, 2)
  166. url = Common.getHost(self);
  167. self.data['file'] = {
  168. #'id' : file['id'],
  169. 'status' : file['status'],
  170. 'url' : url + file['url'],
  171. 'img' : url + file['url'] + '.jpg',
  172. 'page' : file['page'],
  173. 'ext' : file['ext'],
  174. 'name' : file['name'],
  175. 'key' : file['key'],
  176. 'size' : file['size']
  177. }
  178. self.out('yes', self.data['file'])
  179. # 验证用户是否有效
  180. class check_path(Load):
  181. @Web.setting
  182. def get(self):
  183. appid = self.input('appid')
  184. appsecret = self.input('appsecret')
  185. if not appid:
  186. self.out('参数错误:appid')
  187. if not appsecret:
  188. self.out('参数错误:appsecret')
  189. site = Demeter.model('site')
  190. site.appid = appid
  191. site.appsecret = appsecret
  192. self.data = {}
  193. self.data['site'] = site.select(type='fetchone')
  194. if not self.data['site']:
  195. self.out('站点信息不存在')
  196. time = Demeter.time()
  197. if time < self.data['site']['sdate'] or time > self.data['site']['edate']:
  198. self.out('授权已失效')
  199. self.out('yes', appid)
  200. # 授权用户可以访问html的接口 后端接口,token不允许暴露
  201. class auth_path(Load):
  202. @Web.setting
  203. def get(self):
  204. file = self.input('file')
  205. uid = self.input('uid')
  206. self.data = {}
  207. file = Common.signature(self, 2)
  208. if file:
  209. self.service.auth(self.data['site']['id'], uid, file['id'], 2)
  210. self.out('yes', {'msg':1})
  211. # 读取html 带有权限控制 该接口为前端接口 需要有授权接口 后续实现
  212. class view_path(Load):
  213. @Web.setting
  214. def get(self):
  215. uid = self.input('uid')
  216. page = self.input('page')
  217. path = self.input('path')
  218. callback = self.input('callback')
  219. source = self.input('source')
  220. self.data = {}
  221. file = Common.signature(self, 11)
  222. # 读取源文件
  223. user = self.service.getAuth(self.data['site']['id'], uid, file['id'])
  224. limit = -1
  225. if not user and file['file_type'] == 1:
  226. # 没有文件信息,则有限制
  227. limit = self.data['site']['page']
  228. if page:
  229. content = self.getPage(path, page, limit)
  230. elif source:
  231. content = self.getSource(file, limit, uid)
  232. else:
  233. content = self.getContent(file, limit, uid)
  234. if callback:
  235. self.out(content)
  236. else:
  237. self.write(content)
  238. def getPage(self, path, page, limit):
  239. num = page.replace('.page', '')
  240. if limit > 0 and int(num) > limit:
  241. self.out('您没有权限查看')
  242. url = Common.getHost(self);
  243. static = url + path
  244. if 'save' in Demeter.config['setting']:
  245. page = path + page
  246. else:
  247. page = Demeter.path + 'runtime' + path + page
  248. content = File.readContent(page)
  249. content = content.replace('src="', 'src="' + str(static))
  250. #content = content.replace('img class', 'img style="width:auto" class')
  251. #content = content.replace('<div id="pf', '<div style="width:100%" id="pf')
  252. #content = content.replace('<div class="pc', '<div style="width:100%" class="pc')
  253. return content
  254. def getContent(self, file, limit, uid):
  255. if limit < 0:
  256. limit = int(file['page'])
  257. url = Common.getHost(self);
  258. style = ['document.css']
  259. script = ['jquery.js', 'layer.js']
  260. content = ''
  261. static = self.static(url)
  262. assets = self.assets(url, file['path'])
  263. for i in style:
  264. content = content + '<link rel="stylesheet" href="'+static+i+'"/>'
  265. #content = content + '<link rel="stylesheet" href="'+assets+file['key']+'.css"/>'
  266. #content = content + '<style>' + File.readContent(file['path'], file['key']+'.css') + '</style>'
  267. for i in script:
  268. content = content + '<script src="'+static+i+'"></script>'
  269. content = content + self.style()
  270. content = content + '<div id="page-container" load-css="'+assets+file['key']+'.css">'
  271. page_url = self.url(url, file['path'], file['key'], uid)
  272. for i in range(limit):
  273. n = str(i+1)
  274. #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>'
  275. 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>'
  276. content = content + '</div>'
  277. #content = content.replace('img class', 'img style="width:auto" class')
  278. #content = content + '<script>try{pdf2htmlEX.defaultViewer = new pdf2htmlEX.Viewer({});}catch(e){}</script>'
  279. return content
  280. def getSource(self, file, limit, uid):
  281. view = file['html'] + '_p' + str(limit) + '.html'
  282. if File.exists(view):
  283. content = File.readContent(view)
  284. File.write(view, content)
  285. else:
  286. content = File.readContent(file['html'])
  287. url = Common.getHost(self);
  288. static = self.static(url) + 'pdf/'
  289. content = content.replace('src="', 'src="' + static)
  290. content = content.replace('href="', 'href="' + self.assets(url, file['path']))
  291. if limit > 0:
  292. content = self.pattern(limit,content)
  293. content = content.replace('data-page-url="', 'data-page-url="' + self.url(url, file['path'], file['key'], uid))
  294. #content = self.domain(content)
  295. content = content.replace('</head>', self.style() + '<head>')
  296. return content
  297. def static(self, url):
  298. return url + '/static/'
  299. def assets(self, url, path):
  300. if 'save' not in Demeter.config['setting']:
  301. path = path.replace(Demeter.path + 'runtime', '')
  302. return url + path
  303. def pattern(self, limit, content):
  304. import re
  305. pattern = '<div id="pf([0-9][a-zA-Z]+|[a-zA-Z0]+|['+str(limit+1)+'-9]|([1-9][0-9]+))"(.*?)<\/div>'
  306. content = re.sub(pattern, '', content)
  307. return content
  308. def domain(self, content):
  309. script = '<script>document.domain="'+self.host+'";</script>'
  310. content = content.replace('</body>', script + '</body>')
  311. return content
  312. def style(self):
  313. 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>'
  314. def url(self, url, path, key, uid):
  315. appid = self.input('appid')
  316. nonce = self.input('nonce')
  317. timestamp = self.input('timestamp')
  318. signature = self.input('signature')
  319. file_id = self.input('file_id')
  320. if 'save' not in Demeter.config['setting']:
  321. path = path.replace(Demeter.path + 'runtime', '')
  322. url = url + '/main/view?path=' + str(path) + '&file=' + key + '&file_id=' + file_id + '&timestamp=' + timestamp + '&nonce=' + nonce + '&appid=' + appid + '&signature=' + signature + '&uid=' + uid + '&page='
  323. return url
  324. # 下载源文件
  325. class down_path(Load):
  326. @Web.setting
  327. def get(self):
  328. appid = self.input('appid')
  329. nonce = self.input('nonce')
  330. signature = self.input('signature')
  331. file = self.input('file')
  332. uid = self.input('uid')
  333. self.data = {}
  334. file = Common.signature(self, 2)
  335. user = self.service.getAuth(self.data['site']['id'], uid, file['id'])
  336. if not user and file['file_type'] == 1:
  337. # 没有文件信息,则不允许下载
  338. self.out('没有权限下载')
  339. self.set_header ('Content-Type', 'application/octet-stream')
  340. self.set_header ('Content-Disposition', 'attachment; filename='+file['name']+file['ext'])
  341. content = File.read(file['local'])
  342. self.write(content)