Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][FIX] fs_attachment: fix fs_url computing #404

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fs_attachment/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ Laurent Mignon <laurent.mignon@acsone.eu>
Marie Lejeune <marie.lejeune@acsone.eu>
Wolfgang Pichler <wpichler@callino.at>
Nans Lefebvre <len@lambdao.dev>
Mohamed Alkobrosli <alkobroslymohamed@gmail.com>

Maintainers
~~~~~~~~~~~
Expand Down
7 changes: 7 additions & 0 deletions fs_attachment/models/fs_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,13 @@ def _get_url_for_attachment(
):
fs_filename = fs_filename.replace(fs_storage.directory_path, "")
parts = [base_url, fs_filename]
if attachment.fs_storage_id:
if (
fs_storage.optimizes_directory_path
and not fs_storage.use_filename_obfuscation
):
checksum = attachment.checksum
parts = [base_url, checksum[:2], checksum[2:4], fs_filename]
return self._normalize_url("/".join(parts))

@api.model
Expand Down
1 change: 1 addition & 0 deletions fs_attachment/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Laurent Mignon <laurent.mignon@acsone.eu>
Marie Lejeune <marie.lejeune@acsone.eu>
Wolfgang Pichler <wpichler@callino.at>
Nans Lefebvre <len@lambdao.dev>
Mohamed Alkobrosli <alkobroslymohamed@gmail.com>
3 changes: 2 additions & 1 deletion fs_attachment/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,8 @@ <h2><a class="toc-backref" href="#toc-entry-17">Contributors</a></h2>
Laurent Mignon &lt;<a class="reference external" href="mailto:laurent.mignon&#64;acsone.eu">laurent.mignon&#64;acsone.eu</a>&gt;
Marie Lejeune &lt;<a class="reference external" href="mailto:marie.lejeune&#64;acsone.eu">marie.lejeune&#64;acsone.eu</a>&gt;
Wolfgang Pichler &lt;<a class="reference external" href="mailto:wpichler&#64;callino.at">wpichler&#64;callino.at</a>&gt;
Nans Lefebvre &lt;<a class="reference external" href="mailto:len&#64;lambdao.dev">len&#64;lambdao.dev</a>&gt;</p>
Nans Lefebvre &lt;<a class="reference external" href="mailto:len&#64;lambdao.dev">len&#64;lambdao.dev</a>&gt;
Mohamed Alkobrosli &lt;<a class="reference external" href="mailto:alkobroslymohamed&#64;gmail.com">alkobroslymohamed&#64;gmail.com</a>&gt;</p>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-18">Maintainers</a></h2>
Expand Down
40 changes: 40 additions & 0 deletions fs_attachment/tests/test_fs_storage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2023 ACSONE SA/NV (http://acsone.eu).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import base64
import os

from odoo.exceptions import ValidationError
Expand Down Expand Up @@ -372,3 +373,42 @@ def test_recompute_urls(self):
"""
)
self.assertEqual(self.env.cr.dictfetchall()[0].get("count"), 2)

def test_url_for_image_dir_optimized_and_not_obfuscated(self):
# Create a base64 encoded mock image (1x1 pixel transparent PNG)
image_data = base64.b64encode(
b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08"
b"\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\nIDAT\x08\xd7c\xf8\x0f\x00"
b"\x01\x01\x01\x00\xd1\x8d\xcd\xbf\x00\x00\x00\x00IEND\xaeB`\x82"
)

# Create a mock image filestore
fs_storage = self.env["fs.storage"].create(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the protocol of your storage? Protocol is a required field and there is nor more default value on it on the 16.0 branch. Be careful to don't serve your images from the odoo's filestore in a public way. If you configure your proxy to get access to the filestore under the 'images' path, you expose all the files from your odoo instance....

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lmignon

Thanks for clarifying, but I use different filestore for only products imsges thumbnails.

But the bug is completely different from these settings that you mentioned.

I can use many protocols and to test I used file protocol.

Thanks

{
"name": "FS Product Image Backend",
"code": "file",
"base_url": "https://localhost/images",
"optimizes_directory_path": True,
"use_filename_obfuscation": False,
}
)

# Create a mock image attachment
attachment = self.env["ir.attachment"].create(
{"name": "test_image.png", "datas": image_data, "mimetype": "image/png"}
)

# Get the url from the model
fs_url_1 = fs_storage._get_url_for_attachment(attachment)

# Generate the url that should be accessed
base_url = fs_storage.base_url_for_files
fs_filename = attachment.fs_filename
checksum = attachment.checksum
parts = [base_url, checksum[:2], checksum[2:4], fs_filename]
fs_url_2 = fs_storage._normalize_url("/".join(parts))

# Make some checks and asset if the two urls are equal
self.assertTrue(parts)
self.assertTrue(checksum)
self.assertEqual(fs_url_1, fs_url_2)
Loading