support multiple instances of same document, extend url from api url

This commit is contained in:
j 2016-10-26 23:35:49 +02:00
parent 41bb911859
commit db3c235e15

View file

@ -812,23 +812,33 @@ class Client(object):
if f['id'] not in available and f['extension'] in DOCUMENT_FORMATS] if f['id'] not in available and f['extension'] in DOCUMENT_FORMATS]
return missing return missing
def find_document(self, oshash):
r = self.api.findDocuments({
'keys': ['id'],
'query': {
'conditions': [
{'key': 'oshash', 'value': oshash, 'operator': '=='}
],
'operator': '&'
}
})
if r['data']['items']:
return r['data']['items'][0]['id']
return None
def _add_document(self, f, item=None): def _add_document(self, f, item=None):
if f.split('.')[-1] not in DOCUMENT_FORMATS: if f.split('.')[-1] not in DOCUMENT_FORMATS:
return False return False
oshash = ox.oshash(f)
did = self.find_document(oshash)
if not did:
url = '%supload/document/' % self._config['url'] url = '%supload/document/' % self._config['url']
r = self.api.upload_chunks(url, f, { r = self.api.upload_chunks(url, f, {
'filename': os.path.basename(f) 'filename': os.path.basename(f)
}) })
if r and item: if r and item:
oshash = ox.oshash(f) did = self.find_document(oshash)
r = self.api.findDocuments({ if item:
"keys": ['id'],
"query": {
"conditions": [{"key": 'oshash', "value": oshash, "operator": '=='}],
"operator": '&'
}
})
did = r['data']['items'][0]['id']
r = self.api.addDocument({ r = self.api.addDocument({
'id': did, 'id': did,
'item': item 'item': item
@ -1061,12 +1071,16 @@ class API(ox.API):
print(filename) print(filename)
hide_cursor() hide_cursor()
result_url = data.get('url')
if 'uploadUrl' in data: def full_url(path):
uploadUrl = data['uploadUrl'] if path.startswith('/'):
if uploadUrl.startswith('/'):
u = urlparse(url) u = urlparse(url)
uploadUrl = '%s://%s%s' % (u.scheme, u.netloc, uploadUrl) path = '%s://%s%s' % (u.scheme, u.netloc, path)
return path
result_url = full_url(data.get('url'))
if 'uploadUrl' in data:
uploadUrl = full_url(data['uploadUrl'])
f = open(filename, 'rb') f = open(filename, 'rb')
fsize = os.stat(filename).st_size fsize = os.stat(filename).st_size
done = 0 done = 0