ftpd.py 1.2 KB
Newer Older
Julian Rother's avatar
Julian Rother committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
import os
import requests

from config import *

def on_file_received(handler, path):
	if path.startswith(RELEASED_TMP):
		path = path[len(RELEASED_TMP):].lstrip('/')
		requests.get(API_BASEURL+'/internal/sort/encoded/'+path, params={'apikey': API_APIKEY})
	elif path.startswith(RAW_TMP):
		path = path[len(RAW_TMP):].lstrip('/')
		num = 0
16 17
		rpath = path
		while os.path.exists(RAW_TARGET+'/'+rpath):
Julian Rother's avatar
Julian Rother committed
18
			num += 1
19 20 21 22
			p = path.rsplit('.', 1)
			rpath = '.'.join([p[0]+'_'+str(num)]+p[1:])
		os.rename(RAW_TMP+'/'+path, RAW_TARGET+'/'+rpath)
		requests.get(API_BASEURL+'/internal/sort/autoencode', params={'apikey': API_APIKEY, 'path': rpath})
Julian Rother's avatar
Julian Rother committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36
	else:
		print("Unknown path:", path)

authorizer = DummyAuthorizer()
authorizer.add_user('kodiert', PASSWORD, RELEASED_TMP, perm='elradfwMT') # No MKD!
authorizer.add_user('autoencode', PASSWORD, RAW_TMP, perm='elradfwMT') # No MKD!

handler = FTPHandler
handler.banner = "Video AG upload ftpd ready."
handler.authorizer = authorizer
handler.on_file_received = on_file_received

server = FTPServer((FTPD_ADDR, FTPD_PORT), handler)
server.serve_forever()