浏览代码

更新 'service/convert.py'

rabin 6 年之前
父节点
当前提交
029bcf7c05
共有 1 个文件被更改,包括 373 次插入373 次删除
  1. 373 373
      service/convert.py

+ 373 - 373
service/convert.py

@@ -1,373 +1,373 @@
-# -*- coding: utf-8 -*-
-from __future__ import division
-from .__load__ import *
-
-class Convert(object):
-
-	def crearSignature(self):
-		num = Demeter.time() - 3600*24
-		model = Demeter.model('signature')
-		model.cdate.assign(num, exp='<=')
-		model.delete()
-
-	def signature(self, site_id, appid, appsecret, timestamp, nonce, file, file_id, uid, status = 1):
-		sign = Demeter.sha1(appid + '&' + appsecret + '&' + str(timestamp) + '&' + str(nonce) + '&' + file + '&' + str(file_id) + '&' + str(uid));
-
-		time = Demeter.time()
-		num = 3600*12
-		if time - int(timestamp) > num:
-			return False
-
-		# sign 只能使用一次
-		if status < 10:
-			model = Demeter.model('signature')
-			model.signature = sign
-			model.site_id = site_id
-			info = model.select(type='fetchone')
-			if info:
-				return False
-
-		param = {}
-		param['signature'] = sign
-		param['appid'] = appid
-		param['appsecret'] = appsecret
-		param['timestamp'] = timestamp
-		param['nonce'] = nonce
-		param['file'] = file
-		param['file_id'] = file_id
-		param['uid'] = uid
-
-		if status < 10:
-			model.signature = sign
-			model.site_id = site_id
-			model.insert()
-
-		return param
-
-	def update(self, site, appid, file, file_id, file_type, uid):
-
-		info = self.getFile(appid, file)
-
-		convert = Demeter.model('convert')
-		convert.site_id = site
-		convert.key = info['key']
-
-		data = convert.select(type='fetchone')
-		if not data:
-			convert.site_id = site
-			convert.uid = uid
-			convert.file_id = file_id
-			convert.file_type = file_type
-			convert.file_size = 0
-			convert.file = info['file']
-			convert.key = info['key']
-			convert.name = info['name']
-			convert.ext = info['ext']
-
-			convert.local = info['local']
-			convert.path = info['path']
-			convert.pdf = info['pdf']
-			convert.html = info['html']
-			convert.url = info['url']
-
-			id = convert.insert()
-
-			info['status'] = 1
-			info['id'] = id
-
-		else:
-			info['id'] = data['id']
-			info['status'] = data['status']
-
-		if uid:
-			self.auth(site, uid, info['id'], 1)
-
-		return info
-
-	def getAuth(self, site, uid, convert_id):
-		user = Demeter.model('user')
-		user.uid = uid
-		user.site_id = site
-		user.convert_id = convert_id
-
-		data = user.select(type='fetchone')
-
-		return data
-
-
-	def auth(self, site, uid, convert_id, status):
-		user = Demeter.model('user')
-		user.uid = uid
-		user.site_id = site
-		user.convert_id = convert_id
-
-		data = user.select(type='fetchone')
-		if not data:
-			user.site_id = site
-			user.uid = uid
-			user.convert_id = convert_id
-			user.status = status
-			user.insert()
-		elif data['status'] != status:
-			# 适用于文档转让
-			user.id = data['id']
-			update = {}
-			update['status'] = status
-			user.update(update)
-
-		return True
-
-	def get(self, site, appid, file):
-		convert = Demeter.model('convert')
-		convert.site_id = site
-		#convert.key = self.getKey(appid, file)
-		convert.key = file
-
-		data = convert.select(type='fetchone')
-
-		return data
-
-	def getKey(self, appid, file):
-		return Demeter.sha1(str(appid) + '_' + str(file))
-
-	def getFile(self, appid, file):
-		info = {}
-
-		(filepath,temp) = os.path.split(file)
-		(filename,extension) = os.path.splitext(temp)
-
-		info['file'] = file
-		info['key'] = self.getKey(appid, file)
-		info['ext'] = extension
-		info['name'] = filename
-
-		info = self.getLocalFile(appid, file, info)
-
-		return info
-
-	def getLocalFile(self, appid, file, info):
-
-		day = str(date.today())
-		day = day.split('-')
-
-		#filename =  Demeter.md5(str(uuid.uuid5(uuid.uuid1(), info['key'])))
-		filename =  info['key']
-		filepath = str(appid) + '/' + day[0] + '/' + day[1] + '/' + day[2]
-		path = ''
-
-		if 'save' in Demeter.config['setting']:
-			filepath = File.mkdirs(os.path.join(Demeter.config['setting']['save'], filepath)) + '/' + filename
-		else:
-			filepath = File.mkdirs(os.path.join(Demeter.path, 'runtime','files', filepath)) + '/' + filename
-
-		local = filepath + info['ext']
-
-		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 + '/'
-
-		return info
-		if File.exists(local):
-			return info
-		else:
-			self.download(file, local);
-			return info
-
-	def download(self, file, local):
-		if 'http' in file:
-			import requests
-			r = requests.get(file, stream=True)
-			with open(local, 'wb') as up:
-				for chunk in r.iter_content(chunk_size=1024):
-					if chunk:
-						up.write(chunk)
-
-		else:
-			import shutil
-			shutil.copyfile(file, local)
-
-		if File.exists(local):
-			return True
-		return False
-
-	def command(self, info):
-		File.mkdir(info['path'])
-		convert = 'cd ' + info['path'] + ' && '
-
-		if info['ext'] != '.pdf':
-			#convert = convert + 'libreoffice  --invisible --convert-to pdf ' + info['local']
-			convert = convert + 'soffice --headless --invisible --convert-to pdf ' + info['local']
-			convert = convert + ' && '
-		else:
-			info['pdf'] = info['local']
-		convert = convert + 'pdf2htmlEX --zoom 1 --use-cropbox 0 --no-drm 1 --split-pages 1 '
-		convert = convert + '--embed-css 0  --embed-javascript 0 --embed-image 0 --embed-font 1 --process-outline 0 '
-		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
-		siteModel = Demeter.model('site')
-		siteModel.id = info['site_id']
-		site = siteModel.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']):
-				self.download(info['file'], info['local'])
-
-			if not File.exists(info['html']):
-			#if info:
-				handle = self.command(info)
-				Shell.popen(handle)
-				if File.exists(info['html']):
-
-					# 截屏
-					self.cut(info)
-					#self.string_switch(info['html'], "taste", "tasting")
-					# 获取有多少页
-					page = self.total(info['path'])
-					model.id = id
-					size = os.path.getsize(info['local'])
-					update = {}
-					update['file_size'] = size
-					update['page'] = page
-					update['status'] = 3
-					model.update(update)
-
-					# 通知接口 通知应用成功转换
-					info['page'] = page
-					info['file_size'] = size
-					info['convert_status'] = 1
-					self.api(info, site)
-					return
-
-			model.id = id
-			update = {}
-			update['status'] = 4
-			model.update(update)
-			# 通知接口 通知应用失败转换
-			info['page'] = 0
-			info['file_size'] = 0
-			info['convert_status'] = 2
-			self.api(info, site)
-
-	def api(self, info, site):
-		if 'file_id' in info and info['file_id']:
-			api = site['api']
-
-			appid = site['appid']
-			appsecret = site['appsecret']
-			timestamp = Demeter.time()
-			nonce = Demeter.hash()
-			file = info['key']
-			file_id = info['file_id']
-			uid = info['uid']
-
-			param = self.signature(site['id'], appid, appsecret, timestamp, nonce, file, file_id, uid)
-
-			param['url'] = 'main/view'
-			param['img'] = info['url'] + '.jpg'
-			param['page'] = info['page']
-			param['ext'] = info['ext']
-			param['file_size'] = info['file_size']
-			param['status'] = info['convert_status']
-
-			Demeter.curl(api, param, 'post')
-
-	def cut(self, info):
-		from pdf2jpg import pdf2jpg
-		from wand.image import Image
-		dest = info['html'] + '.photo'
-		result = pdf2jpg.convert_pdf2jpg(info['pdf'], dest, pages="0")
-		source = result[0]['output_jpgfiles'][0]
-		dest = info['html'] + '.jpg'
-		command = 'mv '+source+' ' + dest
-		Shell.popen(command)
-		target_width = 300
-		with Image(filename=dest) as img:
-			target_width, target_height = self.getSize(target_width, img.width, img.height)
-			img.sample(target_width, target_height)
-			img.save(filename=dest)
-
-		'''
-		from wand.image import Image
-		pdf = Image(filename=source, resolution=50)
-		jpg = pdf.convert('jpg')
-		req_image = []
-		i = 0
-		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(dest, 'wb')
-			ff.write(img)
-			ff.close()
-		'''
-
-
-	def getSize(self, target_width, img_width, img_height):
-		if img_width > target_width:
-			ratio = target_width / img_width
-			target_height = int(ratio * img_height)
-		else:
-			target_width = img_width
-			target_height = img_height
-		return target_width, target_height
-
-	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)
+# -*- coding: utf-8 -*-
+from __future__ import division
+from .__load__ import *
+
+class Convert(object):
+
+	def crearSignature(self):
+		num = Demeter.time() - 3600*24
+		model = Demeter.model('signature')
+		model.cdate.assign(num, exp='<=')
+		model.delete()
+
+	def signature(self, site_id, appid, appsecret, timestamp, nonce, file, file_id, uid, status = 1):
+		sign = Demeter.sha1(appid + '&' + appsecret + '&' + str(timestamp) + '&' + str(nonce) + '&' + file + '&' + str(file_id) + '&' + str(uid));
+
+		time = Demeter.time()
+		num = 3600*12
+		if time - int(timestamp) > num:
+			return False
+
+		# sign 只能使用一次
+		if status < 10:
+			model = Demeter.model('signature')
+			model.signature = sign
+			model.site_id = site_id
+			info = model.select(type='fetchone')
+			if info:
+				return False
+
+		param = {}
+		param['signature'] = sign
+		param['appid'] = appid
+		param['appsecret'] = appsecret
+		param['timestamp'] = timestamp
+		param['nonce'] = nonce
+		param['file'] = file
+		param['file_id'] = file_id
+		param['uid'] = uid
+
+		if status < 10:
+			model.signature = sign
+			model.site_id = site_id
+			model.insert()
+
+		return param
+
+	def update(self, site, appid, file, file_id, file_type, uid):
+
+		info = self.getFile(appid, file)
+
+		convert = Demeter.model('convert')
+		convert.site_id = site
+		convert.key = info['key']
+
+		data = convert.select(type='fetchone')
+		if not data:
+			convert.site_id = site
+			convert.uid = uid
+			convert.file_id = file_id
+			convert.file_type = file_type
+			convert.file_size = 0
+			convert.file = info['file']
+			convert.key = info['key']
+			convert.name = info['name']
+			convert.ext = info['ext']
+
+			convert.local = info['local']
+			convert.path = info['path']
+			convert.pdf = info['pdf']
+			convert.html = info['html']
+			convert.url = info['url']
+
+			id = convert.insert()
+
+			info['status'] = 1
+			info['id'] = id
+
+		else:
+			info['id'] = data['id']
+			info['status'] = data['status']
+
+		if uid:
+			self.auth(site, uid, info['id'], 1)
+
+		return info
+
+	def getAuth(self, site, uid, convert_id):
+		user = Demeter.model('user')
+		user.uid = uid
+		user.site_id = site
+		user.convert_id = convert_id
+
+		data = user.select(type='fetchone')
+
+		return data
+
+
+	def auth(self, site, uid, convert_id, status):
+		user = Demeter.model('user')
+		user.uid = uid
+		user.site_id = site
+		user.convert_id = convert_id
+
+		data = user.select(type='fetchone')
+		if not data:
+			user.site_id = site
+			user.uid = uid
+			user.convert_id = convert_id
+			user.status = status
+			user.insert()
+		elif data['status'] != status:
+			# 适用于文档转让
+			user.id = data['id']
+			update = {}
+			update['status'] = status
+			user.update(update)
+
+		return True
+
+	def get(self, site, appid, file):
+		convert = Demeter.model('convert')
+		convert.site_id = site
+		#convert.key = self.getKey(appid, file)
+		convert.key = file
+
+		data = convert.select(type='fetchone')
+
+		return data
+
+	def getKey(self, appid, file):
+		return Demeter.sha1(str(appid) + '_' + str(file))
+
+	def getFile(self, appid, file):
+		info = {}
+
+		(filepath,temp) = os.path.split(file)
+		(filename,extension) = os.path.splitext(temp)
+
+		info['file'] = file
+		info['key'] = self.getKey(appid, file)
+		info['ext'] = extension
+		info['name'] = filename
+
+		info = self.getLocalFile(appid, file, info)
+
+		return info
+
+	def getLocalFile(self, appid, file, info):
+
+		day = str(date.today())
+		day = day.split('-')
+
+		#filename =  Demeter.md5(str(uuid.uuid5(uuid.uuid1(), info['key'])))
+		filename =  info['key']
+		filepath = str(appid) + '/' + day[0] + '/' + day[1] + '/' + day[2]
+		path = ''
+
+		if 'save' in Demeter.config['setting']:
+			filepath = File.mkdirs(os.path.join(Demeter.config['setting']['save'], filepath)) + '/' + filename
+		else:
+			filepath = File.mkdirs(os.path.join(Demeter.path, 'runtime','files', filepath)) + '/' + filename
+
+		local = filepath + info['ext']
+
+		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 + '/'
+
+		return info
+		if File.exists(local):
+			return info
+		else:
+			self.download(file, local);
+			return info
+
+	def download(self, file, local):
+		if 'http' in file:
+			import requests
+			r = requests.get(file, stream=True)
+			with open(local, 'wb') as up:
+				for chunk in r.iter_content(chunk_size=1024):
+					if chunk:
+						up.write(chunk)
+
+		else:
+			import shutil
+			shutil.copyfile(file, local)
+
+		if File.exists(local):
+			return True
+		return False
+
+	def command(self, info):
+		File.mkdir(info['path'])
+		convert = 'cd ' + info['path'] + ' && '
+
+		if info['ext'] != '.pdf':
+			#convert = convert + 'libreoffice  --invisible --convert-to pdf ' + info['local']
+			convert = convert + 'soffice --headless --invisible --convert-to pdf ' + info['local']
+			convert = convert + ' && '
+		else:
+			info['pdf'] = info['local']
+		convert = convert + 'pdf2htmlEX --zoom 1 --use-cropbox 0 --no-drm 1 --split-pages 1 '
+		convert = convert + '--embed-css 0  --embed-javascript 0 --embed-image 0 --embed-font 1 --process-outline 0 '
+		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
+		siteModel = Demeter.model('site')
+		siteModel.id = info['site_id']
+		site = siteModel.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']):
+				self.download(info['file'], info['local'])
+
+			if not File.exists(info['html']):
+			#if info:
+				handle = self.command(info)
+				Shell.popen(handle)
+				if File.exists(info['html']):
+
+					# 截屏
+					self.cut(info)
+					#self.string_switch(info['html'], "taste", "tasting")
+					# 获取有多少页
+					page = self.total(info['path'])
+					model.id = id
+					size = os.path.getsize(info['local'])
+					update = {}
+					update['file_size'] = size
+					update['page'] = page
+					update['status'] = 3
+					model.update(update)
+
+					# 通知接口 通知应用成功转换
+					info['page'] = page
+					info['file_size'] = size
+					info['convert_status'] = 1
+					self.api(info, site)
+					return
+
+			model.id = id
+			update = {}
+			update['status'] = 4
+			model.update(update)
+			# 通知接口 通知应用失败转换
+			info['page'] = 0
+			info['file_size'] = 0
+			info['convert_status'] = 2
+			self.api(info, site)
+
+	def api(self, info, site):
+		if 'file_id' in info and info['file_id']:
+			api = site['api']
+
+			appid = site['appid']
+			appsecret = site['appsecret']
+			timestamp = Demeter.time()
+			nonce = Demeter.hash()
+			file = info['key']
+			file_id = info['file_id']
+			uid = info['uid']
+
+			param = self.signature(site['id'], appid, appsecret, timestamp, nonce, file, file_id, uid)
+
+			param['url'] = 'main/view'
+			param['img'] = info['url'] + '.jpg'
+			param['page'] = info['page']
+			param['ext'] = info['ext']
+			param['file_size'] = info['file_size']
+			param['status'] = info['convert_status']
+
+			Demeter.curl(api, param, 'post')
+
+	def cut(self, info):
+		from pdf2jpg import pdf2jpg
+		from wand.image import Image
+		dest = info['html'] + '.photo'
+		result = pdf2jpg.convert_pdf2jpg(info['pdf'], dest, pages="0,1")
+		source = result[0]['output_jpgfiles'][0]
+		dest = info['html'] + '.jpg'
+		command = 'mv '+source+' ' + dest
+		Shell.popen(command)
+		target_width = 300
+		with Image(filename=dest) as img:
+			target_width, target_height = self.getSize(target_width, img.width, img.height)
+			img.sample(target_width, target_height)
+			img.save(filename=dest)
+
+		'''
+		from wand.image import Image
+		pdf = Image(filename=source, resolution=50)
+		jpg = pdf.convert('jpg')
+		req_image = []
+		i = 0
+		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(dest, 'wb')
+			ff.write(img)
+			ff.close()
+		'''
+
+
+	def getSize(self, target_width, img_width, img_height):
+		if img_width > target_width:
+			ratio = target_width / img_width
+			target_height = int(ratio * img_height)
+		else:
+			target_width = img_width
+			target_height = img_height
+		return target_width, target_height
+
+	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)