2014-03-10 01:18:05 -04:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# This file is part of SickRage.
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is free software: you can redistribute it and/or modify
|
2014-03-10 01:18:05 -04:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is distributed in the hope that it will be useful,
|
2014-03-10 01:18:05 -04:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2014-05-23 08:37:22 -04:00
|
|
|
# along with SickRage. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
import os
|
|
|
|
import string
|
|
|
|
|
2014-06-11 04:34:28 -04:00
|
|
|
from tornado.httputil import HTTPHeaders
|
|
|
|
from tornado.web import RequestHandler
|
2014-03-10 01:18:05 -04:00
|
|
|
from sickbeard import encodingKludge as ek
|
2014-08-29 06:29:56 -04:00
|
|
|
from sickbeard import logger
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
# use the built-in if it's available (python 2.6), if not use the included library
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
from lib import simplejson as json
|
|
|
|
|
|
|
|
# this is for the drive letter code, it only works on windows
|
|
|
|
if os.name == 'nt':
|
|
|
|
from ctypes import windll
|
|
|
|
|
|
|
|
# adapted from http://stackoverflow.com/questions/827371/is-there-a-way-to-list-all-the-available-drive-letters-in-python/827490
|
|
|
|
def getWinDrives():
|
|
|
|
""" Return list of detected drives """
|
|
|
|
assert os.name == 'nt'
|
|
|
|
|
|
|
|
drives = []
|
2014-03-25 01:57:24 -04:00
|
|
|
bitmask = windll.kernel32.GetLogicalDrives() #@UndefinedVariable
|
2014-03-10 01:18:05 -04:00
|
|
|
for letter in string.uppercase:
|
|
|
|
if bitmask & 1:
|
|
|
|
drives.append(letter)
|
|
|
|
bitmask >>= 1
|
|
|
|
|
|
|
|
return drives
|
|
|
|
|
|
|
|
|
2014-06-19 10:31:44 -04:00
|
|
|
def foldersAtPath(path, includeParent=False, includeFiles=False):
|
2014-03-10 01:18:05 -04:00
|
|
|
""" Returns a list of dictionaries with the folders contained at the given path
|
|
|
|
Give the empty string as the path to list the contents of the root path
|
|
|
|
under Unix this means "/", on Windows this will be a list of drive letters)
|
|
|
|
"""
|
|
|
|
|
|
|
|
# walk up the tree until we find a valid path
|
|
|
|
while path and not os.path.isdir(path):
|
|
|
|
if path == os.path.dirname(path):
|
|
|
|
path = ''
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
path = os.path.dirname(path)
|
|
|
|
|
|
|
|
if path == "":
|
|
|
|
if os.name == 'nt':
|
|
|
|
entries = [{'current_path': 'Root'}]
|
|
|
|
for letter in getWinDrives():
|
|
|
|
letterPath = letter + ':\\'
|
|
|
|
entries.append({'name': letterPath, 'path': letterPath})
|
|
|
|
return entries
|
|
|
|
else:
|
|
|
|
path = '/'
|
|
|
|
|
|
|
|
# fix up the path and find the parent
|
|
|
|
path = os.path.abspath(os.path.normpath(path))
|
|
|
|
parentPath = os.path.dirname(path)
|
|
|
|
|
|
|
|
# if we're at the root then the next step is the meta-node showing our drive letters
|
|
|
|
if path == parentPath and os.name == 'nt':
|
|
|
|
parentPath = ""
|
|
|
|
|
2014-08-29 06:29:56 -04:00
|
|
|
try:
|
|
|
|
fileList = [{'name': filename, 'path': ek.ek(os.path.join, path, filename)} for filename in ek.ek(os.listdir, path)]
|
|
|
|
except OSError, e:
|
|
|
|
logger.log(u"Unable to open " + path + ": " + repr(e) + " / " + str(e), logger.WARNING)
|
|
|
|
fileList = [{'name': filename, 'path': ek.ek(os.path.join, parentPath, filename)} for filename in ek.ek(os.listdir, parentPath)]
|
|
|
|
|
2014-06-19 10:31:44 -04:00
|
|
|
if not includeFiles:
|
|
|
|
fileList = filter(lambda entry: ek.ek(os.path.isdir, entry['path']), fileList)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
# prune out directories to proect the user from doing stupid things (already lower case the dir to reduce calls)
|
2014-03-25 01:57:24 -04:00
|
|
|
hideList = ["boot", "bootmgr", "cache", "msocache", "recovery", "$recycle.bin", "recycler",
|
|
|
|
"system volume information", "temporary internet files"] # windows specific
|
|
|
|
hideList += [".fseventd", ".spotlight", ".trashes", ".vol", "cachedmessages", "caches", "trash"] # osx specific
|
2014-03-10 01:18:05 -04:00
|
|
|
fileList = filter(lambda entry: entry['name'].lower() not in hideList, fileList)
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
fileList = sorted(fileList,
|
|
|
|
lambda x, y: cmp(os.path.basename(x['name']).lower(), os.path.basename(y['path']).lower()))
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
entries = [{'current_path': path}]
|
|
|
|
if includeParent and parentPath != path:
|
2014-03-25 01:57:24 -04:00
|
|
|
entries.append({'name': "..", 'path': parentPath})
|
2014-03-10 01:18:05 -04:00
|
|
|
entries.extend(fileList)
|
|
|
|
|
|
|
|
return entries
|
|
|
|
|
|
|
|
|
2014-06-11 04:34:28 -04:00
|
|
|
class WebFileBrowser(RequestHandler):
|
2014-06-19 10:31:44 -04:00
|
|
|
def index(self, path='', includeFiles=False, *args, **kwargs):
|
2014-06-16 01:45:52 -04:00
|
|
|
self.set_header("Content-Type", "application/json")
|
2014-06-19 10:31:44 -04:00
|
|
|
return json.dumps(foldersAtPath(path, True, bool(int(includeFiles))))
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-06-19 10:31:44 -04:00
|
|
|
def complete(self, term, includeFiles=0):
|
2014-06-16 01:45:52 -04:00
|
|
|
self.set_header("Content-Type", "application/json")
|
2014-06-19 10:31:44 -04:00
|
|
|
paths = [entry['path'] for entry in foldersAtPath(os.path.dirname(term), includeFiles=bool(int(includeFiles))) if 'path' in entry]
|
|
|
|
return json.dumps(paths)
|