Exemplo n.º 1
0
def download_zip(request):
    song = get_object_or_404(Song, pk=get_val(request.GET, 'song', 0))

    if not song.permission_to_view_source(request.user):
        raise Http403

    wanted_samples = request.GET.getlist('s')
    if len(wanted_samples) == 0:
        want_project = True
        deps = SampleDependency.objects.filter(song=song).exclude(uploaded_sample=None)
    else:
        want_project = False

        wanted_sample_ids = []
        
        for wanted_sample in wanted_samples:
            if wanted_sample == 'project':
                want_project = True
                continue
            try:
                wanted_sample_id = int(wanted_sample)
            except ValueError:
                continue

            wanted_sample_ids.append(wanted_sample_id)

        deps = SampleDependency.objects.filter(pk__in=wanted_sample_ids, song=song).exclude(uploaded_sample=None)

    zip_file_h = make_timed_temp_file()
    z = zipfile.ZipFile(zip_file_h, mode='w', compression=zipfile.ZIP_DEFLATED, allowZip64=True)

    import storage

    def store_file_id(file_id, title):
        tmp = tempfile.NamedTemporaryFile(mode='r+b')
        storage.engine.retrieve(file_id, tmp.name)
        z.write(tmp.name, title)
        tmp.close()
        
    if want_project:
        _path, title = os.path.split(song.source_file)
        store_file_id(song.source_file, title)

    for dep in deps:
        file_id = dep.uploaded_sample.sample_file.path
        store_file_id(file_id, dep.title)

    z.close()

    response = HttpResponse(FileWrapper(zip_file_h), mimetype='application/zip')
    response['Content-Disposition'] = 'attachment; filename="%s"' % clean_filename(song.displayString() + '.zip')
    response['Content-Length'] = zip_file_h.tell()

    zip_file_h.seek(0)
    return response
Exemplo n.º 2
0
def download_sample_zip(request):
    """
    when someone uploads samples as a version, there is an option to "Download all as .zip"
    which is this view.
    """
    sample_id_strs = request.GET.getlist('s')

    sample_ids = []
    for sample_id_str in sample_id_strs:
        try:
            sample_id = int(sample_id_str)
        except ValueError:
            continue
        sample_ids.append(sample_id)

    samples = UploadedSample.objects.filter(pk__in=sample_ids)

    # authorize
    for sample in samples:
        if sample.user.id != request.user.id and not sample.band.permission_to_view_source(
                request.user):
            raise Http403

    # package as zip
    zip_file_h = make_timed_temp_file()
    z = zipfile.ZipFile(zip_file_h,
                        mode='w',
                        compression=zipfile.ZIP_DEFLATED,
                        allowZip64=True)

    import storage

    def store_file_id(file_id, title):
        tmp = tempfile.NamedTemporaryFile(mode='r+b')
        storage.engine.retrieve(file_id, tmp.name)
        z.write(tmp.name, title)
        tmp.close()

    for sample in samples:
        file_id = sample.sample_file.path
        store_file_id(file_id, sample.title)

    z.close()

    response = HttpResponse(FileWrapper(zip_file_h),
                            mimetype='application/zip')
    response['Content-Disposition'] = 'attachment; filename="samples.zip"'
    response['Content-Length'] = zip_file_h.tell()

    zip_file_h.seek(0)
    return response
Exemplo n.º 3
0
def download_sample_zip(request):
    """
    when someone uploads samples as a version, there is an option to "Download all as .zip"
    which is this view.
    """
    sample_id_strs = request.GET.getlist('s')
    
    sample_ids = []
    for sample_id_str in sample_id_strs:
        try:
            sample_id = int(sample_id_str)
        except ValueError:
            continue
        sample_ids.append(sample_id)

    samples = UploadedSample.objects.filter(pk__in=sample_ids)

    # authorize
    for sample in samples:
        if sample.user.id != request.user.id and not sample.band.permission_to_view_source(request.user):
            raise Http403

    # package as zip
    zip_file_h = make_timed_temp_file()
    z = zipfile.ZipFile(zip_file_h, mode='w', compression=zipfile.ZIP_DEFLATED, allowZip64=True)

    import storage

    def store_file_id(file_id, title):
        tmp = tempfile.NamedTemporaryFile(mode='r+b')
        storage.engine.retrieve(file_id, tmp.name)
        z.write(tmp.name, title)
        tmp.close()

    for sample in samples:
        file_id = sample.sample_file.path
        store_file_id(file_id, sample.title)

    z.close()
            
    response = HttpResponse(FileWrapper(zip_file_h), mimetype='application/zip')
    response['Content-Disposition'] = 'attachment; filename="samples.zip"'
    response['Content-Length'] = zip_file_h.tell()

    zip_file_h.seek(0)
    return response
