Beispiel #1
0
def handleFileField(context, value, obj=None, is_primary=False, with_title=False):
    if not value:
        return ""
    name = getattr(value, 'filename', '')
    # if obj:
    #     name = obj.getId()
    # if not name:
    #     try:
    #         name = value.getId()
    #     except:
    #         name = ''
    # if not name:
    #     return ''
    name = IUserPreferredFileNameNormalizer(context.request).normalize(
        safe_unicode(name))
    # Crude band-aid: if there's no ending, stick in the lower part of
    # the content type
    if name.find('.') == -1:
        if IDexterityContent.providedBy(obj):
            content_type = getattr(value, 'contentType', '')
        else:
            content_type = getattr(value, 'content_type', '')
        name = "{0}.{1}".format(
            name, content_type.split('/')[-1])
    if is_primary:
        item_path = '/'.join(aq_parent(obj).getPhysicalPath()[context.LEN_PORTAL_PATH:])
    else:
        item_path = '/'.join(obj.getPhysicalPath()[context.LEN_PORTAL_PATH:])
    file_path = '/{0}/{1}'.format(item_path, name)
    try:
        data = value.data
    except POSKeyError:
        data = ""
    if isinstance(data, Pdata):
        data = data.data
    if len(data) == 0:
        return ''
    if context.export_binary:
        os.chdir(context.exportdir)
        if not os.path.exists(item_path):
            os.makedirs(item_path)
        fn = '{0}{1}'.format(context.exportdir, file_path)
        try:
            fh = open(fn, 'w')
        except IOError, err:
            if err.strerror == 'File name too long':
                # try to build a shorter filename
                name = "{0}.{1}".format(obj.id, value.content_type.split('/')[-1])
                file_path = '/{0}/{1}'.format(item_path, name)
                fn = '{0}{1}'.format(context.exportdir, file_path)
                try:
                    fh = open(fn, 'w')
                except IOError:
                    fh = None
                    file_path = ''
            else:
                fh = None
                file_path = ''
        except Exception:
            fh = None
            file_path = ''