#coding: utf-8
from django.db import models
from django.contrib.sites.models import Site
from django.conf import settings
import subprocess
import logging
from os import path

log = logging.getLogger(__name__)

class Upload(models.Model):
    """
    uploader: the name of the uploader (optional)
    name: name of the file, or the root folder, for a multi-file torrent
    uuid: unique identifier of the torrent
    date: date of the upload
    password: a password to delete the torrent (optional)
    description: a short description of the torrent
    size: size of the file/folder
    multifile: whether the torrent contains several files or not
    magnet: magnet link of the torrent
    """
    uploader = models.CharField(max_length=32, default="Anonymous")
    name = models.CharField(max_length=128)
    uuid = models.CharField(max_length=36, primary_key=True)
    date = models.DateField(auto_now_add = True)
    password = models.CharField(max_length=32, default="", blank=True)
    description = models.CharField(max_length=400, default="", blank=True)
    size = models.CharField(max_length=8)
    multifile = models.BooleanField(default=False, blank=True)
    magnet = models.TextField(default="")
    private = models.BooleanField(default=False, blank=True)

    def __unicode__(self):
        return u'%s - %s/%s - %s' % (self.uploader, self.uuid, self.name, self.date)

    def get_absolute_url(self):
        return '/view/%s/' % self.uuid

    def get_torrent_file(self):
        return '%s%s/%s.torrent' % (settings.MEDIA_URL, self.uuid, self.name)

    def get_direct_torrent_file(self):
        return "http://%s%s" % (
                Site.objects.get_current().domain, self.get_torrent_file())

    def get_file(self):
        return '%s%s/%s' % (settings.MEDIA_URL, self.uuid, self.name)

    def get_direct_file(self):
        return "http://%s%s" % (
                Site.objects.get_current().domain, self.get_file())

    def get_files(self):
        dir = path.join(settings.MEDIA_ROOT, self.uuid, self.name)
        try:
            proc = subprocess.Popen(['tree', '-ah', dir], stdout=subprocess.PIPE)
            res = proc.communicate()[0]
        except OSError:
            res = ''
            log.info('Call to `tree` failed.')
        tb = res.decode('utf-8').split('\n')
        return '\n'.join(tb)

@@ -2210,16 +2210,17 @@
   res = 
+b
 ''%0A     

remove an error when tree isn’t available