Esempio n. 1
0
def get_file(fn):
    # request_uri = fn.split("/",1)[-1]
    fsize = None
    exists = False
    mime_type = ''

    request_uri = fn.replace(STATIC_FILES_DIR + "/", "")
    print request_uri
    if request_uri.startswith(atp["static_path"]):
        torrent = atp
        chosen_file = "/".join(request_uri.split("/")[2:])
        print "chosen file:" + chosen_file
        url = "magnet:?xt=urn:btih:" + request_uri.split("/")[1]
        info_hash = libtorrent.parse_magnet_uri(url)["info_hash"]
        handle = torrent_session.find_torrent(info_hash)
        # print str(info_hash)+".resume", os.listdir("resume")
        if handle.is_valid():
            print "Magnet already in the session"
            torrent_handle = handle
        # elif str(info_hash)+".resume" in os.listdir("resume"):
        #     torrent["resume_data"] = io.open("resume/"+str(info_hash)+".resume", "rb").read()
        #     torrent_handle = torrent_session.add_torrent(torrent)
        else:
            print " start new torrent"
            torrent["url"] = url
            torrent["save_path"] = os.path.join(TORRENTS_DIR, str(info_hash))
            # torrent["paused"]=True
            torrent_handle = torrent_session.add_torrent(torrent)
            while not torrent_handle.has_metadata():
                sleep(0.5)

        info = torrent_handle.get_torrent_info()
        torrent_files = info.files()
        # torrent_handle.auto_managed(False)
        if chosen_file is not None:
            for i, file_info in enumerate(torrent_files):
                print "path:", file_info.path
                if chosen_file == file_info.path:
                    torrent_handle.file_priority(i, 1)
                    torrent_file = file_info
                    file_index = i
                else:
                    torrent_handle.file_priority(i, 0)
        torrent_handle.map_piece = lambda offset: info.map_file(
            file_index, offset, 1)

        path = os.path.join(TORRENTS_DIR, torrent_file.path)
        first_byte = torrent_file.offset
        last_byte = torrent_file.offset + torrent_file.size
        torrent_handle.set_sequential_download(True)
        first_piece = torrent_handle.map_piece(first_byte).piece
        last_piece = torrent_handle.map_piece(last_byte).piece
        num_pieces = info.num_pieces()
        future_pieces = int((last_piece - first_piece) * 0.01)
        print "offset", torrent_file.offset, "piece", first_piece, "total", num_pieces
        piece_priorities = first_piece * [0] + (num_pieces - first_piece) * [1]
        piece_priorities = piece_priorities[:num_pieces]
        # piece_priorities=first_piece*[0]+future_pieces*[7]+more_pieces*[1]+(num_pieces-first_piece-future_pieces-more_pieces)*[0]
        torrent_handle.prioritize_pieces(piece_priorities)
        # torrent_handle.prioritize_pieces(num_pieces*[0])
        # torrent_handle.piece_priority(first_piece,7)
        torrent_handle.piece_priority(last_piece, 7)
        torrent_handle.piece_priority(last_piece - 1, 7)
        # torrent_handle.num_pieces=info.num_pieces()
        piece = first_piece
        waiting = True

        while waiting:
            sleep(1)
            # if torrent_handle.have_piece(first_piece) and torrent_handle.have_piece(last_piece):
            #     waiting =False
            status = torrent_handle.status()
            pieces = status.pieces
            print pieces[piece:piece + future_pieces], last_piece, pieces[
                last_piece -
                1], status.progress, status.download_rate, status.state
            if pieces[piece:piece +
                      future_pieces] == future_pieces * [True] and pieces[
                          last_piece - 1]:
                waiting = False
        print "first and last piece received"
        # torrent_handle.prioritize_pieces(num_pieces*[1])
    else:
        torrent_handle = None
    try:
        fsize = os.path.getsize(fn)
        exists = True
        type, encoding = mimetypes.guess_type(request_uri)
        if type:
            mime_type = type
    except:
        pass

    return File(request_uri, fn, fsize, exists, mime_type, torrent_handle)
