Skip to content

Commit

Permalink
fix: Use doctype setting to set auto-extracted file as private
Browse files Browse the repository at this point in the history
- Use `make_attachments_public` to determine the file privacy while auto creating files from the text editor field
- Currently, all files in the text editor field are automatically public
  • Loading branch information
marination committed Feb 9, 2024
1 parent 7e83d63 commit 7445de9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions frappe/core/doctype/file/test_file.py
Expand Up @@ -717,16 +717,18 @@ def tearDown(self) -> None:

class TestFileUtils(FrappeTestCase):
def test_extract_images_from_doc(self):
is_private = not frappe.db.get_value("DocType", "ToDo", "make_attachments_public")

# with filename in data URI
todo = frappe.get_doc(
{
"doctype": "ToDo",
"description": 'Test <img src="data:image/png;filename=pix.png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=">',
}
).insert()
self.assertTrue(frappe.db.exists("File", {"attached_to_name": todo.name}))
self.assertIn('<img src="/files/pix.png">', todo.description)
self.assertListEqual(get_attached_images("ToDo", [todo.name])[todo.name], ["/files/pix.png"])
self.assertTrue(frappe.db.exists("File", {"attached_to_name": todo.name, "is_private": is_private}))
self.assertIn('<img src="/private/files/pix.png">', todo.description)
self.assertListEqual(get_attached_images("ToDo", [todo.name])[todo.name], ["/private/files/pix.png"])

# without filename in data URI
todo = frappe.get_doc(
Expand Down
2 changes: 1 addition & 1 deletion frappe/core/doctype/file/utils.py
Expand Up @@ -216,7 +216,7 @@ def get_file_name(fname: str, optional_suffix: str | None = None) -> str:

def extract_images_from_doc(doc: "Document", fieldname: str):
content = doc.get(fieldname)
content = extract_images_from_html(doc, content)
content = extract_images_from_html(doc, content, is_private=(not doc.meta.make_attachments_public))
if frappe.flags.has_dataurl:
doc.set(fieldname, content)

Expand Down

0 comments on commit 7445de9

Please sign in to comment.