rabin 6 роки тому
батько
коміт
e4f7ef6aef
3 змінених файлів з 149 додано та 77 видалено
  1. 3 68
      convert.py
  2. 57 2
      front/api/main.py
  3. 89 7
      service/convert.py

+ 3 - 68
convert.py

@@ -4,78 +4,13 @@ sys.getdefaultencoding()
 reload(sys)
 sys.setdefaultencoding('UTF-8')
 from demeter.core import *
-import os
-import os.path
-import requests
-import shutil
 
 # 测试命令行传参 python convert.py -f file.doc
 param = {}
 param['file'] = 'f'
 Demeter.getopt(param)
 
-def command(file):
-	id = int(file)
-	model = Demeter.model('convert')
-	model.id = id
-	info = convert.select(type='fetchone')
-
-	status = True
-	if info['status'] == 1 or info['status'] == 4:
-		status = False
-
-	if info and status == False:
-		model.id = id
-		update = {}
-		update['status'] = 2
-		#model.update(update)
-
-		if not File.exists(info['local']):
-			if 'http' in info['file']:
-				r = requests.get(info['file'], stream=True)
-				with open(info['local'], 'wb') as up:
-					for chunk in r.iter_content(chunk_size=1024):
-						if chunk:
-							up.write(chunk)
-
-			else:
-				shutil.copyfile(info['file'], info['local'])
-
-		if not File.exists(info['html']):
-			convert = 'cd ' + info['path'] + ' && '
-			convert = convert + 'libreoffice  --invisible --convert-to pdf ' + info['local']
-			convert = convert + ' && '
-			convert = convert + 'pdf2htmlEX --zoom 1.3 --no-drm 1 --split-pages 1 '
-			convert = convert + '--embed-css 1  --embed-javascript 0 --embed-image 0 --embed-font 1 '
-			convert = convert + '--embed-external-font 0 --dest-dir '+info['path']+' --page-filename %d.page ' + info['pdf']
-
-			print convert
-			return
-			Shell.popen(convert)
-			if File.exists(info['html']):
-				# 获取有多少页
-				page = total(info['path'])
-				model.id = id
-				update = {}
-				update['page'] = page
-				update['status'] = 3
-				model.update(update)
-				return
-
-		model.id = id
-		update = {}
-		update['status'] = 4
-		model.update(update)
-		
-
-
-def total(path):
-	page = 0
-	for parentdir,dirname,filenames in os.walk(path):  
-		for filename in filenames:
-			if os.path.splitext(filename)[1]=='.page':
-				page = page + 1
-	return page
-
 file = Demeter.option['file']
-command(file)
+id = int(file)
+convert = Demeter.service('convert')
+convert.handle(id)

+ 57 - 2
front/api/main.py

