用于将文件存储到文件系统存储中的字段
项目描述
此插件定义了一个新的字段类型FSFile,它是一个文件字段,将文件存储在外部文件系统中而不是Odoo的文件存储中。这对于您不想存储在文件存储中的大文件非常有用。此外,字段值为您提供了访问文件内容和元数据的接口。
目录
使用方法
新字段 FSFile 已开发,允许您在外部文件系统存储中存储文件。其设计基于以下原则:
只有在需要时才从文件系统中读取文件内容。
默认情况下必须能够将文件内容作为流进行操作。
与Odoo的二进制字段不同,内容是原始文件内容(没有base64编码)。
为了允许与其他系统交换文件内容,可以将内容写入base64。读取操作将返回一个包含文件名、MIME类型、大小和下载文件URL的JSON结构。
这种设计允许在处理大型文件以及通过默认jsonrpc接口与其他系统交换时,最大限度地减少服务器的内存消耗。
具体来说,这种设计允许您编写如下代码:
from IO import BytesIO
from odoo import models, fields
from odoo.addons.fs_file.fields import FSFile
class MyModel(models.Model):
_name = 'my.model'
name = fields.Char()
file = FSFile()
# Create a new record with a raw content
my_model = MyModel.create({
'name': 'My File',
'file': BytesIO(b"content"),
})
assert(my_model.file.read() == b"content")
# Create a new record with a base64 encoded content
my_model = MyModel.create({
'name': 'My File',
'file': b"content".encode('base64'),
})
assert(my_model.file.read() == b"content")
# Create a new record with a file content
my_model = MyModel.create({
'name': 'My File',
'file': open('my_file.txt', 'rb'),
})
assert(my_model.file.read() == b"content")
assert(my_model.file.name == "my_file.txt")
# create a record with a file content as base64 encoded and a filename
# This method is useful to create a record from a file uploaded
# through the web interface.
my_model = MyModel.create({
'name': 'My File',
'file': {
'filename': 'my_file.txt',
'content': base64.b64encode(b"content"),
},
})
assert(my_model.file.read() == b"content")
assert(my_model.file.name == "my_file.txt")
# write the content of the file as base64 encoded and a filename
# This method is useful to update a record from a file uploaded
# through the web interface.
my_model.write({
'file': {
'name': 'my_file.txt',
'file': base64.b64encode(b"content"),
},
})
# the call to read() will return a json structure with the filename,
# the mimetype, the size and a url to download the file.
info = my_model.file.read()
assert(info["file"] == {
"filename": "my_file.txt",
"mimetype": "text/plain",
"size": 7,
"url": "/web/content/1234/my_file.txt",
})
# use the field as a file stream
# In such a case, the content is read from the filesystem without being
# stored in memory.
with my_model.file.open("rb) as f:
assert(f.read() == b"content")
# use the field as a file stream to write the content
# In such a case, the content is written to the filesystem without being
# stored in memory. This kind of approach is useful to manipulate large
# files and to avoid to use too much memory.
# Transactional behaviour is ensured by the implementation!
with my_model.file.open("wb") as f:
f.write(b"content")
变更日志
16.0.1.0.6 (2024-02-23)
错误修正
修复了创建空文件的问题。
在此更改之前,创建空文件会导致约束违反错误。这是因为即使给文件起了名字,如果没有提供内容,该名字也不会被保存到FSFileValue对象中。因此,当在数据库中创建对应的ir.attachment时,名字没有设置,违反了“必需”约束。(#341)
16.0.1.0.5 (2023-11-30)
错误修正
确保在将新值分配给FSFile字段时,缓存被正确设置。如果字段已存储,缓存中的值必须是链接到存储文件所使用的附件记录的FSFileValue对象。否则,值必须是已提供的值,因为它可能是计算方法的结果。(#290)
16.0.1.0.4 (2023-10-17)
错误修正
使用sudo()浏览附件以避免读取访问错误
在具有多fs图像关系的模型中,表单中的新行将触发onchanges并调用fs.file模型的‘convert_to_cache()’方法,该方法将尝试使用可能没有读取附件模型权限的用户配置文件浏览附件。(#288)
16.0.1.0.3 (2023-10-05)
错误修正
修复了mimetype属性在FSFileValue对象上的问题。
mimetype值按以下方式计算
如果设置了附件,则从附件中获取mimetype。
如果没有设置附件,则从文件名中猜测mimetype。
如果无法从文件名中猜测mimetype,则从文件内容中猜测mimetype。(#284)
16.0.1.0.1 (2023-09-29)
功能
在FSFileValue对象上添加了url_path属性。此属性允许您轻松获取对文件系统上文件相对路径的访问。只有当文件系统存储配置了Base URL值时,此值才可用。(#281)
错误修正
如果信息不可用,url_path、url和internal_url属性在FSFileValue对象上返回None(而不是False)。
FSFileValue对象上的url属性返回文件系统URL,而不是附件的URL字段。(#281)
错误跟踪器
在GitHub Issues上跟踪错误。如果遇到问题,请检查是否已报告您的问题。如果您是第一个发现问题的,请通过提供详细和受欢迎的反馈来帮助我们解决问题。
请不要直接联系贡献者寻求支持或帮助解决技术问题。
鸣谢
贡献者
Laurent Mignon <laurent.mignon@acsone.eu>
Marie Lejeune <marie.lejeune@acsone.eu>
Hugues Damry <hughes.damry@acsone.eu>
Nguyen Minh Chien <chien@trobz.com>
维护者
此模块由OCA维护。
OCA,即Odoo社区协会,是一个非营利组织,其使命是支持Odoo功能的协作开发并促进其广泛使用。
当前维护者
此模块是GitHub上OCA/storage项目的一部分。
欢迎您贡献。要了解如何贡献,请访问https://odoo-community.org/page/Contribute。
项目详情
哈希值 for odoo_addon_fs_file-17.0.1.0.0.4-py3-none-any.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | ea29b7be25ad657820c3fca6881b8dafb70848f93732510eda0145c61cf57572 |
|
MD5 | d90a2357b225b6f3f0bcdb0ebeda2575 |
|
BLAKE2b-256 | 0e11b57bab740a233a5da1168d61f08a9fa74373859f97d631abec4cd4ebe633 |