2021-10-14 17:56:01 +04:00
|
|
|
import lxml.html
|
|
|
|
from typing import Union
|
2021-10-15 19:31:47 +04:00
|
|
|
from typing import TYPE_CHECKING
|
2021-10-14 17:56:01 +04:00
|
|
|
|
|
|
|
from . import atconnect
|
|
|
|
|
2021-10-15 19:31:47 +04:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from atserver import AternosServer
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
FTYPE_FILE = 0
|
|
|
|
FTYPE_DIR = 1
|
|
|
|
|
|
|
|
class AternosFile:
|
|
|
|
|
2021-10-14 18:41:57 +04:00
|
|
|
def __init__(
|
2021-10-15 19:31:47 +04:00
|
|
|
self, atserv:'AternosServer',
|
2021-10-14 18:41:57 +04:00
|
|
|
path:str, name:str, ftype:int=FTYPE_FILE,
|
2021-10-15 15:12:45 +04:00
|
|
|
size:Union[int,float]=0, dlallowed:bool=False) -> None:
|
2021-10-14 17:56:01 +04:00
|
|
|
|
|
|
|
self.atserv = atserv
|
2021-10-15 15:12:45 +04:00
|
|
|
self._path = path
|
2021-10-14 17:56:01 +04:00
|
|
|
self._name = name
|
|
|
|
self._ftype = ftype
|
2021-10-14 18:41:57 +04:00
|
|
|
self._size = float(size)
|
2021-10-15 15:12:45 +04:00
|
|
|
self._dlallowed = dlallowed
|
2021-10-14 17:56:01 +04:00
|
|
|
|
2021-10-14 18:41:57 +04:00
|
|
|
def delete(self) -> None:
|
2021-10-14 17:56:01 +04:00
|
|
|
|
|
|
|
self.atserv.atserver_request(
|
|
|
|
'https://aternos.org/panel/ajax/delete.php',
|
|
|
|
atconnect.REQPOST, data={'file': self._name},
|
|
|
|
sendtoken=True
|
|
|
|
)
|
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
@property
|
|
|
|
def content(self) -> bytes:
|
|
|
|
file = self.atserv.atserver_request(
|
|
|
|
f'https://aternos.org/panel/ajax/files/download.php',
|
|
|
|
atconnect.REQGET, params={'file': self.path.replace('/','%2F')}
|
|
|
|
)
|
|
|
|
if not self._dlallowed:
|
|
|
|
raise AternosIOError('Downloading this file is not allowed. Try to get text')
|
|
|
|
return file.content
|
|
|
|
|
|
|
|
@content.setter
|
|
|
|
def content(self, value:bytes) -> None:
|
|
|
|
self.atserv.atserver_request(
|
|
|
|
f'https://aternos.org/panel/ajax/save.php',
|
|
|
|
atconnect.REQPOST, data={'content': value},
|
|
|
|
sendtoken=True
|
|
|
|
)
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
@property
|
2021-10-14 18:41:57 +04:00
|
|
|
def text(self) -> str:
|
2021-10-14 17:56:01 +04:00
|
|
|
editor = self.atserv.atserver_request(
|
|
|
|
f'https://aternos.org/files/{self._name}',
|
|
|
|
atconnect.REQGET
|
|
|
|
)
|
|
|
|
edittree = lxml.html.fromstring(editor.content)
|
|
|
|
|
|
|
|
editfield = edittree.xpath('//div[@class="ace_layer ace_text-layer"]')[0]
|
|
|
|
editlines = editfield.xpath('/div[@class="ace_line"]')
|
|
|
|
rawlines = []
|
|
|
|
|
|
|
|
for line in editlines:
|
|
|
|
rawlines.append(line.text)
|
|
|
|
return rawlines
|
|
|
|
|
|
|
|
@text.setter
|
2021-10-15 15:12:45 +04:00
|
|
|
def text(self, value:str) -> None:
|
2021-10-14 17:56:01 +04:00
|
|
|
self.atserv.atserver_request(
|
|
|
|
f'https://aternos.org/panel/ajax/save.php',
|
|
|
|
atconnect.REQPOST, data={'content': value},
|
|
|
|
sendtoken=True
|
|
|
|
)
|
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
return self._path
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
@property
|
2021-10-14 18:41:57 +04:00
|
|
|
def name(self) -> str:
|
2021-10-14 17:56:01 +04:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
2021-10-15 15:12:45 +04:00
|
|
|
def is_dir(self) -> bool:
|
|
|
|
if self._ftype == FTYPE_DIR:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_file(self) -> bool:
|
|
|
|
if self._ftype == FTYPE_FILE:
|
|
|
|
return True
|
|
|
|
return False
|
2021-10-14 17:56:01 +04:00
|
|
|
|
|
|
|
@property
|
2021-10-14 18:41:57 +04:00
|
|
|
def size(self) -> float:
|
2021-10-14 17:56:01 +04:00
|
|
|
return self._size
|
2021-10-15 15:12:45 +04:00
|
|
|
|
|
|
|
@property
|
2021-10-15 19:31:47 +04:00
|
|
|
def dlallowed(self) -> bool:
|
2021-10-15 15:12:45 +04:00
|
|
|
return self._dlallowed
|