This commit is contained in:
lwl0608 2024-04-29 11:45:42 +08:00
parent 8661576b2b
commit 3f996628c0
1 changed files with 34 additions and 4 deletions
cronjob/media

View File

@ -24,9 +24,13 @@ class S:
collection="image"
)
print(self.col_image)
self.del_images = list()
def __del__(self):
self.col_image.close()
for lp in self.del_images:
os.remove(lp)
logger.Info("{} files removed".format(len(self.del_images)))
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)
@ -39,19 +43,45 @@ class S:
# 获取所有heic图片
q = {"fmt": "image/heic"}
images = self.col_image.find(q, limit=1)
idx = 0
for img in images:
print(img)
idx += 1
old_key = safe_get_str(img, "src_id")
old_fmt = safe_get_str(img, "fmt")
new_key = old_key + "_h2j"
arr = old_key.split("/")
local_path = arr[len(arr) - 1] + ".jpeg"
self.del_images.append(local_path)
ret_save = self.heic_to_jpg_and_save(old_key, local_path)
ret_save_statue = ret_save.resp.status
logger.Info("heic_to_jpg_and_save, ret: {}".format(ret_save_statue))
ret_save_status = ret_save.resp.status
if ret_save_status != 200:
logger.Info("heic_to_jpg_and_save fail, old_key: {}, ret: {}".format(old_key, ret_save_status))
continue
# 上传oss
ret_upload = self.upload_img_to_oss(local_path, new_key)
ret_upload_status = ret_upload.resp.status
if ret_upload_status != 200:
logger.Info("upload_img_to_oss fail, old_key: {}, ret: {}".format(old_key, ret_save_status))
continue
# 修改db
q = {
"src_id": old_key
}
up = {
"$set": {
"src_id": new_key,
"old_fmt": old_fmt,
"fmt": "image/jpeg"
}
}
mongo_ret = self.col_image.update_one(q, up)
logger.Info("{}, heic2jpeg finish, new_key: {}, mongo_modify: {}".format(idx, new_key, mongo_ret.modified_count))
# ret_upload = self.upload_img_to_oss(local_path, new_key)
s = S()
s.proc_oen()