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

Use File field for other content types than Image or File #46

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

espenmn
Copy link

@espenmn espenmn commented Jul 17, 2024

No description provided.

@CLAassistant
Copy link

CLAassistant commented Jul 17, 2024

CLA assistant check
All committers have signed the CLA.

@espenmn
Copy link
Author

espenmn commented Aug 29, 2024

I have a better approach to this, because:

  1. Someone could turn off 'required' for file type
  2. File attachment could 'disappear' since files are stored on the file system. This is unlikely, but better have a check for it anyway.

So I suggest:

def getFileExt(context):
portal_type = context.portal_type
# First set 'filename' to None, this will prevent error local variable 'filename' referenced before assignment if file is missing
filename = None

#This makes it possible to use with all content that has a 'file'
if hasattr(context, 'file'):
    #Check if content has uploaded file, in case required setting is disabled
    if context.file is not None:
        #Check if file contains data, for use with other content types than Image and File
        filename = context.file.filename if context.file.getSize() and context.file.getSize() > 1 else None

if portal_type == "Image" : 
    filename = context.image.filename if hasattr(context, "image") else None

if portal_type == "File" or portal_type == "Document" :
    filename = context.file.filename if hasattr(context, "file") else None

if filename :
    return filename[filename.rfind('.') + 1:].lower()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants