def get(self, request, *args, **kwargs): """ Stream a ZIP file of collection data without loading the whole file into memory. Based on ZipStream """ from telemeta.views import MarkerView from telemeta.backup import CollectionSerializer import zipstream from zipfile import ZIP_DEFLATED, ZIP_STORED import json zip_file = zipstream.ZipFile(mode='w', compression=ZIP_STORED, allowZip64=True) cache_data = TelemetaCache(settings.TELEMETA_DATA_CACHE_DIR) collection = self.get_object() serializer = CollectionSerializer(collection) data = collection.get_json().encode('utf-8') filename = collection.public_id + '.json' cache_data.write_bin(data, filename) path = cache_data.dir + os.sep + filename zip_file.write(path, arcname=collection.public_id + os.sep + filename) data = serializer.get_xml().encode('utf-8') filename = collection.public_id + '.xml' cache_data.write_bin(data, filename) path = cache_data.dir + os.sep + filename zip_file.write(path, arcname=collection.public_id + os.sep + filename) for item in collection.items.all(): if item.file: filename, ext = os.path.splitext( item.file.path.split(os.sep)[-1]) zip_file.write(item.file.path, arcname=collection.public_id + os.sep + item.code + ext) marker_view = MarkerView() markers = marker_view.get_markers(item.id) if markers: data = json.dumps(markers) filename = item.code + '.json' cache_data.write_bin(data, filename) path = cache_data.dir + os.sep + filename zip_file.write(path, arcname=collection.public_id + os.sep + filename) response = StreamingHttpResponse(zip_file, content_type='application/zip') response['Content-Disposition'] = "attachment; filename=%s.%s" % \ (collection.code, 'zip') return response
def get(self, request, *args, **kwargs): """ Stream a ZIP file of collection data without loading the whole file into memory. Based on ZipStream """ from telemeta.views import MarkerView from telemeta.backup import CollectionSerializer import zipstream from zipfile import ZIP_DEFLATED, ZIP_STORED import json zip_file = zipstream.ZipFile(mode='w', compression=ZIP_STORED, allowZip64=True) cache_data = TelemetaCache(settings.TELEMETA_DATA_CACHE_DIR) collection = self.get_object() serializer = CollectionSerializer(collection) data = collection.get_json().encode('utf-8') filename = collection.public_id + '.json' cache_data.write_bin(data, filename) path = cache_data.dir + os.sep + filename zip_file.write(path, arcname=collection.public_id + os.sep + filename) data = serializer.get_xml().encode('utf-8') filename = collection.public_id + '.xml' cache_data.write_bin(data, filename) path = cache_data.dir + os.sep + filename zip_file.write(path, arcname=collection.public_id + os.sep + filename) for item in collection.items.all(): if item.file: filename, ext = os.path.splitext(item.file.path.split(os.sep)[-1]) zip_file.write(item.file.path, arcname=collection.public_id + os.sep + item.code + ext) marker_view = MarkerView() markers = marker_view.get_markers(item.id) if markers: data = json.dumps(markers) filename = item.code + '.json' cache_data.write_bin(data, filename) path = cache_data.dir + os.sep + filename zip_file.write(path, arcname=collection.public_id + os.sep + filename) response = StreamingHttpResponse(zip_file, content_type='application/zip') response['Content-Disposition'] = "attachment; filename=%s.%s" % \ (collection.code, 'zip') return response
def get(self, request, *args, **kwargs): """ Stream a ZIP file of collection data without loading the whole file into memory. Based on ZipStream """ from telemeta.views import MarkerView from telemeta.backup import CollectionSerializer from telemeta.util import zipstream import json z = zipstream.ZipFile() cache_data = TelemetaCache(settings.TELEMETA_DATA_CACHE_DIR) collection = self.get_object() serializer = CollectionSerializer(collection) data = serializer.get_xml().encode("utf-8") filename = collection.public_id + '.xml' cache_data.write_bin(data, filename) path = cache_data.dir + os.sep + filename z.write(path, arcname=collection.public_id + os.sep + filename) for item in collection.items.all(): filename, ext = os.path.splitext(item.file.path.split(os.sep)[-1]) z.write(item.file.path, arcname=collection.public_id + os.sep + item.code + ext) marker_view = MarkerView() markers = marker_view.get_markers(item.id) if markers: data = json.dumps(markers) filename = item.code + '.json' cache_data.write_bin(data, filename) path = cache_data.dir + os.sep + filename z.write(path, arcname=collection.public_id + os.sep + filename) try: from django.http import StreamingHttpResponse response = StreamingHttpResponse(z, content_type='application/zip') except: response = HttpResponse(z, content_type='application/zip') response['Content-Disposition'] = "attachment; filename=%s.%s" % \ (item.code, 'zip') return response
def get(self, request, *args, **kwargs): """ Stream a ZIP file of collection data without loading the whole file into memory. Based on ZipStream """ from telemeta.views import MarkerView from telemeta.backup import CollectionSerializer import json import zipstream z = zipstream.ZipFile() cache_data = TelemetaCache(settings.TELEMETA_DATA_CACHE_DIR) collection = self.get_object() serializer = CollectionSerializer(collection) data = serializer.get_xml().encode("utf-8") filename = collection.public_id + '.xml' cache_data.write_bin(data, filename) path = cache_data.dir + os.sep + filename z.write(path, arcname=collection.public_id + os.sep + filename) for item in collection.items.all(): filename = item.file.path.split(os.sep)[-1] z.write(item.file.path, arcname=collection.public_id + os.sep + filename) marker_view = MarkerView() markers = marker_view.get_markers(item.id) if markers: data = json.dumps(markers) filename = item.code + '.json' cache_data.write_bin(data, filename) path = cache_data.dir + os.sep + filename z.write(path, arcname=collection.public_id + os.sep + filename) try: from django.http import StreamingHttpResponse response = StreamingHttpResponse(z, content_type='application/zip') except: response = HttpResponse(z, content_type='application/zip') response['Content-Disposition'] = "attachment; filename=%s.%s" % \ (item.code, 'zip') return response
def backup(dest_dir): from telemeta.models import MediaCollection from telemeta.backup import CollectionSerializer collections = MediaCollection.objects.order_by("id") count = collections.count() print "Writing README file..", write_readme(dest_dir, count) print "Done." i = 0 for collection in collections: if i % 100 == 0: set_dir = dest_dir + ("/collections-%d-%d" % (i + 1, i + 100)) os.mkdir(set_dir) i += 1 print "Processing collection %d/%d (%d%%) with id: %s.. " % (i, count, i * 100 / count, collection.id), sys.stdout.flush() serializer = CollectionSerializer(collection) serializer.backup(set_dir) print "Done"
def backup(dest_dir): from telemeta.models import MediaCollection from telemeta.backup import CollectionSerializer collections = MediaCollection.objects.order_by('id') count = collections.count() print "Writing README file..", write_readme(dest_dir, count) print "Done." i = 0 for collection in collections: if i % 100 == 0: set_dir = dest_dir + ("/collections-%d-%d" % (i + 1, i + 100)) os.mkdir(set_dir) i += 1 print "Processing collection %d/%d (%d%%) with id: %s.. " \ % (i, count, i*100/count, collection.id), sys.stdout.flush() serializer = CollectionSerializer(collection) serializer.store(set_dir) print "Done"