2018-06-25 09:47:59 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import cgi
|
|
|
|
import json
|
2018-09-22 23:34:44 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
2019-01-27 03:05:59 +02:00
|
|
|
FIXED_IN_PATH_NAME = 'none'
|
|
|
|
|
|
|
|
|
2018-06-25 09:47:59 +02:00
|
|
|
class PhotoImporterHandler(http.server.BaseHTTPRequestHandler):
|
2019-01-27 03:05:59 +02:00
|
|
|
|
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'))
|
|
|
|
|
2019-01-27 03:05:59 +02:00
|
|
|
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)
|
|
|
|
|
2018-09-22 23:34:44 +02:00
|
|
|
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):
|
2018-09-22 23:34:44 +02:00
|
|
|
mount_list = self.__get_mounted_list()
|
2018-06-25 09:47:59 +02:00
|
|
|
dev_list = os.listdir('/dev')
|
2019-01-27 03:05:59 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2018-09-22 23:34:44 +02:00
|
|
|
res = {}
|
2018-06-25 09:47:59 +02:00
|
|
|
for dev in dev_list:
|
2019-01-27 03:05:59 +02:00
|
|
|
if re.match(self.server.remote_drive_reg(), dev) \
|
2019-02-02 10:56:22 +02:00
|
|
|
or dev == FIXED_IN_PATH_NAME:
|
|
|
|
|
2018-09-22 23:34:44 +02:00
|
|
|
r = {}
|
|
|
|
r['path'] = mount_list.get('/dev/' + dev, '')
|
2018-09-23 21:55:10 +02:00
|
|
|
r['progress'] = 0
|
2018-09-22 23:34:44 +02:00
|
|
|
if r['path']:
|
2018-09-23 21:55:10 +02:00
|
|
|
stat = self.server.import_status(r['path'])
|
2018-09-22 23:34:44 +02:00
|
|
|
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'])
|
2019-01-27 03:05:59 +02:00
|
|
|
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'
|
2018-09-22 23:34:44 +02:00
|
|
|
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
|
2019-02-02 10:56:22 +02:00
|
|
|
logging.debug('pmount')
|
2018-09-23 22:35:44 +02:00
|
|
|
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
|
2019-02-02 10:56:22 +02:00
|
|
|
logging.debug('pumount')
|
2018-09-23 22:35:44 +02:00
|
|
|
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
|
|
|
|
2018-09-22 23:34:44 +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
|
|
|
|
|
2018-09-22 23:34:44 +02:00
|
|
|
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
|
|
|
|
|
2019-01-27 03:05:59 +02:00
|
|
|
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]
|
2019-01-30 22:41:32 +02:00
|
|
|
except Exception as ex:
|
2018-09-23 21:55:10 +02:00
|
|
|
self.__bad_request_response(str(ex))
|
|
|
|
logging.exception(ex)
|
|
|
|
return
|
|
|
|
|
|
|
|
result = None
|
|
|
|
|
|
|
|
if action == 'start':
|
|
|
|
result = self.__import_start(in_path)
|
2019-01-27 03:05:59 +02:00
|
|
|
self.__ok_response(result)
|
2018-09-23 21:55:10 +02:00
|
|
|
elif action == 'stop':
|
|
|
|
result = self.__import_stop(in_path)
|
2019-01-27 03:05:59 +02:00
|
|
|
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
|
|
|
|
|
2018-09-22 23:34:44 +02:00
|
|
|
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:
|
2019-01-27 03:05:59 +02:00
|
|
|
if (path[0]) == '/':
|
|
|
|
path = path[1:]
|
2018-09-22 23:34:44 +02:00
|
|
|
fname = os.path.join(self.server.web_path(), path)
|
2019-01-27 03:05:59 +02:00
|
|
|
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)
|
2019-01-27 03:05:59 +02:00
|
|
|
self.send_header('Content-type', cont)
|
2018-06-25 09:47:59 +02:00
|
|
|
self.end_headers()
|
2019-01-27 03:05:59 +02:00
|
|
|
self.wfile.write(bytearray(f.read()))
|
|
|
|
except IOError as ex:
|
2018-06-25 09:47:59 +02:00
|
|
|
self.__not_found_response()
|
2019-01-27 03:05:59 +02:00
|
|
|
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):
|
2019-02-02 10:56:22 +02:00
|
|
|
logging.debug('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
|
|
|
|
|
2018-09-22 23:34:44 +02:00
|
|
|
if path == '/sysinfo':
|
|
|
|
self.__sysinfo_request(params)
|
|
|
|
return
|
|
|
|
|
2018-06-25 09:47:59 +02:00
|
|
|
if path == '/':
|
|
|
|
path = 'index.html'
|
|
|
|
|
|
|
|
if '..' in path:
|
2019-01-27 03:05:59 +02:00
|
|
|
logging.warning('".." in path: ' + path)
|
2018-06-25 09:47:59 +02:00
|
|
|
self.__not_found_response()
|
|
|
|
return
|
|
|
|
|
|
|
|
ext = os.path.splitext(path)[1]
|
2019-01-27 03:05:59 +02:00
|
|
|
if ext in ('.html', '.js', '.png'):
|
2018-06-25 09:47:59 +02:00
|
|
|
self.__file_request(path)
|
|
|
|
return
|
|
|
|
|
2019-01-27 03:05:59 +02:00
|
|
|
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):
|
2019-02-02 10:56:22 +02:00
|
|
|
logging.debug('do_POST: ' + self.path)
|
2018-06-25 09:47:59 +02:00
|
|
|
try:
|
|
|
|
path, params = self.__path_params()
|
|
|
|
|
2018-09-22 23:34:44 +02:00
|
|
|
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'])
|
2018-09-22 23:34:44 +02:00
|
|
|
self.__web_path = cfg['server']['web_path']
|
2018-06-25 09:47:59 +02:00
|
|
|
self.__remote_drive_reg = cfg['server']['remote_drive_reg']
|
2018-09-22 23:34:44 +02:00
|
|
|
self.__out_path = cfg['server']['out_path']
|
2019-01-27 03:05:59 +02:00
|
|
|
self.__fixed_in_path = cfg['server']['in_path']
|
2018-06-25 09:47:59 +02:00
|
|
|
super().__init__(('', port), PhotoImporterHandler)
|
|
|
|
|
2018-09-22 23:34:44 +02:00
|
|
|
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
|
|
|
|
|
2018-09-22 23:34:44 +02:00
|
|
|
def out_path(self):
|
|
|
|
return self.__out_path
|
|
|
|
|
2019-01-27 03:05:59 +02:00
|
|
|
def fixed_in_path(self):
|
|
|
|
return self.__fixed_in_path
|
|
|
|
|
2018-09-23 21:55:10 +02:00
|
|
|
def import_start(self, in_path):
|
2018-09-23 22:35:44 +02:00
|
|
|
logging.info('import_start: %s', in_path)
|
2019-01-27 03:05:59 +02:00
|
|
|
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
|
|
|
|
|
2018-09-23 22:35:44 +02:00
|
|
|
def import_done(self, in_path):
|
|
|
|
logging.info('import_done: %s', in_path)
|
|
|
|
if in_path in self.__importers:
|
|
|
|
del self.__importers[in_path]
|
|
|
|
|
2019-01-27 03:05:59 +02:00
|
|
|
def get_log(self, in_path):
|
|
|
|
if in_path in self.__importers:
|
|
|
|
return self.__importers[in_path].log_text()
|
|
|
|
else:
|
2019-02-02 10:56:22 +02:00
|
|
|
return ''
|
2019-01-27 03:05:59 +02:00
|
|
|
|
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()
|