def write(content, backend, truncate=None): """Write the content to a backend, and get a URL for it. :param content: data. :type content: str :param truncate: number of charactors to slice from the key. :type truncate: int :returns: str """ key = hashlib.sha1(content.encode('utf-8')).hexdigest() if truncate: key = key[:truncate] if read(key): return key, local_url(key=key, backend=backend), False encoded_content = content.encode('utf-8') cdn.upload(key=key, content=encoded_content) log.info('Wrote paste to CDN', key=key) with LocalCache() as c: log.info('new object cached', key=key) c.set( key, encoded_content, expire=150 ) return key, local_url(key=key, backend=backend), True
def read(key): """Read the content from the CDN. :param key: index item. :type key: str """ r = requests.get(remote_url(key)) if r.status_code == requests.codes.ok: log.info('Retrieved paste from CDN', key=key) return r'{}'.format(r.text)
def object_count(self): """Return the object count and size of a given container. The returned object is (<count>, <size>) Size is returned in bytes. :returns: tuple """ container = self.conn.get_container(name=self.container) log.info('container information: %s' % container) return int(container['X-Container-Object-Count']), int(container['X-Container-Bytes-Used'])
def index(): urlform = forms.UrlForm() pasteform = forms.PasteForm() obj_count, obj_total_size = backend.count() log.info('object count %s' % obj_count) return flask.render_template( 'index.html', urlform=urlform, pasteform=pasteform, obj_count=obj_count, obj_total_size=round((obj_total_size / 1024 / 1024), 3) )
def read(key): """Read the content from the CDN. :param key: index item. :type key: str """ with LocalCache() as c: content = c.get(key) if not content: r = requests.get(remote_url(key)) if r.status_code == requests.codes.ok: log.info('Retrieved paste from CDN', key=key) return r'{}'.format(r.text) else: log.info('Read object from cache', key=key) return content.decode("utf-8")
def count(container=None): """Read the content from the CDN. :param container: Name of the CDN container to count. :type container: str :returns: int """ with LocalCache() as c: object_count = c.get(b'object_count') if not object_count: log.info('No valid count object cached.') else: log.info('Cached object returned, count: %s.' % object_count) total_size = c.get(b'total_size') if not total_size: log.info('No valid size object cached.') else: log.info('Cached object returned, size: %s.' % total_size) if not all([object_count, total_size]): object_count, total_size = cdn.count(container=container) log.info('Count object cached') c.set( b'object_count', object_count, expire=900 ) log.info('Size object cached') c.set( b'total_size', total_size, expire=900 ) return object_count, total_size