Skip to content

feat: add features like public bucket name, list of doctypes that can… #38

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

Open
wants to merge 1 commit into
base: master
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
34 changes: 29 additions & 5 deletions frappe_s3_attachment/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@ def __init__(self):
)
else:
self.S3_CLIENT = boto3.client('s3')
self.BUCKET = self.s3_settings_doc.bucket_name

self.folder_name = self.s3_settings_doc.folder_name
self.BUCKET = self.s3_settings_doc.bucket_name
self.PUBLIC_BUCKET = self.s3_settings_doc.public_bucket_name
# Doctypes which can have public file uploads.
# Format: {<doctype name>: <string to be used to prepend the aws key>}
self.DOCTYPES_WITH_PUBLIC_ACCESS = [
{doctype.doctype_name: doctype.prepend_s3_key_with}
for doctype in self.s3_settings_doc.public_doctype_details
]

def strip_special_chars(self, file_name):
"""
Expand Down Expand Up @@ -107,10 +115,10 @@ def upload_files_to_s3_with_key(
Uploads a new file to S3.
Strips the file extension to set the content_type in metadata.
"""
mime_type = magic.from_file(file_path, mime=True)
key = self.key_generator(file_name, parent_doctype, parent_name)
content_type = mime_type
try:
mime_type = magic.from_file(file_path, mime=True)
key = self.key_generator(file_name, parent_doctype, parent_name)
content_type = mime_type
if is_private:
self.S3_CLIENT.upload_file(
file_path, self.BUCKET, key,
Expand All @@ -123,6 +131,11 @@ def upload_files_to_s3_with_key(
}
)
else:
key_prepend_with = self.DOCTYPES_WITH_PUBLIC_ACCESS.get(parent_doctype, "")
if key_prepend_with and not key_prepend_with.endswith("/"):
key_prepend_with += "/"
key = key_prepend_with + key

self.S3_CLIENT.upload_file(
file_path, self.BUCKET, key,
ExtraArgs={
Expand Down Expand Up @@ -207,9 +220,20 @@ def file_upload_to_s3(doc, method):
parent_doctype = doc.attached_to_doctype
parent_name = doc.attached_to_name
ignore_s3_upload_for_doctype = frappe.local.conf.get('ignore_s3_upload_for_doctype') or ['Data Import']

if parent_doctype not in ignore_s3_upload_for_doctype:
if not doc.is_private:
file_path = site_path + '/public' + path

if parent_doctype not in s3_upload.DOCTYPES_WITH_PUBLIC_ACCESS and not doc.is_private:
# If a doctype is not allowed (or not declared) to have public files,
# remove the file path and throw an error.
try:
os.remove(file_path)
except OSError:
pass
frappe.throw("Not allowed to upload the file with public access. Make the file as private and retry.")

else:
file_path = site_path + path
key = s3_upload.upload_files_to_s3_with_key(
Expand All @@ -224,7 +248,7 @@ def file_upload_to_s3(doc, method):
else:
file_url = '{}/{}/{}'.format(
s3_upload.S3_CLIENT.meta.endpoint_url,
s3_upload.BUCKET,
s3_upload.PUBLIC_BUCKET,
key
)
os.remove(file_path)
Expand Down
Loading