main.py 13 KB

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