Exemplo n.º 4
0
def download_sample(request, sample_id_str, sample_title):
    sample_id = int(sample_id_str)
    sample = get_object_or_404(UploadedSample, pk=sample_id)

    if sample.user.id != request.user.id and not sample.band.permission_to_view_source(request.user):
        # not authorized
        raise Http403

    # grab the sample from storage to temp file
    import storage

    sample_file_h = make_timed_temp_file()
    storage.engine.retrieve(sample.sample_file.path, sample_file_h.name)

    # return to browser
    response = HttpResponse(FileWrapper(sample_file_h), mimetype='application/octet-stream')
    response['Content-Disposition'] = 'attachment; filename="%s"' % sample_title
    response['Content-Length'] = os.path.getsize(sample_file_h.name)

    sample_file_h.seek(0)
    return response
Exemplo n.º 5
0
def download_sample(request, sample_id_str, sample_title):
    sample_id = int(sample_id_str)
    sample = get_object_or_404(UploadedSample, pk=sample_id)

    if sample.user.id != request.user.id and not sample.band.permission_to_view_source(
            request.user):
        # not authorized
        raise Http403

    # grab the sample from storage to temp file
    import storage

    sample_file_h = make_timed_temp_file()
    storage.engine.retrieve(sample.sample_file.path, sample_file_h.name)

    # return to browser
    response = HttpResponse(FileWrapper(sample_file_h),
                            mimetype='application/octet-stream')
    response[
        'Content-Disposition'] = 'attachment; filename="%s"' % sample_title
    response['Content-Length'] = os.path.getsize(sample_file_h.name)

    sample_file_h.seek(0)
    return response
Exemplo n.º 6
0
def download_zip(request):
    song = get_object_or_404(Song, pk=get_val(request.GET, 'song', 0))

    if not song.permission_to_view_source(request.user):
        raise Http403

    wanted_samples = request.GET.getlist('s')
    if len(wanted_samples) == 0:
        want_project = True
        deps = SampleDependency.objects.filter(song=song).exclude(
            uploaded_sample=None)
    else:
        want_project = False

        wanted_sample_ids = []

        for wanted_sample in wanted_samples:
            if wanted_sample == 'project':
                want_project = True
                continue
            try:
                wanted_sample_id = int(wanted_sample)
            except ValueError:
                continue

            wanted_sample_ids.append(wanted_sample_id)

        deps = SampleDependency.objects.filter(
            pk__in=wanted_sample_ids, song=song).exclude(uploaded_sample=None)

    zip_file_h = make_timed_temp_file()
    z = zipfile.ZipFile(zip_file_h,
                        mode='w',
                        compression=zipfile.ZIP_DEFLATED,
                        allowZip64=True)

    import storage

    def store_file_id(file_id, title):
        tmp = tempfile.NamedTemporaryFile(mode='r+b')
        storage.engine.retrieve(file_id, tmp.name)
        z.write(tmp.name, title)
        tmp.close()

    if want_project:
        _path, title = os.path.split(song.source_file)
        store_file_id(song.source_file, title)

    for dep in deps:
        file_id = dep.uploaded_sample.sample_file.path
        store_file_id(file_id, dep.title)

    z.close()

    response = HttpResponse(FileWrapper(zip_file_h),
                            mimetype='application/zip')
    response[
        'Content-Disposition'] = 'attachment; filename="%s"' % clean_filename(
            song.displayString() + '.zip')
    response['Content-Length'] = zip_file_h.tell()

    zip_file_h.seek(0)
    return response