mirror of
https://github.com/guohuadeng/app-odoo.git
synced 2025-02-23 04:11:36 +02:00
prepare #I84HUG app_ai_media网站图片等多媒体转本地--blog_post
This commit is contained in:
@@ -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',
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|
||||||
@@ -117,6 +128,19 @@ def get_image_from_url(url):
|
|||||||
# 返回这个图片的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')
|
||||||
# 临时用 agent 处理,后续要前端中正确处理或者都从后台来
|
# 临时用 agent 处理,后续要前端中正确处理或者都从后台来
|
||||||
|
|||||||
Reference in New Issue
Block a user