You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.0 KiB

from couchpotato.api import addApiView
14 years ago
from couchpotato.core.helpers.request import getParam, jsonified
from couchpotato.core.plugins.base import Plugin
import ctypes
import os
import string
if os.name == 'nt':
import win32file
class FileBrowser(Plugin):
14 years ago
def __init__(self):
addApiView('directory.list', self.view)
14 years ago
def getDirectories(self, path = '/', show_hidden = True):
# Return driveletters or root if path is empty
if path == '/' or not path or path == '\\':
if os.name == 'nt':
return self.getDriveLetters()
14 years ago
path = '/'
dirs = []
14 years ago
for f in os.listdir(path):
p = os.path.join(path, f)
if os.path.isdir(p) and ((self.is_hidden(p) and bool(int(show_hidden))) or not self.is_hidden(p)):
dirs.append(p + os.path.sep)
return dirs
def getFiles(self):
pass
def getDriveLetters(self):
driveletters = []
for drive in string.ascii_uppercase:
if win32file.GetDriveType(drive + ":") in [win32file.DRIVE_FIXED, win32file.DRIVE_REMOTE]:
driveletters.append(drive + ":\\")
return driveletters
def view(self):
try:
14 years ago
dirs = self.getDirectories(path = getParam('path', '/'), show_hidden = getParam('show_hidden', True))
except:
dirs = []
14 years ago
return jsonified({
'is_root': getParam('path', '/') == '/',
'empty': len(dirs) == 0,
'dirs': dirs,
})
def is_hidden(self, filepath):
name = os.path.basename(os.path.abspath(filepath))
return name.startswith('.') or self.has_hidden_attribute(filepath)
def has_hidden_attribute(self, filepath):
try:
attrs = ctypes.windll.kernel32.GetFileAttributesW(unicode(filepath))
assert attrs != -1
result = bool(attrs & 2)
except (AttributeError, AssertionError):
result = False
return result