def download_file(request, name, pk, filename): """ If the requested file is not already cached locally from a previous download it will be fetched from PyPi for local storage and the client will be redirected to PyPi, unless the LOCALSHOP_ISOLATED variable is set to True, in wich case the file will be served to the client after it is downloaded. """ release_file = models.ReleaseFile.objects.get(pk=pk) if not release_file.distribution: logger.info("Queueing %s for mirroring", release_file.url) release_file_notfound.send(sender=release_file.__class__, release_file=release_file) if not settings.LOCALSHOP_ISOLATED: logger.debug("Redirecting user to pypi") return redirect(release_file.url) else: release_file = models.ReleaseFile.objects.get(pk=pk) # TODO: Use sendfile if enabled response = HttpResponse( FileWrapper(release_file.distribution.file), content_type='application/force-download') response['Content-Disposition'] = 'attachment; filename=%s' % ( release_file.filename) size = release_file.distribution.file.size if size: response["Content-Length"] = size return response
def download_file(request, name, pk, filename): """Redirect the client to the pypi hosted file if the file is not mirror'ed yet (and isn't a local package). Otherwise serve the file. """ release_file = models.ReleaseFile.objects.get(pk=pk) if not release_file.distribution: logger.info("Queueing %s for mirroring", release_file.url) release_file_notfound.send(sender=release_file.__class__, release_file=release_file) return redirect(release_file.url) # TODO: Use sendfile if enabled response = HttpResponse(release_file.distribution.file, content_type='application/force-download') response['Content-Disposition'] = 'attachment; filename=%s' % ( release_file.filename) if release_file.distribution.file.size: response["Content-Length"] = release_file.distribution.file.size return response