Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions motioneye/handlers/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,41 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations

import logging
import os
from os.path import exists, join
from typing import ClassVar

from tornado.web import HTTPError

from motioneye import settings, utils
from motioneye.handlers.base import BaseHandler
from motioneye.settings import LOG_PATH, LOG_TO_FILE
from motioneye.utils import call_subprocess

__all__ = ('LogHandler',)


class LogHandler(BaseHandler):
LOGS: ClassVar[dict] = {
'motion': (os.path.join(settings.LOG_PATH, 'motion.log'), 'motion.log'),
LOGS: ClassVar[dict[str, tuple[str, str]]] = {
'motion': (join(LOG_PATH, 'motion.log'), 'motion.log'),
}

@BaseHandler.auth(admin=True)
def get(self, name):
log = self.LOGS.get(name)
def get(self, name: str) -> None:
log: tuple[str, str] | None = self.LOGS.get(name)
if log is None:
raise HTTPError(404, f'log file name "{name}" is undefined')

path, filename = log
if path.startswith('/') and not exists(path):
if LOG_TO_FILE:
raise HTTPError(404, f'{name} log path "{path}" not found')

if path.startswith('/') and not os.path.exists(path):
raise HTTPError(404, f'{name} log path "{path}" not found')
raise HTTPError(404, 'file logging is disabled on this motionEye server')

self.set_header('Content-Type', 'text/plain')
self.set_header('Content-Disposition', 'attachment; filename=' + filename + ';')
self.set_header('Content-Disposition', f'attachment; filename={filename};')

if path.startswith('/'): # an actual path
logging.debug(f'serving log file "{filename}" from "{path}"')
Expand All @@ -56,7 +60,7 @@ def get(self, name):
logging.debug(f'serving log file "{filename}" from command "{path}"')

try:
output = utils.call_subprocess(path.split())
output = call_subprocess(path.split())

except Exception as e:
output = f'failed to execute command: {e}'
Expand Down
1 change: 1 addition & 0 deletions motioneye/meyectl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations

import argparse
import gettext
Expand Down
2 changes: 1 addition & 1 deletion motioneye/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def test_requirements():
)
sys.exit(-1)

if not os.access(settings.LOG_PATH, os.W_OK):
if settings.LOG_TO_FILE and not os.access(settings.LOG_PATH, os.W_OK):
logging.fatal(
f'log directory "{settings.LOG_PATH}" does not exist or is not writable'
)
Expand Down
Loading