Esempio n. 2
0
def handle_request(clientsock, addr):

    data = clientsock.recv(RECV_BUFSIZ)

    #Log('Request received: %s' % data)

    request = parse_http_request(data)

    path = STATIC_FILES_DIR + clean_path(request.request_uri)
    request_uri=request.request_uri[1:]
    if request_uri.startswith("_omx"):
        req=request_uri.split("/")
        cmd=req[1]
        if cmd=="play":
            omxplayer.reset_tv()
            link=("/").join(req[2:])
            path=STATIC_FILES_DIR + "/"+clean_path(req[2])
            print path
            if os.path.isdir(path):
                link= "http://localhost/"+link
                playing_video=omxplayer.omxplayer(str(link))
            elif link.split(".").pop()  in omxplayer.FORMATS:
                playing_video=omxplayer.omxplayer(str(link))
            elif link.split(".").pop()  in omxplayer.FBI_FORMATS:
                shown_image= omxplayer.image(str(link))
            elif link.split(".").pop() in ["pdf"]:
                shown_pdf= omxplayer.pdf(str(link))
            else:
                link=omxplayer.ytdl(link)  
                playing_video=omxplayer.omxplayer(str(link))
            response = HttpResponse(protocol=request.protocol, status_code=200)
            response.write_to(clientsock)
            clientsock.close()


    # if request_uri.startswith("magnet") or request_uri.endswith(".torrent") or request_uri.startswith(atp["static_path"]):
    if request_uri.startswith("magnet") or request_uri.endswith(".torrent"):
        chosen_file=None
        torrent=atp
        
        print "req uri:"+request_uri
        if request_uri.startswith("magnet"):
            info_hash=libtorrent.parse_magnet_uri(request_uri)["info_hash"]
        elif request_uri.startswith(atp["static_path"]):
            chosen_file=urllib.unquote("/".join(request_uri.split("/")[2:]))
            print "chosen file:"+chosen_file
            request_uri="magnet:?xt=urn:btih:"+request_uri.split("/")[1]
            info_hash=libtorrent.parse_magnet_uri(request_uri)["info_hash"]
        elif request_uri.endswith(".torrent"):

            #get info hash
            pass
        handle=torrent_session.find_torrent(info_hash)

        if handle.is_valid():
            print "Magnet already in the session"
            torrent_handle=handle 
        elif (str(info_hash)+".resume") in os.listdir("resume"):
            torrent["resume_data"] = io.open("resume/"+str(info_hash)+".resume", "rb").read()
            torrent_handle = torrent_session.add_torrent(torrent)
        else:
            print " start new torrrent"
            torrent["url"]=request_uri
            # torrent["paused"]=True
            torrent["save_path"]=os.path.join(TORRENTS_DIR,str(info_hash))
            torrent_handle = torrent_session.add_torrent(torrent)
            print "getting metadata"
            while not torrent_handle.has_metadata():
                sleep(1)
        print "getting info"       
        info=torrent_handle.get_torrent_info()        
        torrent_files=info.files()
        torrent_handle.auto_managed(False)    
        if chosen_file is not None:
            for i,file_info in enumerate(torrent_files):
                print "path:",file_info.path 
                if chosen_file == file_info.path:

                    torrent_handle.file_priority(i,1)
                    file_index=i
                else:
                    torrent_handle.file_priority(i,0)    

            file = get_torrent_file(torrent_handle,file_index)

            if file.exists and request.is_range_requested():
                response = HttpResponse(protocol=request.protocol, status_code=206,
                                        range=request.get_range())
                response.file = file

            elif file.exists:
                response = HttpResponse(protocol=request.protocol, status_code=200)
                response.file = file
                Log('%s GET "%s" %s %s %s' %
                    (addr[0], request.request_uri, request.protocol, request.get_range(), response.status_code))

                response.write_to(clientsock)
                clientsock.close()    
         
        info=torrent_handle.get_torrent_info()
        name= info.name()
        # f = str()
        # f += '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">'
        # f += "<html>\n<title>Directory listing for %s</title>\n" % name
        # f +="<body>\n<h2>Directory listing for %s</h2>\n" % name
        # f += "<hr>\n<ul>\n"
        f={}
        for i,file_info in enumerate(torrent_files):
            f[i]={"link":"/"+os.path.join(atp["static_path"],str(info_hash),file_info.path),
                  "size":file_info.size,
                  "path":file_info.path
            }
            # f += '<li><a href="%s">%s</a><h6>%s</h6></li>\n' % (os.path.join(atp["static_path"],str(info_hash),file_info.path), file_info.path,file_info.size)
        # f += "</ul>\n<hr>\n</body>\n</html>\n"
        f=json.dumps(f)
        response = HttpResponse(protocol=request.protocol, status_code=200)
        response.headers['Content-type'] = 'application/json'
        response.headers['Content-Length'] = len(f)
        # response.headers['Accept-Ranges'] = 'bytes'
        response.content = f
        Log('%s GET "%s" %s %s %s' %
            (addr[0], request.request_uri, request.protocol, request.get_range(), response.status_code))
        response.write_to(clientsock)
        clientsock.close()
        return None

      
        
        # h.set_sequential_download(True)
        # while h.status().progress < 0.01:
        #     sleep(1)          
        # h.file_index=max_i  
        # h.offset=max_offset     
    
    # check if path is dir (copy from the SimpleHttpServer)
    path=STATIC_FILES_DIR + clean_path(request.request_uri)
    if os.path.isdir(path):
        print path
        if not path.endswith('/'):
            # redirect browser - doing basically what apache does
            response = HttpResponse(protocol=request.protocol, status_code=301)
            response.headers['Location'] = path + "/"
            Log('%s GET "%s" %s %s %s' %
                (addr[0], request.request_uri, request.protocol, request.get_range(), response.status_code))
            response.write_to(clientsock)
            clientsock.close()
            return None
        for index in "index.html", "index.htm":
            index = os.path.join(path, index)
            if os.path.exists(index):
                path = index
                break
        else:
            # quick and dirty but it works :P (also copy from SimpleHttpServer)
            try:
                list = os.listdir(path)
            except os.error:
                response = HttpResponse(protocol=request.protocol, status_code=404)
                response.headers['Content-type'] = 'text/plain'
                response.content = 'No permission to list directory'
                Log('%s GET "%s" %s %s %s' %
                    (addr[0], request.request_uri, request.protocol, request.get_range(), response.status_code))
                response.write_to(clientsock)
                clientsock.close()
                return None
            list.sort(key=lambda a: a.lower())
            f = str()
            displaypath = cgi.escape(urllib.unquote(request.request_uri))
            # f += '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">'
            # f += "<html>\n<title>Directory listing for %s</title>\n" % displaypath
            # f +="<body>\n<h2>Directory listing for %s</h2>\n" % displaypath
            f += "<hr>\n<ul>\n"
            for name in list:
                fullname = os.path.join(path, name)
                displayname = linkname = name
                # Append / for directories or @ for symbolic links
                if os.path.isdir(fullname):
                    displayname = name + "/"
                    linkname = name + "/"
                if os.path.islink(fullname):
                    displayname = name + "/" # "@"
                    # Note: a link to a directory displays with @ and links with /
                f += '<li><a href="%s">%s</a>\n' % (urllib.quote(linkname), cgi.escape(displayname))
            f += "</ul>\n<hr>\n</body>\n</html>\n"
            response = HttpResponse(protocol=request.protocol, status_code=200)
            response.headers['Content-type'] = 'text/html'
            response.headers['Content-Length'] = len(f)
            response.headers['Accept-Ranges'] = 'bytes'
            response.content = f
            Log('%s GET "%s" %s %s %s' %
                (addr[0], request.request_uri, request.protocol, request.get_range(), response.status_code))
            response.write_to(clientsock)
            clientsock.close()
            return None
    print urllib.unquote(request.request_uri)[1:]

    file = get_file(path)

    if file.exists and request.is_range_requested():
        response = HttpResponse(protocol=request.protocol, status_code=206,
                                range=request.get_range())
        response.headers["Content-Type"]=file.mime_type
        response.headers["Connection"]="Keep-Alive"
        response.file = file

    elif file.exists:
        response = HttpResponse(protocol=request.protocol, status_code=200)
        response.file = file

    else:
        response = HttpResponse(protocol=request.protocol, status_code=404)
        response.headers['Content-type'] = 'text/plain'
        response.content = 'This file does not exist!'

    Log('%s GET "%s" %s %s %s' %
        (addr[0], request.request_uri, request.protocol, request.get_range(), response.status_code))

    response.write_to(clientsock)
    clientsock.close()
Esempio n. 3
0
def handle_request(clientsock, addr):

    data = clientsock.recv(RECV_BUFSIZ)

    #Log('Request received: %s' % data)

    request = parse_http_request(data)

    path = STATIC_FILES_DIR + clean_path(request.request_uri)
    request_uri = request.request_uri[1:]
    if request_uri.startswith("_omx"):
        req = request_uri.split("/")
        cmd = req[1]
        if cmd == "play":
            omxplayer.reset_tv()
            link = ("/").join(req[2:])
            path = STATIC_FILES_DIR + "/" + clean_path(req[2])
            print path
            if os.path.isdir(path):
                link = "http://localhost/" + link
                playing_video = omxplayer.omxplayer(str(link))
            elif link.split(".").pop() in omxplayer.FORMATS:
                playing_video = omxplayer.omxplayer(str(link))
            elif link.split(".").pop() in omxplayer.FBI_FORMATS:
                shown_image = omxplayer.image(str(link))
            elif link.split(".").pop() in ["pdf"]:
                shown_pdf = omxplayer.pdf(str(link))
            else:
                link = omxplayer.ytdl(link)
                playing_video = omxplayer.omxplayer(str(link))
            response = HttpResponse(protocol=request.protocol, status_code=200)
            response.write_to(clientsock)
            clientsock.close()

    # if request_uri.startswith("magnet") or request_uri.endswith(".torrent") or request_uri.startswith(atp["static_path"]):
    if request_uri.startswith("magnet") or request_uri.endswith(".torrent"):
        chosen_file = None
        torrent = atp

        print "req uri:" + request_uri
        if request_uri.startswith("magnet"):
            info_hash = libtorrent.parse_magnet_uri(request_uri)["info_hash"]
        elif request_uri.startswith(atp["static_path"]):
            chosen_file = urllib.unquote("/".join(request_uri.split("/")[2:]))
            print "chosen file:" + chosen_file
            request_uri = "magnet:?xt=urn:btih:" + request_uri.split("/")[1]
            info_hash = libtorrent.parse_magnet_uri(request_uri)["info_hash"]
        elif request_uri.endswith(".torrent"):

            #get info hash
            pass
        handle = torrent_session.find_torrent(info_hash)

        if handle.is_valid():
            print "Magnet already in the session"
            torrent_handle = handle
        elif (str(info_hash) + ".resume") in os.listdir("resume"):
            torrent["resume_data"] = io.open(
                "resume/" + str(info_hash) + ".resume", "rb").read()
            torrent_handle = torrent_session.add_torrent(torrent)
        else:
            print " start new torrrent"
            torrent["url"] = request_uri
            # torrent["paused"]=True
            torrent["save_path"] = os.path.join(TORRENTS_DIR, str(info_hash))
            torrent_handle = torrent_session.add_torrent(torrent)
            print "getting metadata"
            while not torrent_handle.has_metadata():
                sleep(1)
        print "getting info"
        info = torrent_handle.get_torrent_info()
        torrent_files = info.files()
        torrent_handle.auto_managed(False)
        if chosen_file is not None:
            for i, file_info in enumerate(torrent_files):
                print "path:", file_info.path
                if chosen_file == file_info.path:

                    torrent_handle.file_priority(i, 1)
                    file_index = i
                else:
                    torrent_handle.file_priority(i, 0)

            file = get_torrent_file(torrent_handle, file_index)

            if file.exists and request.is_range_requested():
                response = HttpResponse(protocol=request.protocol,
                                        status_code=206,
                                        range=request.get_range())
                response.file = file

            elif file.exists:
                response = HttpResponse(protocol=request.protocol,
                                        status_code=200)
                response.file = file
                Log('%s GET "%s" %s %s %s' %
                    (addr[0], request.request_uri, request.protocol,
                     request.get_range(), response.status_code))

                response.write_to(clientsock)
                clientsock.close()

        info = torrent_handle.get_torrent_info()
        name = info.name()
        # f = str()
        # f += '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">'
        # f += "<html>\n<title>Directory listing for %s</title>\n" % name
        # f +="<body>\n<h2>Directory listing for %s</h2>\n" % name
        # f += "<hr>\n<ul>\n"
        f = {}
        for i, file_info in enumerate(torrent_files):
            f[i] = {
                "link":
                "/" + os.path.join(atp["static_path"], str(info_hash),
                                   file_info.path),
                "size":
                file_info.size,
                "path":
                file_info.path
            }
            # f += '<li><a href="%s">%s</a><h6>%s</h6></li>\n' % (os.path.join(atp["static_path"],str(info_hash),file_info.path), file_info.path,file_info.size)
        # f += "</ul>\n<hr>\n</body>\n</html>\n"
        f = json.dumps(f)
        response = HttpResponse(protocol=request.protocol, status_code=200)
        response.headers['Content-type'] = 'application/json'
        response.headers['Content-Length'] = len(f)
        # response.headers['Accept-Ranges'] = 'bytes'
        response.content = f
        Log('%s GET "%s" %s %s %s' %
            (addr[0], request.request_uri, request.protocol,
             request.get_range(), response.status_code))
        response.write_to(clientsock)
        clientsock.close()
        return None

        # h.set_sequential_download(True)
        # while h.status().progress < 0.01:
        #     sleep(1)
        # h.file_index=max_i
        # h.offset=max_offset

    # check if path is dir (copy from the SimpleHttpServer)
    path = STATIC_FILES_DIR + clean_path(request.request_uri)
    if os.path.isdir(path):
        print path
        if not path.endswith('/'):
            # redirect browser - doing basically what apache does
            response = HttpResponse(protocol=request.protocol, status_code=301)
            response.headers['Location'] = path + "/"
            Log('%s GET "%s" %s %s %s' %
                (addr[0], request.request_uri, request.protocol,
                 request.get_range(), response.status_code))
            response.write_to(clientsock)
            clientsock.close()
            return None
        for index in "index.html", "index.htm":
            index = os.path.join(path, index)
            if os.path.exists(index):
                path = index
                break
        else:
            # quick and dirty but it works :P (also copy from SimpleHttpServer)
            try:
                list = os.listdir(path)
            except os.error:
                response = HttpResponse(protocol=request.protocol,
                                        status_code=404)
                response.headers['Content-type'] = 'text/plain'
                response.content = 'No permission to list directory'
                Log('%s GET "%s" %s %s %s' %
                    (addr[0], request.request_uri, request.protocol,
                     request.get_range(), response.status_code))
                response.write_to(clientsock)
                clientsock.close()
                return None
            list.sort(key=lambda a: a.lower())
            f = str()
            displaypath = cgi.escape(urllib.unquote(request.request_uri))
            # f += '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">'
            # f += "<html>\n<title>Directory listing for %s</title>\n" % displaypath
            # f +="<body>\n<h2>Directory listing for %s</h2>\n" % displaypath
            f += "<hr>\n<ul>\n"
            for name in list:
                fullname = os.path.join(path, name)
                displayname = linkname = name
                # Append / for directories or @ for symbolic links
                if os.path.isdir(fullname):
                    displayname = name + "/"
                    linkname = name + "/"
                if os.path.islink(fullname):
                    displayname = name + "/"  # "@"
                    # Note: a link to a directory displays with @ and links with /
                f += '<li><a href="%s">%s</a>\n' % (urllib.quote(linkname),
                                                    cgi.escape(displayname))
            f += "</ul>\n<hr>\n</body>\n</html>\n"
            response = HttpResponse(protocol=request.protocol, status_code=200)
            response.headers['Content-type'] = 'text/html'
            response.headers['Content-Length'] = len(f)
            response.headers['Accept-Ranges'] = 'bytes'
            response.content = f
            Log('%s GET "%s" %s %s %s' %
                (addr[0], request.request_uri, request.protocol,
                 request.get_range(), response.status_code))
            response.write_to(clientsock)
            clientsock.close()
            return None
    print urllib.unquote(request.request_uri)[1:]

    file = get_file(path)

    if file.exists and request.is_range_requested():
        response = HttpResponse(protocol=request.protocol,
                                status_code=206,
                                range=request.get_range())
        response.headers["Content-Type"] = file.mime_type
        response.headers["Connection"] = "Keep-Alive"
        response.file = file

    elif file.exists:
        response = HttpResponse(protocol=request.protocol, status_code=200)
        response.file = file

    else:
        response = HttpResponse(protocol=request.protocol, status_code=404)
        response.headers['Content-type'] = 'text/plain'
        response.content = 'This file does not exist!'

    Log('%s GET "%s" %s %s %s' %
        (addr[0], request.request_uri, request.protocol, request.get_range(),
         response.status_code))

    response.write_to(clientsock)
    clientsock.close()