@@ -6,7 +6,7 @@
 """
 from .__load__ import *
 
-# 请求转换 /main/convert
+# 请求转换 /main/convert 接口必须后端获取,token不允许暴露
 class convert_path(Load):
 	@Web.setting
 	def get(self):
@@ -40,8 +40,63 @@ class convert_path(Load):
 
 		self.out('yes', data)
 
-# 获取信息
+# 获取信息 后端接口,token不允许暴露
 class get_path(Load):
+	@Web.setting
+	def get(self):
+		key = int(self.input('site', 1))
+		token = self.input('token')
+		file = self.input('file')
+		site = Demeter.model('site')
+		site.key = key
+		data = {}
+		data['site'] = site.select(type='fetchone')
+
+		if not file:
+			self.out('错误的文件信息')
+
+		if not data['site']:
+			self.out('站点信息不存在')
+
+		if data['site']['token'] != token:
+			self.out('验证失败')
+
+		service = Demeter.service('convert')
+		data['file'] = service.get(data['site']['id'], key,  file);
+
+		self.out('yes', data)
+
+# 授权用户可以访问html的接口 后端接口,token不允许暴露
+class auth_path(Load):
+	@Web.setting
+	def get(self):
+		key = int(self.input('site', 1))
+		user = self.input('user')
+		token = self.input('token')
+		file = self.input('file')
+		site = Demeter.model('site')
+		site.key = key
+		data = {}
+		data['site'] = site.select(type='fetchone')
+
+		if not file:
+			self.out('错误的文件信息')
+
+		if not data['site']:
+			self.out('站点信息不存在')
+
+		if data['site']['token'] != token:
+			self.out('验证失败')
+
+		service = Demeter.service('convert')
+		data['file'] = service.get(data['site']['id'], key,  file);
+
+		# 授权之后生成一个key,用于前端权限验证
+
+		self.out('yes', data)
+
+# 读取html 带有权限控制 该接口为前端接口 需要有授权接口
+class view_path(Load):
 	@Web.setting
 	def get(self):
 		key = int(self.input('site', 1))

+ 89 - 7
service/convert.py

@@ -2,6 +2,8 @@
 from demeter.core import *
 from datetime import *
 import uuid
+import os
+import os.path
 import requests
 import shutil
 
@@ -82,6 +84,7 @@ class Convert(object):
 
 		info['local'] = local
 		info['pdf'] = filepath + '/' + filename + '.pdf'
+		# 这里要增加权限控制 html不能直接访问
 		info['html'] = filepath + '/' + filename + '.html'
 		info['url'] = info['html'].replace(Demeter.path + 'runtime', '')
 		info['path'] = filepath + '/'
@@ -93,7 +96,7 @@ class Convert(object):
 			self.download(file, local);
 			return info
 
-	def download(file, local):
+	def download(self, file, local):
 		if 'http' in file:
 			r = requests.get(file, stream=True)
 			with open(local, 'wb') as up:
@@ -108,12 +111,91 @@ class Convert(object):
 			return True
 		return False
 
-	def handle(self, file, key, ext, path, pdf):
-		convert = 'libreoffice  --invisible --convert-to pdf ' + file
+	def command(self, info):
+		convert = 'cd ' + info['path'] + ' && '
+		convert = convert + 'libreoffice  --invisible --convert-to pdf ' + info['local']
 		convert = convert + ' && '
 		convert = convert + 'pdf2htmlEX --zoom 1.3 --no-drm 1 --split-pages 1 '
 		convert = convert + '--embed-css 1  --embed-javascript 0 --embed-image 0 --embed-font 1 '
-		convert = convert + '--embed-external-font 0 --dest-dir '+path+' --page-filename %d.page ' + pdf
-
-		print convert
-		return
+		convert = convert + '--embed-external-font 0 --dest-dir '+info['path']+' --page-filename %d.page ' + info['pdf']
+
+		return convert
+
+
+	def total(self, path):
+		page = 0
+		for parentdir,dirname,filenames in os.walk(path):  
+			for filename in filenames:
+				if os.path.splitext(filename)[1]=='.page':
+					page = page + 1
+		return page
+
+	def handle(self, id):
+		model = Demeter.model('convert')
+		model.id = id
+		info = model.select(type='fetchone')
+
+		if not info:
+			return
+		status = True
+		if info['status'] == 1 or info['status'] == 4:
+			status = False
+
+		if info and status == False:
+			model.id = id
+			update = {}
+			update['status'] = 2
+			#model.update(update)
+
+			if not File.exists(info['local']):
+				self.download(info['file'], info['local'])
+
+			if not File.exists(info['html']):
+				handle = self.command(info)
+				print handle
+				return
+				Shell.popen(handle)
+				if File.exists(info['html']):
+
+					self.string_switch(info['html'], "taste", "tasting")
+					# 获取有多少页
+					page = self.total(info['path'])
+					model.id = id
+					update = {}
+					update['page'] = page
+					update['status'] = 3
+					model.update(update)
+					return
+
+			model.id = id
+			update = {}
+			update['status'] = 4
+			model.update(update)
+
+	def string_switch(self, x,y,z,s=1):
+		with open(x, "r", encoding="utf-8") as f:
+			#readlines以列表的形式将文件读出
+			lines = f.readlines()
+	 
+		with open(x, "w", encoding="utf-8") as f_w:
+			#定义一个数字,用来记录在读取文件时在列表中的位置
+			n = 0
+			#默认选项,只替换第一次匹配到的行中的字符串
+			if s == 1:
+				for line in lines:
+					if y in line:
+						line = line.replace(y,z)
+						f_w.write(line)
+						n += 1
+						break
+					f_w.write(line)
+					n += 1
+				#将剩余的文本内容继续输出
+				for i in range(n,len(lines)):
+					f_w.write(lines[i])
+			#全局匹配替换
+			elif s == 'g':
+				for line in lines:
+					if y in line:
+						line = line.replace(y,z)
+					f_w.write(line)