prepare #I84HUG app_ai_media网站图片等多媒体转本地--blog_post

This commit is contained in:
Chill
2023-10-10 18:39:28 +08:00
parent da6a8dc2be
commit a0903b3dbd
2 changed files with 25 additions and 1 deletions

View File

@@ -39,7 +39,7 @@
{ {
'name': "odooai Odooapp Common Func", 'name': "odooai Odooapp Common Func",
'version': '16.23.09.27', 'version': '16.23.10.10',
'author': 'odooai.cn', 'author': 'odooai.cn',
'category': 'Base', 'category': 'Base',
'website': 'https://www.odooai.cn', 'website': 'https://www.odooai.cn',

View File

@@ -104,6 +104,17 @@ class Base(models.AbstractModel):
# 返回这个图片的base64编码 # 返回这个图片的base64编码
return get_image_from_url(url) return get_image_from_url(url)
@api.model
def get_image_url2attachment(self, url, mimetype_list=None):
# Todo: mimetype filter
image, file_name = get_image_url2attachment(url)
if image and file_name:
attachment = self.env['ir.attachment'].create({
'datas': image,
'name': file_name,
})
return attachment
def get_ua_type(self): def get_ua_type(self):
return get_ua_type() return get_ua_type()
@@ -116,6 +127,19 @@ def get_image_from_url(url):
return None return None
# 返回这个图片的base64编码 # 返回这个图片的base64编码
return base64.b64encode(BytesIO(response.content).read()) return base64.b64encode(BytesIO(response.content).read())
def get_image_url2attachment(url):
if not url:
return None
try:
response = requests.get(url, timeout=5)
except Exception as e:
return None, None
# 返回这个图片的base64编码
image = base64.b64encode(BytesIO(response.content).read())
file_name = url.split('/')[-1]
return image, file_name
def get_ua_type(): def get_ua_type():
ua = request.httprequest.headers.get('User-Agent') ua = request.httprequest.headers.get('User-Agent')