1
0
mirror of https://github.com/sashacmc/photo-importer.git synced 2024-11-24 08:02:14 +02:00
photo-importer/server.py

360 lines
11 KiB
Python
Raw Normal View History

2018-06-25 09:47:59 +02:00
#!/usr/bin/python3
import os
import re
import cgi
import json
import psutil
2018-06-25 09:47:59 +02:00
import logging
import argparse
import http.server
import log
import config
2018-09-23 21:55:10 +02:00
import importer
2018-06-25 09:47:59 +02:00
FIXED_IN_PATH_NAME = 'none'
2018-06-25 09:47:59 +02:00
class PhotoImporterHandler(http.server.BaseHTTPRequestHandler):
2018-06-25 09:47:59 +02:00
def __ok_response(self, result):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(bytearray(json.dumps(result), 'utf-8'))
def __text_response(self, result):
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(bytearray(result, 'utf-8'))
2018-06-25 09:47:59 +02:00
def __bad_request_response(self, err):
self.send_response(400)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(bytearray(err, 'utf-8'))
def __not_found_response(self):
self.send_error(404, 'File Not Found: %s' % self.path)
def __server_error_response(self, err):
self.send_error(500, 'Internal Server Error: %s' % err)
def __get_mounted_list(self):
return {dp.device: dp.mountpoint for dp in psutil.disk_partitions()}
def __bytes_to_gbytes(self, b):
return round(b / 1024. / 1024. / 1024., 2)
2018-06-25 09:47:59 +02:00
def __mount_get_list(self):
mount_list = self.__get_mounted_list()
2018-06-25 09:47:59 +02:00
dev_list = os.listdir('/dev')
if self.server.fixed_in_path() != '':
mount_list['/dev/' + FIXED_IN_PATH_NAME] = \
self.server.fixed_in_path()
dev_list.append(FIXED_IN_PATH_NAME)
res = {}
2018-06-25 09:47:59 +02:00
for dev in dev_list:
if re.match(self.server.remote_drive_reg(), dev) \
or dev == FIXED_IN_PATH_NAME:
r = {}
r['path'] = mount_list.get('/dev/' + dev, '')
2018-09-23 21:55:10 +02:00
r['progress'] = 0
if r['path']:
2018-09-23 21:55:10 +02:00
stat = self.server.import_status(r['path'])
du = psutil.disk_usage(r['path'])
r['size'] = self.__bytes_to_gbytes(du.total)
r['usage'] = du.percent
2018-09-23 21:55:10 +02:00
if stat:
stage = stat['stage']
r['state'] = stage
if stage == 'move' or stage == 'rotate':
r['progress'] = \
round(100. *
stat[stage]['processed'] / stat['total'])
elif stage == 'done':
cerr = stat['move']['errors'] + \
stat['rotate']['errors']
if cerr != 0:
r['state'] = 'error'
r['total'] = cerr
r['details'] = str(stat)
else:
r['total'] = stat['total']
2018-09-23 21:55:10 +02:00
else:
r['state'] = 'mounted'
else:
r['size'] = 0
r['usage'] = 0
r['state'] = 'unmounted'
res[dev] = r
2018-06-25 09:47:59 +02:00
return res
2018-07-15 16:43:56 +02:00
def __mount_mount(self, dev):
if dev == '':
self.__bad_request_response('empty "d" param')
return None
logging.info('pmount')
path = self.__get_mounted_list().get('/dev/' + dev, None)
if path:
self.server.import_done(path)
2018-07-15 16:43:56 +02:00
return os.system('pmount --umask=000 /dev/%s' % dev)
def __mount_umount(self, dev):
if dev == '':
self.__bad_request_response('empty "d" param')
return None
logging.info('pumount')
path = self.__get_mounted_list().get('/dev/' + dev, None)
if path:
self.server.import_done(path)
2018-07-15 16:43:56 +02:00
return os.system('pumount /dev/%s' % dev)
2018-06-25 09:47:59 +02:00
def __mount_request(self, params):
try:
action = params['a'][0]
except Exception as ex:
self.__bad_request_response(str(ex))
logging.exception(ex)
return
try:
2018-07-15 16:43:56 +02:00
dev = params['d'][0]
2018-06-25 09:47:59 +02:00
except:
2018-07-15 16:43:56 +02:00
dev = ''
2018-06-25 09:47:59 +02:00
result = None
2018-06-25 09:47:59 +02:00
if action == 'list':
result = self.__mount_get_list()
elif action == 'mount':
2018-07-15 16:43:56 +02:00
result = self.__mount_mount(dev)
2018-06-25 09:47:59 +02:00
elif action == 'umount':
2018-07-15 16:43:56 +02:00
result = self.__mount_umount(dev)
2018-06-25 09:47:59 +02:00
else:
self.__bad_request_response('unknown action %s' % action)
return
self.__ok_response(result)
2018-09-23 21:55:10 +02:00
def __import_start(self, in_path):
self.server.import_start(in_path)
return True
def __import_stop(self, dev):
pass
def __import_get_log(self, in_path):
return self.server.get_log(in_path)
2018-09-23 21:55:10 +02:00
def __import_request(self, params):
try:
action = params['a'][0]
except Exception as ex:
self.__bad_request_response(str(ex))
logging.exception(ex)
return
try:
in_path = params['p'][0]
except:
self.__bad_request_response(str(ex))
logging.exception(ex)
return
result = None
if action == 'start':
result = self.__import_start(in_path)
self.__ok_response(result)
2018-09-23 21:55:10 +02:00
elif action == 'stop':
result = self.__import_stop(in_path)
self.__ok_response(result)
elif action == 'getlog':
result = self.__import_get_log(in_path)
self.__text_response(result)
2018-09-23 21:55:10 +02:00
else:
self.__bad_request_response('unknown action %s' % action)
return
def __sysinfo_request(self, params):
res = {}
du = psutil.disk_usage(self.server.out_path())
mem = psutil.virtual_memory()
res['disk_size'] = self.__bytes_to_gbytes(du.total)
res['disk_usage'] = du.percent
res['cpu'] = psutil.cpu_percent()
res['mem_total'] = self.__bytes_to_gbytes(mem.total)
res['mem_usage'] = mem.percent
self.__ok_response(res)
2018-06-25 09:47:59 +02:00
def __file_request(self, path):
try:
if (path[0]) == '/':
path = path[1:]
fname = os.path.join(self.server.web_path(), path)
ext = os.path.splitext(fname)[1]
cont = ''
if ext == '.html':
cont = 'text/html'
elif ext == '.js':
cont = 'text/script'
elif ext == '.png':
cont = 'image/png'
else:
cont = 'text/none'
with open(fname, 'rb') as f:
2018-06-25 09:47:59 +02:00
self.send_response(200)
self.send_header('Content-type', cont)
2018-06-25 09:47:59 +02:00
self.end_headers()
self.wfile.write(bytearray(f.read()))
except IOError as ex:
2018-06-25 09:47:59 +02:00
self.__not_found_response()
logging.exception(ex)
2018-06-25 09:47:59 +02:00
def __path_params(self):
path_params = self.path.split('?')
if len(path_params) > 1:
return path_params[0], cgi.parse_qs(path_params[1])
else:
return path_params[0], {}
def do_GET(self):
logging.info('do_GET: ' + self.path)
2018-06-25 09:47:59 +02:00
try:
path, params = self.__path_params()
if path == '/mount':
self.__mount_request(params)
return
2018-09-23 21:55:10 +02:00
if path == '/import':
self.__import_request(params)
return
if path == '/sysinfo':
self.__sysinfo_request(params)
return
2018-06-25 09:47:59 +02:00
if path == '/':
path = 'index.html'
if '..' in path:
logging.warning('".." in path: ' + path)
2018-06-25 09:47:59 +02:00
self.__not_found_response()
return
ext = os.path.splitext(path)[1]
if ext in ('.html', '.js', '.png'):
2018-06-25 09:47:59 +02:00
self.__file_request(path)
return
logging.warning('Wrong path: ' + path)
2018-06-25 09:47:59 +02:00
self.__not_found_response()
except Exception as ex:
self.__server_error_response(str(ex))
logging.exception(ex)
def do_POST(self):
logging.info('do_POST: ' + self.path)
2018-06-25 09:47:59 +02:00
try:
path, params = self.__path_params()
if path == '/mount':
2018-06-25 09:47:59 +02:00
self.__mount_request(params)
return
2018-09-23 21:55:10 +02:00
if path == '/import':
self.__import_request(params)
return
2018-06-25 09:47:59 +02:00
except Exception as ex:
self.__server_error_response(str(ex))
logging.exception(ex)
class PhotoImporterServer(http.server.HTTPServer):
def __init__(self, cfg):
self.__cfg = cfg
2018-09-23 21:55:10 +02:00
self.__importers = {}
2018-06-25 09:47:59 +02:00
port = int(cfg['server']['port'])
self.__web_path = cfg['server']['web_path']
2018-06-25 09:47:59 +02:00
self.__remote_drive_reg = cfg['server']['remote_drive_reg']
self.__out_path = cfg['server']['out_path']
self.__fixed_in_path = cfg['server']['in_path']
2018-06-25 09:47:59 +02:00
super().__init__(('', port), PhotoImporterHandler)
def web_path(self):
return self.__web_path
2018-06-25 09:47:59 +02:00
def remote_drive_reg(self):
return self.__remote_drive_reg
def out_path(self):
return self.__out_path
def fixed_in_path(self):
return self.__fixed_in_path
2018-09-23 21:55:10 +02:00
def import_start(self, in_path):
logging.info('import_start: %s', in_path)
if in_path in self.__importers and in_path != self.fixed_in_path():
2018-09-23 21:55:10 +02:00
raise Exception('Already started')
self.__importers[in_path] = importer.Importer(
self.__cfg,
in_path,
self.out_path(),
False)
self.__importers[in_path].start()
def import_status(self, in_path):
if in_path in self.__importers:
return self.__importers[in_path].status()
else:
return None
def import_done(self, in_path):
logging.info('import_done: %s', in_path)
if in_path in self.__importers:
del self.__importers[in_path]
def get_log(self, in_path):
if in_path in self.__importers:
return self.__importers[in_path].log_text()
else:
return ''
2018-06-25 09:47:59 +02:00
def args_parse():
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', help='Config file')
parser.add_argument('-l', '--logfile', help='Log file', default='slog.txt')
return parser.parse_args()
def main():
args = args_parse()
cfg = config.Config(args.config)
log.initLogger(args.logfile)
try:
server = PhotoImporterServer(cfg)
logging.info("Photo importer server up.")
2018-07-15 16:43:56 +02:00
server.serve_forever()
2018-06-25 09:47:59 +02:00
except KeyboardInterrupt:
server.socket.close()
logging.info("Photo importer server down.")
if __name__ == '__main__':
main()