123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- # -*- coding=utf-8
- # appid 已在配置中移除,请在参数 Bucket 中带上 appid。Bucket 由 BucketName-APPID 组成
- # 1. 设置用户配置, 包括 secretId,secretKey 以及 Region
- import os
- import re
- import time
- import datetime
- import random
- import hashlib
- from qcloud_cos import CosConfig
- from qcloud_cos import CosS3Client
- from qcloud_cos import CosServiceError
- from qcloud_cos import CosClientError
- import sys
- import logging
- # 日志
- image_upload_log = './logs/image_log2.txt' # 图片上传日志
- logging.basicConfig(level=logging.INFO,
- # stream=sys.stdout,
- filename = image_upload_log)
- # 腾讯云文件存储,存储桶,测试环境用户配置
- public_bucket = 'zxhx-pro-1302712961' # 桶名称
- APPID = '1302712961'
- secret_id = "AKIDC9pETRbZfWBbmhoglkT4PUJGzRjmj3Ia" # "云 API 密钥 SecretId";
- secret_key = "C6jlX4LKfleGdmfQvGNgj74lESRpBIEJ" # "云 API 密钥 SecretKey";
- TIMEOUT = 30
- token = None # 使用临时密钥需要传入Token,默认为空,可不填
- # 设置一个默认的存储桶地域
- region = "ap-beijing"
- addr = ".cos." + region + ".myqcloud.com" # 后缀拼接
- scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填
- cos_config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
- client = CosS3Client(cos_config) # 获取客户端对象
- def upload_img_to_ucloud(param_qcloud):
- put_key, localfile = param_qcloud
- # 高级上传接口(推荐)
- # 根据文件大小自动选择简单上传或分块上传,分块上传具备断点续传功能。
- response = client.upload_file(
- Bucket=public_bucket,
- LocalFilePath=localfile, # 本地文件的路径
- Key=put_key, # 上传到桶之后的文件名
- # PartSize=10,
- PartSize=1, # 上传分成几部分
- MAXThread=10, # 支持最多的线程数
- EnableMD5=False # 是否支持MD5
- )
- # print(response['ETag'])
- def upload_replace_image(filename_root, sid, html):
- return_error = {"errcode": 1,
- "errmsg": "word图片上传失败。"}
- daytime = datetime.datetime.now().strftime('/%Y/%m/%d/')
- image_path = filename_root + "/files"
- # todo 判断试卷是否含有图片,如果有就替换上传,没有就不处理
- local2online_dict = {}
- judge_file = os.path.isdir(image_path)
- if judge_file: # 可以不用判断
- image_number = re.findall(r'<img\s*src\s*=\s*"files/image', str(html))
- local_images_path_list = os.listdir(image_path) # 本地图片文件名
- local_images_path_list = list(filter(
- lambda x: str(x).endswith(".png") or str(x).endswith(".gif") or str(x).endswith(".jpeg") or str(x).endswith(
- ".jpg"), local_images_path_list)) # 只允许特定后缀名的图片
- if len(image_number) != len(local_images_path_list):
- return return_error
- else:
- # 從大到小把圖片进行排序
- try:
- local_images_path_list.sort(key=lambda x: int(re.search(r"image(\d+)\.[pngifje]+", str(x)).group(1)))
- except:
- return return_error
- # logger.info("local_images_path_list==>{}".format(str(local_images_path_list)))
- put_key_list = [] # 上传云上的图片名称
- localfile_list = [] # 本地图片地址
- rawpic_list = [] # html中的图片路径
- online_image_url_list = [] # 线上图片地址
- if local_images_path_list:
- try:
- for i, img in enumerate(local_images_path_list, start=1): # 所有的图片【image1.png,image2.png,。。。】
- # src_pat2 = re.compile(r'<img\s*src\s*=\s*"files/image{}\.png"'.format(i))
- # src_pat3 = re.compile(r'<img\s*src\s*=\s*"files/image{}\.gif"'.format(i))
- # src_pat4 = re.compile(r'<img\s*src\s*=\s*"files/image{}\.jpeg"'.format(i))
- # src_pat5 = re.compile(r'<img\s*src\s*=\s*"files/image{}\.jpg"'.format(i))
- # if str(img).endswith("png"):
- # pat = src_pat2
- # # re_list.append(src_pat2)
- # elif str(img).endswith("gif"):
- # pat = src_pat3
- # # re_list.append(src_pat3)
- # elif str(img).endswith("jpeg"):
- # pat = src_pat4
- # # re_list.append(src_pat4)
- # elif str(img).endswith("jpg"):
- # pat = src_pat5
- # # re_list.append(src_pat5)
- # todo 上传线上,并替换线上图片
- localfile = image_path + "/{}".format(img)
- rawfile = '<img src="files' + "/{}".format(img)
- localfile_list.append(localfile)
- rawpic_list.append(rawfile)
- # todo 图片压缩
- # resize_img(localfile, localfile)
- hash_img = get_md5(img)
- # 上传文件在空间中的名称
- put_key = "teacher/uploadfiles/wording/" + str(sid) + str(daytime) + str(hash_img)
- put_key_list.append(put_key)
- # html替换为线上的地址
- online_image_url = "http://" + str(public_bucket) + str(addr) + "/" + str(put_key)
- # html = pat.sub(r'<img src={}'.format(online_image_url), str(html))
- online_image_url = '<img src="{}'.format(online_image_url)
- online_image_url_list.append(online_image_url)
- local2online_dict = dict(zip(rawpic_list, online_image_url_list))
- return local2online_dict, put_key_list, localfile_list
- except:
- return return_error
- else:
- return return_error
- else:
- return local2online_dict, [], []
- def get_md5(image):
- """
- 由于hash不处理unicode编码的字符串(python3默认字符串是unicode)
- 所以这里判断是否字符串,如果是则进行转码
- 初始化md5、将image_name进行加密、然后返回加密字串
- """
- image_name, image_type = str(image).split(".")
- image_name = str(image_name) + str(time.time()) + str(random.random())
- if isinstance(image_name, str):
- image_name = image_name.encode("utf-8")
- md = hashlib.md5()
- md.update(image_name)
- # a = time.time()
- # b = random.random()
- return str(md.hexdigest()) + "." + str(image_type)
|