diff --git a/cronjob/media/compress.py b/cronjob/media/compress.py index 6e02396..fe8ae06 100644 --- a/cronjob/media/compress.py +++ b/cronjob/media/compress.py @@ -69,15 +69,53 @@ class S: def upload_img_to_oss(self, local_path: str, oss_src_id: str): return self.bucket.put_object_from_file(oss_src_id, local_path) + def deal_one(self, oss_src_id: str, oss_src_download_path: str, fmt: str, resize_p: int): + oss_src_id_python_type = oss_src_id.replace("/", "_") + # 当前目录 + cur_dir = os.getcwd() + "/" + # 源文件转resize_p + oss_resize_dir = "imgprod{}".format(resize_p) + oss_resize_src_id = oss_src_id.replace("imgprod", oss_resize_dir) + local_resize_path = cur_dir + oss_src_id_python_type.replace("imgprod", oss_resize_dir) + ".{}".format(fmt) + ret = compress_and_save_image(oss_src_download_path, local_resize_path, resize_p) + if ret != "success": + logger.Error("compress_and_save_image {} fail, err: {}, src: {}, local: {}".format(resize_p, ret, oss_src_download_path, local_resize_path)) + return False + + try: + ret = self.upload_img_to_oss(local_resize_path, oss_resize_src_id) + if ret.status != 200: + logger.Error("upload_img_to_oss {} fail, err: {}, src: {}, local: {}".format(resize_p, ret, oss_src_download_path, local_resize_path)) + return False + # 更新mongo + file_size = os.path.getsize(local_resize_path) + q = { + "src_id": oss_src_id + } + up = { + "size_{}".format(resize_p): file_size, + "src_id_{}".format(resize_p): oss_resize_src_id + } + mongo_ret = self.col_image.update_one(q, up) + if not mongo_ret: + logger.Error("update_one fail, q: {}, up: {}".format(q, up)) + return False + except Exception as e: + logger.Error("upload_img_to_oss {} fail, err: {}, src: {}, local: {}".format(resize_p, str(e), oss_src_download_path, local_resize_path)) + return False + + os.remove(local_resize_path) + return True + def proc(self): images = [ { - "src_id": "imgprod/23/f8/9b54-b9f0-4add-9024-7eb06b0ba0bf" + "src_id": "imgprod/8d/d5/7ddc-1225-4f4c-ace0-c1c63f46b23d" } ] for image in images: - src_id = safe_get_str(image, "src_id") - src_id_python_type = src_id.replace("/", "_") + oss_src_id = safe_get_str(image, "src_id") + src_id_python_type = oss_src_id.replace("/", "_") # 当前目录 cur_dir = os.getcwd() + "/" @@ -85,49 +123,32 @@ class S: # 先下载到本地,源文件 oss_src_download_path = cur_dir + src_id_python_type + ".jpeg" try: - self.save_img_from_oss(src_id, oss_src_download_path) + self.save_img_from_oss(oss_src_id, oss_src_download_path) src_size = os.path.getsize(oss_src_download_path) - except Exception as e: - logger.Error("save_img_from_oss fail, err: {}, src: {}".format(str(e), src_id)) - continue - - # 源文件转720P - oss_720_src_id = src_id.replace("imgprod", "imgprod720") - local_720p_path = cur_dir + src_id_python_type.replace("imgprod", "imgprod720") + ".jpeg" - ret720 = compress_and_save_image(oss_src_download_path, local_720p_path, Resize720P) - if ret720 != "success": - logger.Error("compress_and_save_image 720 fail, err: {}, src: {}, local: {}".format(ret720, oss_src_download_path, local_720p_path)) - continue - try: - ret = self.upload_img_to_oss(local_720p_path, oss_720_src_id) - if ret.status != 200: - logger.Error("upload_img_to_oss 720 fail, err: {}, src: {}, local: {}".format(ret720, oss_src_download_path, local_720p_path)) + q = { + "src_id": oss_src_id + } + up = { + "size_src": src_size, + } + mongo_ret = self.col_image.update_one(q, up) + if not mongo_ret: + logger.Error("update_one fail, q: {}, up: {}".format(q, up)) continue - # 更新mongo - except Exception as e: - logger.Error("upload_img_to_oss 720 fail, err: {}, src: {}, local: {}".format(str(e), oss_src_download_path, local_720p_path)) + logger.Error("save_img_from_oss fail, err: {}, src_id: {}".format(str(e), oss_src_id)) continue - # 源文件转1080P - oss_1080_src_id = src_id.replace("imgprod", "imgprod1080") - local_1080p_path = cur_dir + src_id_python_type.replace("imgprod", "imgprod1080") + ".jpeg" - ret1080 = compress_and_save_image(oss_src_download_path, local_1080p_path, Resize1080P) - if ret1080 != "success": - logger.Error("compress_and_save_image 1080 fail, err: {}, src: {}, local: {}".format(ret1080, oss_src_download_path, local_1080p_path)) - continue - ret = self.upload_img_to_oss(local_1080p_path, oss_1080_src_id) - print(ret.status) + # 720P + ret = self.deal_one(oss_src_id, oss_src_download_path, "jpeg", Resize720P) - # 源文件转1440P - oss_1440_src_id = src_id.replace("imgprod", "imgprod1440") - local_1440p_path = cur_dir + src_id_python_type.replace("imgprod", "imgprod1440") + ".jpeg" - ret1440 = compress_and_save_image(oss_src_download_path, local_1440p_path, Resize1440P) - if ret1440 != "success": - logger.Error("compress_and_save_image 1440 fail, err: {}, src: {}, local: {}".format(ret1440, oss_src_download_path, local_1440p_path)) - continue - ret = self.upload_img_to_oss(local_1440p_path, oss_1440_src_id) - print(ret.status) + # 1080P + ret = self.deal_one(oss_src_id, oss_src_download_path, "jpeg", Resize1080P) + + # 1440P + ret = self.deal_one(oss_src_id, oss_src_download_path, "jpeg", Resize1440P) + + os.remove(oss_src_download_path) s = S()