Beispiel #1
0
    def save_file_path(self, binary, filename=None, compressed=False):
        # Generate a unique code using SHA256
        if isinstance(binary, StorageFile):
            filename = filename or binary.name
            binary = binary.read()
        if isinstance(binary, (bytes, str)):
            unique_code = string_unique_code(binary)
        else:
            unique_code = file_unique_code(binary)
            if not filename:
                filename = basename(binary.name)

        lock_key = 'storage save %s' % unique_code
        self.cache.lock(lock_key)
        try:
            file_path = (
                self.session
                .query(*FilePath.__table__.c.values())
                .filter(FilePath.code == unique_code)
                .first())

            # Save file if dont exists
            if not file_path:
                # Create a new filepath
                filename = maybe_string(filename)
                mimetype = find_mimetype(filename=filename, header_or_file=binary)
                file_path = FilePath(code=unique_code, mimetype=maybe_string(mimetype), compressed=compressed)

                to_add = []
                file_size = 0

                # Save blocks
                blocks = self.save_blocks(binary)

                for order, (block_id, block_size) in enumerate(blocks):
                    to_add.append(FileBlock(file_id_block=block_id, order=order))
                    file_size += block_size

                # Add file path to DB
                file_path.size = file_size
                file_path.id = self.direct_insert(file_path).inserted_primary_key[0]

                # Relate blocks and file path
                for block_relation in to_add:
                    block_relation.file_id_path = file_path.id
                    self.direct_insert(block_relation)

            return file_path
        finally:
            self.cache.unlock(lock_key)
Beispiel #2
0
 def create(msgid, mapping=None, default=None, context=None):
     if isinstance(msgid, TranslationString):
         domain = msgid.domain or factory_domain
     else:
         msgid = maybe_string(msgid)
         domain = factory_domain
     return TranslationString(msgid, domain=domain, default=default, mapping=mapping, context=context)
Beispiel #3
0
def validate_code(key, value, length, reverse=False, startswith=None):
    value = maybe_string(value)
    if not value:
        raise Error(key, _('Required'))

    number = value.strip().lower()
    if not number.isnumeric():
        raise Error(key, _('Need to be a number'))
    elif len(number) != length:
        raise Error(key, _('Need to have ${length} digits', mapping={'length': length}))

    if startswith and str(number[0]) not in startswith:
        startswith_str = '"%s"' % '", "'.join(startswith)
        raise Error(key, _('Need to start with ${chars}', mapping={'chars': startswith_str}))

    last_number = int(number[-1])
    number = number[:-1]

    if reverse:
        number_list = reversed(number)
    else:
        number_list = list(number)

    prime = find_next_prime(length)
    check_digit = prime - (sum(i * int(d) for i, d in enumerate(number_list, 2)) % prime)
    if last_number != check_digit:
        raise Error(key, _('Invalid number'))
    else:
        return value
Beispiel #4
0
def find_mimetype(filename=None, header_or_file=None):
    if is_file_type(header_or_file):
        header_or_file.seek(0)
        header = header_or_file.read(guess_mimetype_with_data.min_header_size)
    else:
        header = maybe_bytes(header_or_file)

    mimetype = None
    if header is not None:
        mimetype = guess_mimetype_with_data(header)

    if not mimetype:
        filename = maybe_string(filename)
        if filename:
            mimetype, encoding = mimetypes.guess_type(filename, strict=True)
            if not mimetype:
                mimetype, encoding = mimetypes.guess_type(filename, strict=False)

    return maybe_string(mimetype)
Beispiel #5
0
def normalize_full_name(value):
    value = maybe_string(value)
    if value:
        words = [
            word
            for word in clean_string(value, replace_case='_').lower().split()
            if word not in IGNORE_FULL_NAME_WORDS]
        if words:
            return ' '.join(words)
    return ''
Beispiel #6
0
def validate_url(key, value, required=False):
    url = maybe_string(value)
    if required and (not url or url in URL_PROTOCOLS):
        raise Error(key, 'Obrigatório')

    elif url and url not in URL_PROTOCOLS:
        for protocol in URL_PROTOCOLS:
            if url.startswith(protocol):
                if len(url[len(protocol):]) < 2:
                    raise Error(key, 'É necessário indicar um url')
                return url

        raise Error(key, 'O url tem de começar por "%s"' % '" ou "'.join(URL_PROTOCOLS))
Beispiel #7
0
    def save_file(
            self,
            binary,
            application_code,
            code_key=None,
            type_key=None,
            data=None,
            filename=None,
            title=None,
            parent_id=None):

        file_path = self.save_file_path(binary, filename)

        session_id = session_type = None
        if self.request.authenticated:
            session_id = self.request.authenticated.session_id
            session_type = self.request.authenticated.session_type

        # Add applications file relation
        new = File(
            file_id=file_path.id,
            parent_id=parent_id,
            key=make_unique_hash(70),
            application_code=to_string(application_code),
            code_key=maybe_string(code_key),
            type_key=maybe_string(type_key),
            data=maybe_string(data),
            filename=maybe_string(filename),
            title=maybe_string(title),
            session_type=session_type,
            session_id=session_id)
        self.session.add(new)
        self.session.flush()

        # Add some attributes
        new.size = file_path.size
        new.mimetype = file_path.mimetype
        return new
Beispiel #8
0
def format_email(email, encoding=None, force_development_email=None):
    label = None

    if isinstance(email, dict):
        if 'label' in email:
            label = email['label']
        email = email['email']

    elif not isinstance(email, string_types):
        label, email = email

    return to_unicode(
        formataddr((
            maybe_string(label, encoding=encoding),
            to_string(force_development_email or email, encoding=encoding))))
Beispiel #9
0
        def _render(value, system):
            f = value['file']
            response = system['request'].response
            response.app_iter = f
            response.content_length = int(value['content_length'])
            response.status = 200
            response.content_type = maybe_string(value.get('content_type'))

            filename = value.get('filename') or basename(f.name)
            if value.get('is_attachment'):
                content_disposition = 'attachment; filename="%s"' % filename
            else:
                content_disposition = 'inline; filename="%s"' % filename

            response.content_disposition = encode_and_decode(content_disposition, 'latin-1', 'ignore')

            # Add others headers
            add_header = response._headerlist.append
            add_header(('Status-Code', response.status))

            return None
Beispiel #10
0
def get_url_info(*args, **kwargs):
    headers = get_url_headers(*args, **kwargs)
    return {
        'size': maybe_integer(headers.get('Content-Length')) or 0,
        'type': maybe_string(headers.get('Content-Type')),
        'updated': guess_datetime(headers.get('Last-Modified'))}