2013-04-03 21:31:44 +08:00
|
|
|
|
|
|
|
|
from webob.dec import wsgify
|
|
|
|
|
from webob.exc import *
|
|
|
|
|
from webob import Response
|
|
|
|
|
|
2013-04-04 03:50:32 +08:00
|
|
|
# TODO: I don't think this should have to be at the top of every module!
|
|
|
|
|
import sys
|
|
|
|
|
sys.path.insert(0, "/usr/share/anki")
|
|
|
|
|
|
|
|
|
|
#import anki
|
|
|
|
|
#from anki.sync import HttpSyncServer, CHUNK_SIZE
|
|
|
|
|
#from anki.db import sqlite
|
|
|
|
|
#from anki.utils import checksum
|
2013-04-03 21:31:44 +08:00
|
|
|
import anki
|
2013-04-04 03:50:32 +08:00
|
|
|
from anki.sync import LocalServer, MediaSyncer
|
|
|
|
|
# TODO: shouldn't use this directly! This should be through the thread pool
|
|
|
|
|
from anki.storage import Collection
|
2013-04-03 21:31:44 +08:00
|
|
|
|
2013-04-04 03:50:32 +08:00
|
|
|
#import AnkiServer.deck
|
2013-04-03 21:31:44 +08:00
|
|
|
|
2013-04-04 03:50:32 +08:00
|
|
|
#import MySQLdb
|
2013-04-03 21:31:44 +08:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
import simplejson as json
|
|
|
|
|
except ImportError:
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
import os, zlib, tempfile, time
|
|
|
|
|
|
2013-04-04 03:50:32 +08:00
|
|
|
class SyncCollectionHandler(LocalServer):
|
|
|
|
|
operations = ['meta', 'applyChanges', 'start', 'chunk', 'applyChunk', 'sanityCheck2', 'finish']
|
|
|
|
|
|
|
|
|
|
def __init__(self, col):
|
|
|
|
|
LocalServer.__init__(self, col)
|
|
|
|
|
|
|
|
|
|
class SyncMediaHandler(MediaSyncer):
|
|
|
|
|
operations = ['remove', 'files', 'addFiles', 'mediaSanity']
|
|
|
|
|
|
|
|
|
|
def __init__(self, col):
|
|
|
|
|
MediaSyncer.__init__(self, col)
|
|
|
|
|
|
|
|
|
|
# Client passes 'minUsn' but MediaSyncer doesn't have the argument
|
|
|
|
|
def files(self, minUsn=0):
|
|
|
|
|
return MediaSyncer.files(self)
|
|
|
|
|
|
|
|
|
|
class SyncUser(object):
|
|
|
|
|
def __init__(self, name, path):
|
|
|
|
|
# make sure the user path exists
|
|
|
|
|
if not os.path.exists(path):
|
|
|
|
|
os.mkdir(path)
|
|
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
self.name = name
|
|
|
|
|
self.path = path
|
|
|
|
|
self.version = 0
|
|
|
|
|
self.created = time.time()
|
|
|
|
|
|
|
|
|
|
def get_collection_path(self):
|
|
|
|
|
return os.path.realpath(os.path.join(self.path, 'collection.anki2'))
|
2013-04-03 21:31:44 +08:00
|
|
|
|
|
|
|
|
class SyncApp(object):
|
2013-04-04 03:50:32 +08:00
|
|
|
valid_urls = SyncCollectionHandler.operations + SyncMediaHandler.operations + ['hostKey', 'upload']
|
2013-04-03 21:31:44 +08:00
|
|
|
|
|
|
|
|
def __init__(self, **kw):
|
|
|
|
|
self.data_root = os.path.abspath(kw.get('data_root', '.'))
|
|
|
|
|
self.base_url = kw.get('base_url', '/')
|
|
|
|
|
self.users = {}
|
|
|
|
|
|
|
|
|
|
# make sure the base_url has a trailing slash
|
|
|
|
|
if len(self.base_url) == 0:
|
|
|
|
|
self.base_url = '/'
|
|
|
|
|
elif self.base_url[-1] != '/':
|
|
|
|
|
self.base_url = base_url + '/'
|
|
|
|
|
|
2013-04-04 03:50:32 +08:00
|
|
|
def authenticate(self, username, password):
|
|
|
|
|
"""Override this to change how users are authenticated."""
|
|
|
|
|
# TODO: This should have the exact opposite default ;-)
|
2013-04-03 21:31:44 +08:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def username2dirname(self, username):
|
2013-04-04 03:50:32 +08:00
|
|
|
"""Override this to adjust the mapping between users and their directory."""
|
2013-04-03 21:31:44 +08:00
|
|
|
return username
|
2013-04-04 03:50:32 +08:00
|
|
|
|
|
|
|
|
def generateHostKey(self, username):
|
|
|
|
|
import hashlib, time, random, string
|
|
|
|
|
chars = string.ascii_letters + string.digits
|
|
|
|
|
val = ':'.join([username, str(int(time.time())), ''.join(random.choice(chars) for x in range(8))])
|
|
|
|
|
return hashlib.md5(val).hexdigest()
|
|
|
|
|
|
|
|
|
|
def _decode_data(self, data, compression=0):
|
|
|
|
|
import gzip, StringIO
|
|
|
|
|
|
|
|
|
|
if compression:
|
|
|
|
|
buf = gzip.GzipFile(mode="rb", fileobj=StringIO.StringIO(data))
|
|
|
|
|
data = buf.read()
|
|
|
|
|
buf.close()
|
|
|
|
|
|
|
|
|
|
# really lame check for JSON
|
|
|
|
|
if data[0] == '{' and data[-1] == '}':
|
|
|
|
|
data = json.loads(data)
|
|
|
|
|
else:
|
|
|
|
|
data = {'data': data}
|
2013-04-03 21:31:44 +08:00
|
|
|
|
2013-04-04 03:50:32 +08:00
|
|
|
return data
|
2013-04-03 21:31:44 +08:00
|
|
|
|
|
|
|
|
@wsgify
|
|
|
|
|
def __call__(self, req):
|
2013-04-04 03:50:32 +08:00
|
|
|
print req.path
|
2013-04-03 21:31:44 +08:00
|
|
|
if req.path.startswith(self.base_url):
|
|
|
|
|
url = req.path[len(self.base_url):]
|
|
|
|
|
if url not in self.valid_urls:
|
|
|
|
|
raise HTTPNotFound()
|
2013-04-04 03:50:32 +08:00
|
|
|
|
2013-04-03 21:31:44 +08:00
|
|
|
try:
|
2013-04-04 03:50:32 +08:00
|
|
|
compression = req.POST['c']
|
2013-04-03 21:31:44 +08:00
|
|
|
except KeyError:
|
2013-04-04 03:50:32 +08:00
|
|
|
compression = 0
|
|
|
|
|
|
2013-04-03 21:31:44 +08:00
|
|
|
try:
|
2013-04-04 03:50:32 +08:00
|
|
|
data = req.POST['data'].file.read()
|
|
|
|
|
data = self._decode_data(data, compression)
|
2013-04-03 21:31:44 +08:00
|
|
|
except KeyError:
|
2013-04-04 03:50:32 +08:00
|
|
|
data = None
|
|
|
|
|
except ValueError:
|
|
|
|
|
# Bad JSON
|
|
|
|
|
raise HTTPBadRequest()
|
|
|
|
|
print 'data:', data
|
|
|
|
|
|
|
|
|
|
if url == 'hostKey':
|
2013-04-03 21:31:44 +08:00
|
|
|
try:
|
2013-04-04 03:50:32 +08:00
|
|
|
u = data['u']
|
|
|
|
|
p = data['p']
|
2013-04-03 21:31:44 +08:00
|
|
|
except KeyError:
|
2013-04-04 03:50:32 +08:00
|
|
|
raise HTTPForbidden('Must pass username and password')
|
|
|
|
|
if self.authenticate(u, p):
|
|
|
|
|
dirname = self.username2dirname(u)
|
|
|
|
|
if dirname is None:
|
|
|
|
|
raise HTTPForbidden()
|
|
|
|
|
|
|
|
|
|
# setup user and map to a hkey
|
|
|
|
|
hkey = self.generateHostKey(u)
|
|
|
|
|
user_path = os.path.join(self.data_root, dirname)
|
|
|
|
|
self.users[hkey] = SyncUser(u, user_path)
|
|
|
|
|
|
|
|
|
|
result = {'key': hkey}
|
|
|
|
|
return Response(
|
|
|
|
|
status='200 OK',
|
|
|
|
|
content_type='application/json',
|
|
|
|
|
body=json.dumps(result))
|
|
|
|
|
else:
|
|
|
|
|
# TODO: do I have to pass 'null' for the client to receive None?
|
|
|
|
|
raise HTTPForbidden('null')
|
|
|
|
|
|
|
|
|
|
# verify the hostkey
|
|
|
|
|
try:
|
|
|
|
|
hkey = req.POST['k']
|
|
|
|
|
user = self.users[hkey]
|
|
|
|
|
except KeyError:
|
|
|
|
|
raise HTTPForbidden()
|
2013-04-03 21:31:44 +08:00
|
|
|
|
2013-04-04 03:50:32 +08:00
|
|
|
if url in SyncCollectionHandler.operations + SyncMediaHandler.operations:
|
|
|
|
|
# TODO: use thread pool!
|
|
|
|
|
col = Collection(user.get_collection_path())
|
2013-04-03 21:31:44 +08:00
|
|
|
|
2013-04-04 03:50:32 +08:00
|
|
|
if url in SyncCollectionHandler.operations:
|
|
|
|
|
handler = SyncCollectionHandler(col)
|
2013-04-03 21:31:44 +08:00
|
|
|
else:
|
2013-04-04 03:50:32 +08:00
|
|
|
handler = SyncMediaHandler(col)
|
|
|
|
|
|
|
|
|
|
func = getattr(handler, url)
|
2013-04-03 21:31:44 +08:00
|
|
|
|
2013-04-04 03:50:32 +08:00
|
|
|
# 'meta' passes the SYNC_VER but it isn't used in the handler
|
|
|
|
|
if url == 'meta' and data.has_key('v'):
|
|
|
|
|
user.version = data['v']
|
|
|
|
|
del data['v']
|
2013-04-03 21:31:44 +08:00
|
|
|
|
2013-04-04 03:50:32 +08:00
|
|
|
try:
|
|
|
|
|
result = func(**data)
|
|
|
|
|
#except Exception, e:
|
|
|
|
|
# print e
|
|
|
|
|
# raise HTTPInternalServerError()
|
|
|
|
|
finally:
|
|
|
|
|
col.close()
|
|
|
|
|
|
|
|
|
|
print result
|
|
|
|
|
return Response(
|
|
|
|
|
status='200 OK',
|
|
|
|
|
content_type='application/json',
|
|
|
|
|
body=json.dumps(result))
|
|
|
|
|
|
|
|
|
|
elif url == 'upload':
|
|
|
|
|
# TODO: deal with thread pool
|
|
|
|
|
|
|
|
|
|
fd = open(user.get_collection_path(), 'wb')
|
|
|
|
|
fd.write(data['data'])
|
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
|
|
return Response(
|
|
|
|
|
status='200 OK',
|
|
|
|
|
content_type='text/plain',
|
|
|
|
|
body='OK')
|
|
|
|
|
|
|
|
|
|
# TODO: turn this into a 500 error in the future!
|
|
|
|
|
return Response(status='503 Temporarily Unavailable ', content_type='text/plain', body='This operation isn\'t implemented yet.')
|
|
|
|
|
|
|
|
|
|
return Response(status='200 OK', content_type='text/plain', body='Anki Sync Server')
|
2013-04-03 21:31:44 +08:00
|
|
|
|
|
|
|
|
# Our entry point
|
|
|
|
|
def make_app(global_conf, **local_conf):
|
|
|
|
|
return SyncApp(**local_conf)
|
|
|
|
|
|
|
|
|
|
def main():
|
2013-04-04 03:50:32 +08:00
|
|
|
|
2013-04-03 21:31:44 +08:00
|
|
|
from wsgiref.simple_server import make_server
|
|
|
|
|
|
2013-04-04 03:50:32 +08:00
|
|
|
ankiserver = SyncApp()
|
2013-04-03 21:31:44 +08:00
|
|
|
httpd = make_server('', 8001, ankiserver)
|
|
|
|
|
try:
|
2013-04-04 03:50:32 +08:00
|
|
|
print "Starting..."
|
2013-04-03 21:31:44 +08:00
|
|
|
httpd.serve_forever()
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
print "Exiting ..."
|
|
|
|
|
finally:
|
2013-04-04 03:50:32 +08:00
|
|
|
#AnkiServer.deck.thread_pool.shutdown()
|
|
|
|
|
pass
|
2013-04-03 21:31:44 +08:00
|
|
|
|
|
|
|
|
if __name__ == '__main__': main()
|
|
|
|
|
|