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, (binary_type, string_types)): 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_unicode(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)
def create_message( self, subject, recipients, body=None, html=None, sender=None, cc=None, bcc=None, reply_to=None, content_charset='utf-8', attachments=None, message_id=None): force_development_email = None if not self.request.is_production_environ: force_development_email = self.settings.get('force_development_email') or None if body: body = to_string(body, encoding=content_charset) if not html: html = body.replace(NEW_LINE, HTML_NEW_LINE) if html: html = to_string(html, encoding=content_charset) if not html.lower().startswith('<html'): html = '<html><body>%s</body></html>' % html options = {} # FROM sender if sender: options['sender'] = format_email( sender, encoding=content_charset, force_development_email=force_development_email) # Envelope CC if cc: if isinstance(cc, dict): cc = [cc] options['cc'] = [ format_email(e, content_charset, force_development_email=force_development_email) for e in maybe_list(cc)] # Envelope BCC if bcc: if isinstance(bcc, dict): bcc = [bcc] options['bcc'] = [ format_email(e, content_charset, force_development_email=force_development_email) for e in maybe_list(bcc)] if not message_id: domain = self.settings.get('message_domain') or self.api_session_manager.default_domain message_id = make_msgid(make_unique_hash(10), domain) extra_headers = { 'Date': formatdate(localtime=True), 'Message-ID': to_string(message_id)} # Force reply to another email if reply_to: extra_headers['Reply-To'] = '<%s>' % to_string(reply_to) mime_attachments = [] if attachments: if isinstance(attachments, dict): attachments = [attachments] for attachment in maybe_list(attachments): f = attachment.get('file') or attachment['fp'] filename = to_string(attachment.get('filename') or basename(f.name)).replace(' ', '') mimetype = to_string(attachment.get('content_type') or find_mimetype(filename, f)) f.seek(0) mime_attachments.append(self.api_session_manager.attachment_cls( data=f, filename=filename, content_type=mimetype)) return self.api_session_manager.message_cls( subject=to_string(subject, encoding=content_charset), html=html, body=body, recipients=[ format_email(e, content_charset, force_development_email=force_development_email) for e in maybe_list(recipients)], attachments=mime_attachments, extra_headers=extra_headers, **options)