Esempio n. 4
0
def get_file(fn):
    # request_uri = fn.split("/",1)[-1]
    fsize = None
    exists = False
    mime_type = ''

    request_uri=fn.replace(STATIC_FILES_DIR+"/","")
    print request_uri
    if request_uri.startswith(atp["static_path"]):
        torrent=atp
        chosen_file="/".join(request_uri.split("/")[2:])
        print "chosen file:"+chosen_file
        url="magnet:?xt=urn:btih:"+request_uri.split("/")[1]
        info_hash=libtorrent.parse_magnet_uri(url)["info_hash"]
        handle=torrent_session.find_torrent(info_hash)
        # print str(info_hash)+".resume", os.listdir("resume")
        if handle.is_valid():
            print "Magnet already in the session"
            torrent_handle=handle
        # elif str(info_hash)+".resume" in os.listdir("resume"):
        #     torrent["resume_data"] = io.open("resume/"+str(info_hash)+".resume", "rb").read()
        #     torrent_handle = torrent_session.add_torrent(torrent)
        else:
            print " start new torrent"
            torrent["url"]=url
            torrent["save_path"]=os.path.join(TORRENTS_DIR,str(info_hash))
            # torrent["paused"]=True
            torrent_handle = torrent_session.add_torrent(torrent)
            while not torrent_handle.has_metadata():
                sleep(0.5)  

        info=torrent_handle.get_torrent_info()        
        torrent_files=info.files()
        # torrent_handle.auto_managed(False)    
        if chosen_file is not None:
            for i,file_info in enumerate(torrent_files):
                print "path:",file_info.path 
                if chosen_file == file_info.path:
                    torrent_handle.file_priority(i,1)
                    torrent_file=file_info
                    file_index=i
                else:
                    torrent_handle.file_priority(i,0)
        torrent_handle.map_piece = lambda offset: info.map_file(file_index, offset, 1)
        
        path=os.path.join(TORRENTS_DIR,torrent_file.path)
        first_byte=torrent_file.offset
        last_byte=torrent_file.offset+torrent_file.size
        torrent_handle.set_sequential_download(True)   
        first_piece=torrent_handle.map_piece(first_byte).piece
        last_piece=torrent_handle.map_piece(last_byte).piece
        num_pieces=info.num_pieces()
        future_pieces=int((last_piece-first_piece)*0.01)
        print "offset",torrent_file.offset,"piece",first_piece,"total", num_pieces
        piece_priorities=first_piece*[0]+(num_pieces-first_piece)*[1]
        piece_priorities=piece_priorities[:num_pieces]
        # piece_priorities=first_piece*[0]+future_pieces*[7]+more_pieces*[1]+(num_pieces-first_piece-future_pieces-more_pieces)*[0]
        torrent_handle.prioritize_pieces(piece_priorities)
        # torrent_handle.prioritize_pieces(num_pieces*[0])
        # torrent_handle.piece_priority(first_piece,7)
        torrent_handle.piece_priority(last_piece,7)
        torrent_handle.piece_priority(last_piece-1,7)
        # torrent_handle.num_pieces=info.num_pieces()
        piece=first_piece
        waiting=True


        while waiting:
            sleep(1)
            # if torrent_handle.have_piece(first_piece) and torrent_handle.have_piece(last_piece):
            #     waiting =False
            status=torrent_handle.status()
            pieces=status.pieces
            print pieces[piece:piece+future_pieces],last_piece  ,pieces[last_piece-1] , status.progress,status.download_rate,status.state
            if pieces[piece:piece+future_pieces] == future_pieces*[True] and pieces[last_piece-1]:
                waiting=False
        print "first and last piece received"
        # torrent_handle.prioritize_pieces(num_pieces*[1])
    else:
        torrent_handle=None    
    try:
        fsize = os.path.getsize(fn)
        exists = True
        type, encoding = mimetypes.guess_type(request_uri)
        if type:
            mime_type = type
    except:
        pass

    return File(request_uri, fn, fsize, exists, mime_type,torrent_handle)