def get_data(cls, uuids): rv = list() storage_setup = current_app.config['storage'] if 'local' in storage_setup and 'absolut_path' in storage_setup[ 'local']: STORAGE = storage_setup['local']['absolut_path'] for uuid in uuids: vnode = CaliopeDocument.pull(uuid) filename = os.path.join(STORAGE, vnode.uuid) size = os.stat(filename).st_size data = { 'result': 'ok', 'name': vnode.filename, 'size': human_readable_size(size), 'mime': vnode.mimetype, 'id': vnode.uuid, 'thumbnail': get_thumbnail(filename, field_name='data', mimetype=vnode.mimetype) } rv.append(data) return rv
def uploader(): if request.method == 'POST': #print request.form['id'] #print str(request.form.viewitems()) attachment_params = {k: v[0] for k, v in [x for x in request.form.viewitems()]} if 'session_uuid' in request.form: if LoginManager().check_with_uuid(request.form['session_uuid']): print "OK" else: return "unrecheable" else: return "unrecheable" app = current_app storage_setup = app.config['storage'] if 'local' in storage_setup and 'absolut_path' in storage_setup['local']: UPLOAD_FOLDER = storage_setup['local']['absolut_path'] if 'local' in storage_setup and 'allowed_extensions' in storage_setup['local']: ALLOWED_EXTENSIONS = storage_setup['local']['allowed_extensions'] rv = [] for uploaded_file in request.files.getlist('files[]'): filename = secure_filename(uploaded_file.filename) if uploaded_file and allowed_file(uploaded_file.filename, ALLOWED_EXTENSIONS): model = FormManager().get_empty_model('CaliopeDocument', data=True) idfile = model['data']['uuid']['value'] uploaded_file.save(os.path.join(UPLOAD_FOLDER, idfile)) mimetype = mimetypes.guess_type(filename)[0] if mimetype is None: mimetype = 'application/octet-stream' CaliopeServices().update_field(idfile, 'mimetype', mimetype) CaliopeServices().update_field(idfile, 'filename', filename) CaliopeServices().update_field(idfile, 'url', local_document_url('', idfile, '')) #DocumentProcess().enqueue(doc) result = { 'result': 'ok', 'name': filename, 'size': human_readable_size(uploaded_file.tell()), 'id': idfile, 'thumbnail': get_thumbnail(os.path.join(UPLOAD_FOLDER, idfile), mimetype=mimetype, field_name='data') } CaliopeServices().update_relationship(attachment_params['uuid'], attachment_params['field'], idfile) else: result = { 'result': 'error', 'name': filename } rv.append(result); return json.dumps(rv) return "unrecheable"
def get_all_with_thumbnails(cls, context=None, thumbnails=False): rv = cls.get_all(context=context, recursive=False) # TODO: move to DocumentServices user_node = CaliopeUser.index.get(username=LoginManager().get_user()) storage_setup = current_app.config["storage"] if "local" in storage_setup and "absolut_path" in storage_setup["local"]: STORAGE = storage_setup["local"]["absolut_path"] for form in rv[0]["instances"]: # TODO: optimize results, metadata = user_node.cypher( """ START form=node:CaliopeStorage(uuid='{uuid}') MATCH pa=(form)-[*1..2]-(file)<-[CALIOPE_DOCUMENT]-(),p_none=(file)<-[?:PARENT*]-() WHERE p_none = null and has(file.url) return distinct file """.format( uuid=form["uuid"] ) ) # TODO: use cache to thumbnails attachments = list() for row in results: attachment = row[0] file_uuid = attachment["uuid"] node = CaliopeDocument.pull(file_uuid) if thumbnails: filename = os.path.join(STORAGE, file_uuid) size = os.stat(filename).st_size data = { "uuid": file_uuid, "name": node.filename, "size": human_readable_size(size), "thumbnail": get_thumbnail(filename, field_name="data", mimetype=node.mimetype), } else: data = {"uuid": file_uuid, "name": node.filename} attachments.append(data) form["attachments"] = attachments return rv
def get_all_with_thumbnails(cls, context=None, thumbnails=False): rv = cls.get_all(context=context, recursive=False) #TODO: move to DocumentServices user_node = CaliopeUser.index.get(username=LoginManager().get_user()) storage_setup = current_app.config['storage'] if 'local' in storage_setup and 'absolut_path' in storage_setup['local']: STORAGE = storage_setup['local']['absolut_path'] for form in rv[0]['instances']: #TODO: optimize results, metadata = user_node.cypher(""" START form=node:CaliopeStorage(uuid='{uuid}') MATCH pa=(form)-[*1..2]-(file)<-[CALIOPE_DOCUMENT]-(),p_none=(file)<-[?:PARENT*]-() WHERE p_none = null and has(file.url) return distinct file """.format(uuid=form['uuid'])) #TODO: use cache to thumbnails attachments = list() for row in results: attachment = row[0] file_uuid = attachment['uuid'] node = CaliopeDocument.pull(file_uuid) if thumbnails: filename = os.path.join(STORAGE, file_uuid) size = os.stat(filename).st_size data = { 'uuid': file_uuid, 'name': node.filename, 'size': human_readable_size(size), 'thumbnail': get_thumbnail(filename, field_name='data', mimetype=node.mimetype) } else: data = { 'uuid': file_uuid, 'name': node.filename } attachments.append(data) form['attachments'] = attachments return rv
def get_data(cls, uuids): rv = list() storage_setup = current_app.config["storage"] if "local" in storage_setup and "absolut_path" in storage_setup["local"]: STORAGE = storage_setup["local"]["absolut_path"] for uuid in uuids: vnode = CaliopeDocument.pull(uuid) filename = os.path.join(STORAGE, vnode.uuid) size = os.stat(filename).st_size data = { "result": "ok", "name": vnode.filename, "size": human_readable_size(size), "mime": vnode.mimetype, "id": vnode.uuid, "thumbnail": get_thumbnail(filename, field_name="data", mimetype=vnode.mimetype), } rv.append(data) return rv
def uploader(): if request.method == 'POST': #print request.form['id'] #print str(request.form.viewitems()) attachment_params = { k: v[0] for k, v in [x for x in request.form.viewitems()] } if 'session_uuid' in request.form: if LoginManager().check_with_uuid(request.form['session_uuid']): print "OK" else: return "unrecheable" else: return "unrecheable" app = current_app storage_setup = app.config['storage'] if 'local' in storage_setup and 'absolut_path' in storage_setup[ 'local']: UPLOAD_FOLDER = storage_setup['local']['absolut_path'] if 'local' in storage_setup and 'allowed_extensions' in storage_setup[ 'local']: ALLOWED_EXTENSIONS = storage_setup['local']['allowed_extensions'] rv = [] for uploaded_file in request.files.getlist('files[]'): filename = secure_filename(uploaded_file.filename) if uploaded_file and allowed_file(uploaded_file.filename, ALLOWED_EXTENSIONS): model = FormManager().get_empty_model('CaliopeDocument', data=True) idfile = model['data']['uuid']['value'] uploaded_file.save(os.path.join(UPLOAD_FOLDER, idfile)) mimetype = mimetypes.guess_type(filename)[0] if mimetype is None: mimetype = 'application/octet-stream' CaliopeServices().update_field(idfile, 'mimetype', mimetype) CaliopeServices().update_field(idfile, 'filename', filename) CaliopeServices().update_field( idfile, 'url', local_document_url('', idfile, '')) #DocumentProcess().enqueue(doc) result = { 'result': 'ok', 'name': filename, 'size': human_readable_size(uploaded_file.tell()), 'id': idfile, 'thumbnail': get_thumbnail(os.path.join(UPLOAD_FOLDER, idfile), mimetype=mimetype, field_name='data') } CaliopeServices().update_relationship( attachment_params['uuid'], attachment_params['field'], idfile) else: result = {'result': 'error', 'name': filename} rv.append(result) return json.dumps(rv) return "unrecheable"