Skip to content
Merged
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
9 changes: 8 additions & 1 deletion cloud_storage/cloud_storage/overrides/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
FILE_URL = "/api/method/retrieve?key={path}"
URL_PREFIXES = ("http://", "https://", "/api/method/retrieve")

LOCAL_ONLY_DOCTYPES = ("Data Import", "Repost Item Valuation")


class CloudStorageFile(File):
@File.is_remote_file.getter
Expand Down Expand Up @@ -716,7 +718,7 @@ def write_file(file: File, remove_spaces_in_file_name: bool = True) -> File:
file.save_file_on_filesystem()
return file

if file.attached_to_doctype == "Data Import":
if file.attached_to_doctype in LOCAL_ONLY_DOCTYPES:
file.save_file_on_filesystem()
return file

Expand Down Expand Up @@ -769,6 +771,11 @@ def delete_file(file: File, **kwargs) -> File:
if file.is_folder:
return file

# files kept on disk (see LOCAL_ONLY_DOCTYPES) have nothing in the bucket to delete
if file.file_url and not file.is_remote_file:
file.delete_file_from_filesystem()
return file

if file.file_url and "?key=" in file.file_url:
key = file.file_url.split("?key=")[1]
if key:
Expand Down
49 changes: 48 additions & 1 deletion cloud_storage/tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
from moto import mock_s3
from werkzeug.datastructures import FileMultiDict

from cloud_storage.cloud_storage.overrides.file import CloudStorageFile, retrieve
from cloud_storage.cloud_storage.overrides.file import (
LOCAL_ONLY_DOCTYPES,
CloudStorageFile,
delete_file,
retrieve,
write_file,
)
from cloud_storage.migration import migrate_files


Expand Down Expand Up @@ -496,3 +502,44 @@ def paste(file_name, attached_to_name):
key = second.file_url.split("?key=")[1]
assert frappe.db.exists("File", {"s3_key": key})
assert second.unique_url == f"{second.file_url}&fid={first.name}"


@pytest.mark.parametrize("doctype", LOCAL_ONLY_DOCTYPES)
def test_local_only_doctype_file_stays_on_filesystem(doctype):
"""
Files attached to LOCAL_ONLY_DOCTYPES must bypass S3 entirely: core code (e.g. ERPNext's
create_json_gz_file for Repost Item Valuation) rewrites them in place via
get_full_path() + open(), which crashes with FileNotFoundError on a retrieve URL.
"""
frappe.set_user("Administrator")
file = frappe.get_doc(
{
"doctype": "File",
"file_name": f"{frappe.scrub(doctype)}-test.json.gz",
"attached_to_doctype": doctype,
"attached_to_name": "TEST-0001",
"attached_to_field": "reposting_data_file",
"is_private": 1,
}
)
file._content = b"\x1f\x8b" + b"x" * 32

with patch("cloud_storage.cloud_storage.overrides.file.get_cloud_storage_client") as client:
result = write_file(file)

client.assert_not_called()
assert result.file_url.startswith("/private/files/")
assert not result.is_remote_file

# get_full_path() must be a real path that core can reopen for writing on the next step
path = result.get_full_path()
assert Path(path).is_file()
with open(path, "wb") as f:
f.write(b"rewritten")
assert Path(path).read_bytes() == b"rewritten"

# the delete hook must clean the local file instead of trying to delete a bucket object
with patch("cloud_storage.cloud_storage.overrides.file.get_cloud_storage_client") as client:
delete_file(result)
client.assert_not_called()
assert not Path(path).exists()
Loading