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
1 changed files with 32 additions and 18 deletions

View File

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