2022-03-18 18:38:36 +04:00
|
|
|
import enum
|
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
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
from .aterrors import FileError
|
2021-10-14 17:56:01 +04:00
|
|
|
|
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 FileType(enum.IntEnum):
|
2022-06-17 12:30:58 +04:00
|
|
|
|
|
|
|
"""File or dierctory"""
|
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
file = 0
|
|
|
|
directory = 1
|
2021-10-14 17:56:01 +04:00
|
|
|
|
|
|
|
class AternosFile:
|
|
|
|
|
2022-06-17 12:30:58 +04:00
|
|
|
"""File class which contains info about its path, type and size
|
|
|
|
|
|
|
|
:param atserv: :class:`python_aternos.atserver.AternosServer` instance
|
|
|
|
:type atserv: python_aternos.atserver.AternosServer
|
|
|
|
:param path: Path to the file
|
|
|
|
:type path: str
|
|
|
|
:param name: Filename
|
|
|
|
:type name: str
|
|
|
|
:param ftype: File or directory
|
|
|
|
:type ftype: python_aternos.atfile.FileType
|
|
|
|
:param size: File size, defaults to 0
|
|
|
|
:type size: Union[int,float], optional
|
|
|
|
"""
|
|
|
|
|
2021-10-14 18:41:57 +04:00
|
|
|
def __init__(
|
2021-10-15 19:31:47 +04:00
|
|
|
self, atserv:'AternosServer',
|
2022-06-17 12:30:58 +04:00
|
|
|
path:str, name:str, ftype:FileType=FileType.file,
|
2022-03-25 16:45:38 +04:00
|
|
|
size:Union[int,float]=0) -> None:
|
2021-10-14 17:56:01 +04:00
|
|
|
|
|
|
|
self.atserv = atserv
|
2022-03-30 19:40:58 +04:00
|
|
|
self._path = path.lstrip('/')
|
2021-10-14 17:56:01 +04:00
|
|
|
self._name = name
|
2022-03-25 16:45:38 +04:00
|
|
|
self._full = path + name
|
2021-10-14 17:56:01 +04:00
|
|
|
self._ftype = ftype
|
2021-10-14 18:41:57 +04:00
|
|
|
self._size = float(size)
|
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
|
|
|
|
2022-06-17 12:30:58 +04:00
|
|
|
"""Deletes the file"""
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
self.atserv.atserver_request(
|
|
|
|
'https://aternos.org/panel/ajax/delete.php',
|
2022-03-25 16:45:38 +04:00
|
|
|
'POST', data={'file': self._full},
|
2021-10-14 17:56:01 +04:00
|
|
|
sendtoken=True
|
|
|
|
)
|
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
def get_content(self) -> bytes:
|
2022-03-25 16:45:38 +04:00
|
|
|
|
2022-06-17 12:30:58 +04:00
|
|
|
"""Requests file content in bytes (downloads it)
|
|
|
|
|
|
|
|
:raises FileError: If downloading
|
|
|
|
the file is not allowed by Aternos
|
|
|
|
:return: File content
|
|
|
|
:rtype: bytes
|
|
|
|
"""
|
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
file = self.atserv.atserver_request(
|
2022-03-25 16:45:38 +04:00
|
|
|
'https://aternos.org/panel/ajax/files/download.php',
|
2022-03-18 18:38:36 +04:00
|
|
|
'GET', params={
|
2022-03-25 16:45:38 +04:00
|
|
|
'file': self._full
|
2022-03-18 18:38:36 +04:00
|
|
|
}
|
2021-10-15 15:12:45 +04:00
|
|
|
)
|
2022-03-25 16:45:38 +04:00
|
|
|
if file.content == b'{"success":false}':
|
|
|
|
raise FileError('Unable to download the file. Try to get text')
|
2021-10-15 15:12:45 +04:00
|
|
|
return file.content
|
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
def set_content(self, value:bytes) -> None:
|
2022-03-25 16:45:38 +04:00
|
|
|
|
2022-06-17 12:30:58 +04:00
|
|
|
"""Modifies the file content
|
|
|
|
|
|
|
|
:param value: New content
|
|
|
|
:type value: bytes
|
|
|
|
"""
|
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
self.atserv.atserver_request(
|
|
|
|
f'https://aternos.org/panel/ajax/save.php',
|
2022-03-18 18:38:36 +04:00
|
|
|
'POST', data={
|
2022-03-25 16:45:38 +04:00
|
|
|
'file': self._full,
|
2022-03-18 18:38:36 +04:00
|
|
|
'content': value
|
|
|
|
}, sendtoken=True
|
2021-10-15 15:12:45 +04:00
|
|
|
)
|
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
def get_text(self) -> str:
|
2022-03-25 16:45:38 +04:00
|
|
|
|
2022-06-17 12:30:58 +04:00
|
|
|
"""Requests editing the file as a text
|
|
|
|
(try it if downloading is disallowed)
|
|
|
|
|
|
|
|
:return: File text content
|
|
|
|
:rtype: str
|
|
|
|
"""
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
editor = self.atserv.atserver_request(
|
2022-04-01 17:00:02 +04:00
|
|
|
f'https://aternos.org/files/{self._full.lstrip("/")}', 'GET'
|
2021-10-14 17:56:01 +04:00
|
|
|
)
|
|
|
|
edittree = lxml.html.fromstring(editor.content)
|
2022-04-01 17:00:02 +04:00
|
|
|
|
|
|
|
editblock = edittree.xpath('//div[@id="editor"]')[0]
|
|
|
|
return editblock.text_content()
|
2021-10-14 17:56:01 +04:00
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
def set_text(self, value:str) -> None:
|
2022-03-25 16:45:38 +04:00
|
|
|
|
2022-06-17 12:30:58 +04:00
|
|
|
"""Modifies the file content,
|
|
|
|
but unlike set_content takes
|
|
|
|
a string as a new value
|
|
|
|
|
|
|
|
:param value: New content
|
|
|
|
:type value: str
|
|
|
|
"""
|
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
self.set_content(value.encode('utf-8'))
|
2021-10-14 17:56:01 +04:00
|
|
|
|
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
|
2022-03-25 16:45:38 +04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def full(self) -> str:
|
|
|
|
return self._full
|
2021-10-14 17:56:01 +04:00
|
|
|
|
|
|
|
@property
|
2021-10-15 15:12:45 +04:00
|
|
|
def is_dir(self) -> bool:
|
2022-03-18 18:38:36 +04:00
|
|
|
if self._ftype == FileType.directory:
|
2021-10-15 15:12:45 +04:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_file(self) -> bool:
|
2022-03-18 18:38:36 +04:00
|
|
|
if self._ftype == FileType.file:
|
2021-10-15 15:12:45 +04:00
|
|
|
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
|