允许用户播放 mp3 文件,但不要将其直接公开在网络上

发布于 2024-09-08 13:05:17 字数 127 浏览 5 评论 0原文

我想将一些 mp3 存储在一个非公开的文件夹中,无法通过网络直接访问,并且仅允许用户在登录时使用浏览器收听/下载歌曲。

我该怎么做?

我使用 Django 进行 Web 开发,但如果我知道它是如何工作的就足够了。

I want to store some mp3s in a folder which is not public, can't be directly accessed through the web and allow users to hear/download the songs with a browser only if they are logged in.

How can I do that?

I do my web development with Django, but if I know how it works is enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

も星光 2024-09-15 13:05:17

您首先需要设置身份验证。 django 教程对此进行了深入探讨。

您不直接链接 mp3,而是链接到检查身份验证的 django 脚本,然后读取 mp3 并将其提供给具有 mp3 内容类型标头的客户端。

http://yourserver.com/listen?file=Fat+Boys+Greatest+Hits

You first need to setup authentication. The django tutorials thoroughly explore this.

You don't' link the mp3's directly, You link to a django script that checks the auth, then reads the mp3 and serves it to the client with a mp3 content type header.

http://yourserver.com/listen?file=Fat+Boys+Greatest+Hits

长不大的小祸害 2024-09-15 13:05:17

我假设你使用 django。然后你可以尝试这样的事情:

from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse


@login_required
def listen(request, file_name):
    # note that MP3_STORAGE should not be in MEDIA_ROOT
    file = open("%smp3/%s" % (settings.MP3_STORAGE, file_name))
    response = HttpResponse(file.read(), mimetype="audio/mpeg")
    return response

请注意,你的速度会急剧下降。使用生成器分块读取文件可能有助于节省内存。

Python 中读取大文件的惰性方法?

I assume you use django. Then you can try something like this:

from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse


@login_required
def listen(request, file_name):
    # note that MP3_STORAGE should not be in MEDIA_ROOT
    file = open("%smp3/%s" % (settings.MP3_STORAGE, file_name))
    response = HttpResponse(file.read(), mimetype="audio/mpeg")
    return response

Note that you will get dramatic speed decrease. Using generator to read file in blocks may help to save memory.

Lazy Method for Reading Big File in Python?

伤痕我心 2024-09-15 13:05:17
  • 文件不在公共访问范围内(不在
    MEDIA_URL 文件夹)
  • 检查用户是否登录
  • 仅通过视图提供文件
    每个用户的唯一链接

伪代码:

class Mp3(models.Model):
    file = models.FileField(upload_to=path_outside_of_public_access)
    hash = models.CharField(unique=True)


def generate_link_hash(request, file):
    return hashlib.md5("%s_%i_%s_%s" % (request.session.session_key, file.id, str(file.date_added), file.hash)) # or however u like


def files_list(request)
    """ view to show files list """
    for file in files:
        file.link_hash = generate_link_hash(request, file)


@login_required
def download_file(request, file_hash, link_hash):
    """ view to download file """
    file = Mp3.objects.get(hash=file_hash)
    if link_hash == generate_link_hash(request, file):
        file = open(file.file)
        return HttpResponse(file.read(), mimetype="audio/mpeg")
    else:
        raise Http404

应该足以完成这项工作,但请记住 - 一旦访问了什么,您就无法控制它从现在开始的去向。并且每个文件下载都需要通过应用程序读取文件(不是静态给出的),这会影响应用程序的性能。

  • File outside of public access (not in
    MEDIA_URL folders)
  • Check if user logged in
  • Serve files only via a view, with
    unique links for every user

Pseudocode:

class Mp3(models.Model):
    file = models.FileField(upload_to=path_outside_of_public_access)
    hash = models.CharField(unique=True)


def generate_link_hash(request, file):
    return hashlib.md5("%s_%i_%s_%s" % (request.session.session_key, file.id, str(file.date_added), file.hash)) # or however u like


def files_list(request)
    """ view to show files list """
    for file in files:
        file.link_hash = generate_link_hash(request, file)


@login_required
def download_file(request, file_hash, link_hash):
    """ view to download file """
    file = Mp3.objects.get(hash=file_hash)
    if link_hash == generate_link_hash(request, file):
        file = open(file.file)
        return HttpResponse(file.read(), mimetype="audio/mpeg")
    else:
        raise Http404

Should do the job enough, but remember - what is once accessed, you have no control where it goes from now on. And that every file download needs reading the file through the app (it's not given statically), which will affect the performance of your app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文