2021-10-14 17:56:01 +04:00
|
|
|
import lxml.html
|
2022-06-17 12:30:58 +04:00
|
|
|
from typing import Union, Optional, List
|
2021-10-15 19:31:47 +04:00
|
|
|
from typing import TYPE_CHECKING
|
2021-10-14 17:56:01 +04:00
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
from .atfile import AternosFile, FileType
|
2021-10-15 19:31:47 +04:00
|
|
|
if TYPE_CHECKING:
|
2022-03-18 18:38:36 +04:00
|
|
|
from .atserver import AternosServer
|
2021-10-15 19:31:47 +04:00
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
class FileManager:
|
2021-10-14 17:56:01 +04:00
|
|
|
|
2022-06-17 12:30:58 +04:00
|
|
|
"""Aternos file manager class for viewing files structure
|
|
|
|
|
|
|
|
:param atserv: :class:`python_aternos.atserver.AternosServer` instance
|
|
|
|
:type atserv: python_aternos.atserver.AternosServer
|
|
|
|
"""
|
|
|
|
|
2021-10-15 19:31:47 +04:00
|
|
|
def __init__(self, atserv:'AternosServer') -> None:
|
2021-10-14 17:56:01 +04:00
|
|
|
|
|
|
|
self.atserv = atserv
|
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
def listdir(self, path:str='') -> List[AternosFile]:
|
2021-10-14 17:56:01 +04:00
|
|
|
|
2022-06-17 12:30:58 +04:00
|
|
|
"""Requests a list of files
|
|
|
|
in the specified directory
|
|
|
|
|
|
|
|
:param path: Directory
|
|
|
|
(an empty string means root), defaults to ''
|
|
|
|
:type path: str, optional
|
|
|
|
:return: List of :class:`python_aternos.atfile.AternosFile`
|
|
|
|
:rtype: List[AternosFile]
|
|
|
|
"""
|
|
|
|
|
2022-03-30 19:40:58 +04:00
|
|
|
path = path.lstrip('/')
|
2021-10-14 17:56:01 +04:00
|
|
|
filesreq = self.atserv.atserver_request(
|
2022-03-18 18:38:36 +04:00
|
|
|
f'https://aternos.org/files/{path}', 'GET'
|
2021-10-14 17:56:01 +04:00
|
|
|
)
|
|
|
|
filestree = lxml.html.fromstring(filesreq.content)
|
2022-03-30 19:40:58 +04:00
|
|
|
fileslist = filestree.xpath('//div[contains(concat(" ",normalize-space(@class)," ")," file ")]')
|
2021-10-14 17:56:01 +04:00
|
|
|
|
|
|
|
files = []
|
|
|
|
for f in fileslist:
|
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
ftype_raw = f.xpath('@data-type')[0]
|
2022-03-18 18:38:36 +04:00
|
|
|
ftype = FileType.file \
|
2021-10-14 17:56:01 +04:00
|
|
|
if ftype_raw == 'file' \
|
2022-03-18 18:38:36 +04:00
|
|
|
else FileType.directory
|
2021-10-14 17:56:01 +04:00
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
fsize_raw = f.xpath('./div[@class="filesize"]')
|
2021-10-14 17:56:01 +04:00
|
|
|
fsize = 0
|
|
|
|
if len(fsize_raw) > 0:
|
|
|
|
|
|
|
|
fsize_text = fsize_raw[0].text.strip()
|
|
|
|
fsize_num = fsize_text[:fsize_text.rfind(' ')]
|
|
|
|
fsize_msr = fsize_text[fsize_text.rfind(' ')+1:]
|
|
|
|
|
2021-10-14 18:41:57 +04:00
|
|
|
try:
|
2022-03-18 18:38:36 +04:00
|
|
|
fsize = self.convert_size(float(fsize_num), fsize_msr)
|
2021-10-14 18:41:57 +04:00
|
|
|
except ValueError:
|
|
|
|
fsize = -1
|
2021-10-14 17:56:01 +04:00
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
fullpath = f.xpath('@data-path')[0]
|
2021-10-14 17:56:01 +04:00
|
|
|
filepath = fullpath[:fullpath.rfind('/')]
|
|
|
|
filename = fullpath[fullpath.rfind('/'):]
|
|
|
|
files.append(
|
2022-03-18 18:38:36 +04:00
|
|
|
AternosFile(
|
2021-10-14 17:56:01 +04:00
|
|
|
self.atserv,
|
|
|
|
filepath, filename,
|
2022-03-25 16:45:38 +04:00
|
|
|
ftype, fsize
|
2021-10-14 17:56:01 +04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return files
|
|
|
|
|
|
|
|
def convert_size(self, num:Union[int,float], measure:str) -> float:
|
|
|
|
|
2022-06-17 12:30:58 +04:00
|
|
|
"""Converts "human" file size to size in bytes
|
|
|
|
|
|
|
|
:param num: Size
|
|
|
|
:type num: Union[int,float]
|
|
|
|
:param measure: Units (B, kB, MB, GB)
|
|
|
|
:type measure: str
|
|
|
|
:return: Size in bytes
|
|
|
|
:rtype: float
|
|
|
|
"""
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
measure_match = {
|
|
|
|
'B': 1,
|
|
|
|
'kB': 1000,
|
|
|
|
'MB': 1000000,
|
|
|
|
'GB': 1000000000
|
|
|
|
}
|
|
|
|
try:
|
2022-03-30 19:40:58 +04:00
|
|
|
return num * measure_match[measure]
|
2021-10-14 17:56:01 +04:00
|
|
|
except KeyError:
|
2022-03-30 19:40:58 +04:00
|
|
|
return -1
|
2021-10-14 17:56:01 +04:00
|
|
|
|
2022-06-17 12:30:58 +04:00
|
|
|
def get_file(self, path:str) -> Optional[AternosFile]:
|
|
|
|
|
|
|
|
"""Returns :class:`python_aternos.atfile.AternosFile`
|
|
|
|
instance by its path
|
|
|
|
|
|
|
|
:param path: Path to file including its filename
|
|
|
|
:type path: str
|
|
|
|
:return: _description_
|
|
|
|
:rtype: Optional[AternosFile]
|
|
|
|
"""
|
2021-10-14 17:56:01 +04:00
|
|
|
|
|
|
|
filepath = path[:path.rfind('/')]
|
|
|
|
filename = path[path.rfind('/'):]
|
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
filedir = self.listdir(filepath)
|
2021-10-14 17:56:01 +04:00
|
|
|
for file in filedir:
|
|
|
|
if file.name == filename:
|
|
|
|
return file
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def dl_file(self, path:str) -> bytes:
|
|
|
|
|
2022-06-17 12:30:58 +04:00
|
|
|
"""Returns the file content in bytes (downloads it)
|
|
|
|
|
|
|
|
:param path: Path to file including its filename
|
|
|
|
:type path: str
|
|
|
|
:return: File content
|
|
|
|
:rtype: bytes
|
|
|
|
"""
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
file = self.atserv.atserver_request(
|
|
|
|
f'https://aternos.org/panel/ajax/files/download.php?' + \
|
2021-10-15 15:12:45 +04:00
|
|
|
f'file={path.replace("/","%2F")}',
|
2022-03-18 18:38:36 +04:00
|
|
|
'GET'
|
2021-10-14 17:56:01 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
return file.content
|
|
|
|
|
|
|
|
def dl_world(self, world:str='world') -> bytes:
|
|
|
|
|
2022-06-17 12:30:58 +04:00
|
|
|
"""Returns the world zip file content
|
|
|
|
by its name (downloads it)
|
|
|
|
|
|
|
|
:param world: Name of world, defaults to 'world'
|
|
|
|
:type world: str, optional
|
|
|
|
:return: Zip file content
|
|
|
|
:rtype: bytes
|
|
|
|
"""
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
world = self.atserv.atserver_request(
|
|
|
|
f'https://aternos.org/panel/ajax/worlds/download.php?' + \
|
2021-10-15 15:12:45 +04:00
|
|
|
f'world={world.replace("/","%2F")}',
|
2022-03-18 18:38:36 +04:00
|
|
|
'GET'
|
2021-10-14 17:56:01 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
return world.content
|