def update_object(path, meta): path = path.rstrip('/').lstrip('/') meta_fpath = os.path.join(config.shared_db, path).rstrip('/') + config.special_extension infos = loads(open(meta_fpath, 'rb').read()) infos.update(meta) open(meta_fpath, 'wb').write(dumps(infos).encode())
def cb(path='/'): path = _fix_path(path) log.debug('~ Updating %r', path) # TODO: session + permission mgmt m = loads(bottle.request.POST['meta']) root_objects.update_object(path, m) return {}
def get_object_from_path(path): """ returns metadata for an item from its path. List of supported metadata: - id - size - name - descr - mime :arg path: the path of the item :type path: str """ # print('GET %s'%(path)) # TODO: pure metadata reading // a metadata injector will act asychronously path = path.rstrip('/').lstrip('/') fpath = os.path.join(config.shared_root, path).rstrip('/') meta_fpath = os.path.join(config.shared_db, path).rstrip('/') + config.special_extension infos = None try: # print('load %s'%meta_fpath) infos = loads(open(meta_fpath, 'rb').read()) except (OSError, IOError, FileNotFoundError): if not os.path.exists(fpath): return {'error': True, 'message': 'File not found', 'link': path} # or "choices" + "default", instead of link else: file_type = guess_type(fpath) # We have to create an item, we don't have infos yet ! if not infos: st = os.stat(fpath) if '/' in path: name = path.rsplit('/', 1)[1] else: name = path infos = {'size': st.st_size, 'link': name, 'title': (name.rsplit('.', 1)[0] if '.' else name).replace('-', ' ').replace('_', ' ').strip().title(), 'descr': '', 'mime': file_type} # create parent directory parent = os.path.dirname( meta_fpath ) if not os.path.exists( parent ): try: os.makedirs( parent ) except (OSError, FileExistsError): pass # save it if path: # do not save root try: open(meta_fpath, 'wb').write(dumps(infos).encode()) except (OSError, IOError, PermissionError) as e: log.error('Unable to save metadata as %r: %r', meta_fpath, e) return infos