Yes, it would be ok to allow django to generate urls. This excludes URL handling from urls.py. As a rule, you donβt want django to handle a portion of files, see Static docs [1] for more on this, so get an idea of ββusing url patterns from the head.
What you can do is generate a random key using a hash like md5 / sha1 for example. Save the file and key, date and time added to the database, create the download directory in the root directory accessible from your web server, for example apache or nginx ... suggest nginx). Since it is temporary, you need to add a cron job that checks if the time has passed since the url was created, clears the file and deletes the db entry. This should be the django command for manage.py
, , ! , , , . , dl pw, httpbasic auth. " " httpd.auth, htpasswd .
import hashlib, random, datetime, os, shutil
from models import MyDlLink
from myapp import settings as myapp_settings
def genUrl(filepath):
salt = ''.join(['{0}'.format(random.randrange(10) for i in range(10)])
key = hashlib('{0}{1}'.format(salt, filepath).hexdigest()
newpath = os.path.join(myapp_settings.DL_ROOT, key)
shutil.copy2(fname, newpath)
newlink = MyDlink()
newlink.key = key
newlink.date = datetime.datetime.now()
newlink.orgpath = filepath
newlink.newpath = newpath
newlink.url = "{0}/{1}/{2}".format(myapp_settings.DL_URL, key, os.path.basename(fname))
newlink.save()
return newlink
def check_url_expired():
maxage = datetime.timedelta(days=7)
now = datetime.datetime.now()
for link in MyDlink.objects.all():
if(now - link.date) > maxage:
os.path.remove(link.newpath)
link.delete()
[1] http://docs.djangoproject.com/en/1.2/howto/static-files/