def download_media(): id = request.values["id"] uid = uuid.UUID(id) try: # Track -> direct download rv = Track[uid] return send_file(rv.path, mimetype=rv.mimetype, conditional=True) except ObjectNotFound: pass try: # Folder -> stream zipped tracks, non recursive rv = Folder[uid] except ObjectNotFound: try: # Album -> stream zipped tracks rv = Album[uid] except ObjectNotFound: raise NotFound("Track, Folder or Album") z = ZipFile(compression=ZIP_DEFLATED) for track in rv.tracks: z.write(track.path, os.path.basename(track.path)) resp = Response(z, mimetype="application/zip") resp.headers["Content-Disposition"] = "attachment; filename={}.zip".format( rv.name) return resp
def stream_generator(descriptor_entry): rpipe, wpipe = os.pipe() pid = os.fork() if not pid: os.close(rpipe) zs = ZipFile(mode='w', compression=ZIP_DEFLATED) shrink_part = getattr(descriptor_entry.parent, 'relative', '') for fname in recursive_list(descriptor_entry): zip_path = (shrink(fname.relative, shrink_part) if shrink_part != '.' else fname.relative) zs.write(fname.absolute, zip_path) for chunk in zs: try: _ = os.write(wpipe, chunk) except (BrokenPipeError, IOError): break os.close(wpipe) return os.close(wpipe) try: while True: r, _, _ = select.select([rpipe], [], [], 15) if not r: break chunk = os.read(rpipe, 4096) if not chunk: break yield chunk finally: # f**k the zombies! os.close(rpipe) os.wait()
def download_media(): id = request.values["id"] try: uid = get_entity_id(Track, id) except GenericError: uid = None try: fid = get_entity_id(Folder, id) except GenericError: fid = None if uid is None and fid is None: raise GenericError("Invalid ID") if uid is not None: try: rv = Track[uid] return send_file(rv.path, mimetype=rv.mimetype, conditional=True) except ObjectNotFound: try: # Album -> stream zipped tracks rv = Album[uid] except ObjectNotFound: raise NotFound("Track or Album") else: try: # Folder -> stream zipped tracks, non recursive rv = Folder[fid] except ObjectNotFound: raise NotFound("Folder") z = ZipFile(compression=ZIP_DEFLATED) for track in rv.tracks: z.write(track.path, os.path.basename(track.path)) resp = Response(z, mimetype="application/zip") resp.headers["Content-Disposition"] = "attachment; filename={}.zip".format( rv.name) return resp
def download_media(): id = request.values['id'] uid = uuid.UUID(id) try: # Track -> direct download rv = Track[uid] return send_file(rv.path, mimetype = rv.mimetype, conditional=True) except ObjectNotFound: pass try: # Folder -> stream zipped tracks, non recursive rv = Folder[uid] except ObjectNotFound: try: # Album -> stream zipped tracks rv = Album[uid] except ObjectNotFound: raise NotFound('Track, Folder or Album') z = ZipFile(compression = ZIP_DEFLATED) for track in rv.tracks: z.write(track.path, os.path.basename(track.path)) resp = Response(z, mimetype = 'application/zip') resp.headers['Content-Disposition'] = 'attachment; filename={}.zip'.format(rv.name) return resp