def download_version(request, version_id, view_file=True): version = get_object_or_404(Version, id=version_id).resolve_original() item = version.item storage = Storage(settings.BACKTRAC_BACKUP_ROOT) f = storage.get(item.client.hostname, item.path, version.id) mimetype = get_mimetype(f) contents = f.read() #TODO: Fix this so we don't have to load the file into memory first #response = HttpResponse(FileWrapper(f)) response = HttpResponse(contents) if view_file and mimetype.startswith('text/'): response['Content-Disposition'] = 'inline' response['Content-Transfer-Encoding'] = 'binary' response['Content-Type'] = 'text/plain' else: response['Content-Disposition'] = '%sfilename=%s' % \ ('attachment; ' if not view_file else '', item.name) response['Content-Length'] = len(contents) response['Content-Type'] = mimetype return response
def dashboard(request, *args, **kwargs): storage = Storage(settings.BACKTRAC_BACKUP_ROOT) size = storage.get_total_bytes() used = storage.get_used_bytes() used_pc = int(float(used) / float(size) * 100) catalog_size = Version.objects.aggregate(size=Sum('size'))['size'] size_history = get_catalog_graph_data() connected_clients = Client.objects.filter(status__connected=True).count() num_clients = Client.objects.filter().count() events = Event.objects.select_related()[:10] stats = { 'size': size, 'used': used, 'used_pc': used_pc, 'catalog_size': catalog_size, 'size_history': size_history, } kwargs.update({ 'template': 'dashboard.html', 'extra_context': { 'stats': stats, 'events': events, 'server_online': cache.get(SERVER_STATUS_CACHE_KEY, False), 'connected_clients': connected_clients, 'num_clients': num_clients, }, }) return direct_to_template(request, *args, **kwargs)
def setUp(self): """ Create a test user, item and version, then put a file into the storage system for that version. """ User.objects.create_user('test', '*****@*****.**', 'test') self.client_obj = Client.objects.create(hostname='test', secret_key='') self.item, _ = get_or_create_item(self.client_obj, '/test/item', 'f') self.version = Version.objects.create(id=str(uuid.uuid4()), item=self.item, mtime=123, size=456) self.storage = Storage(settings.BACKTRAC_BACKUP_ROOT) _, fd = self.storage.put(self.client_obj.hostname, self.item.path, self.version.id) fd.write(self.FILE_CONTENTS) fd.close()
class DownloadVersionTest(TestCase): FILE_CONTENTS = "This is a test file" def setUp(self): """ Create a test user, item and version, then put a file into the storage system for that version. """ User.objects.create_user('test', '*****@*****.**', 'test') self.client_obj = Client.objects.create(hostname='test', secret_key='') self.item, _ = get_or_create_item(self.client_obj, '/test/item', 'f') self.version = Version.objects.create(id=str(uuid.uuid4()), item=self.item, mtime=123, size=456) self.storage = Storage(settings.BACKTRAC_BACKUP_ROOT) _, fd = self.storage.put(self.client_obj.hostname, self.item.path, self.version.id) fd.write(self.FILE_CONTENTS) fd.close() def test_download_version(self): """ Test that the download_version view returns the correct file from the storage subsystem. """ self.client.login(username='******', password='******') response = self.client.get(reverse('catalog_download_version', args=[self.version.id])) self.assertEqual(response.status_code, 200) self.assertEqual(response.content, self.FILE_CONTENTS) def tearDown(self): """ Remove the test storage directory we created earlier. """ shutil.rmtree(settings.BACKTRAC_BACKUP_ROOT)