示例#1
0
    def scan(self):
        pat = itertools.cycle(r'|/-\\')
        # FIXME in whoosh: using the same indexer for the whole session makes things wrong
        reset()
        max_size = MAX_SIZE
        pfx_len = len(config.shared_root)
        c = itertools.count()
        writer = get_writer()
        w = writer.add_document
        for root, dirs, files in os.walk(config.shared_root):
            for fname in files:
                try:
                    path = os.path.join(root, fname)
                    if os.stat(path).st_size > max_size:
                        d = ''
                    else:
                        d = open( path, 'rb' ).read()
                except Exception:
                    log.warning("Can't open %r in %r !", fname, root)
                else:
                    m = guess_type(path)
                    is_text = m.startswith('text')
                    if is_text:
                        try:
                            d = d.decode('utf-8')
                        except UnicodeDecodeError:
                            d = d.decode('latin1')
                    w(path=path[pfx_len:], mime=guess_type(path), description='', txtcontent=d if is_text else '')
                    i = next(c)
                    print(' Collected {:d} items {:s} {:35s}'.format(i, next(pat), m), end='\r')
        try:
            writer.commit()
        except Exception: # It might be closed/commited yet
            pass

        indexer.optimize()
        print("\ndone.")
示例#2
0
def cb():
    log.debug('~ Uploading!')
    bottle.response.set_header('Content-Type', 'application/json')
    yield
    prefix = os.path.join(config.shared_root, bottle.request.POST['prefix'].lstrip('/'))
    if prefix[-1] != '/':
        prefix += '/'
    items = []
    errors = []
    for f in bottle.request.files.values():
        fname = prefix+f.filename
        for t, d in root_objects.save_object_to_path(fname, f.file.read):
            if t == 'err':
                errors.append(d)
            elif t == 'new':
                items.append( [d, guess_type(d)] )
            yield
    yield bottle.json_dumps( {'error':errors or False, 'children': {'c':['link', 'mime'], 'r':items} } )