Exemplo n.º 1
0
    def GET(self):
        path = xutils.get_argument("path")
        length = 1000
        read = xutils.get_argument("read", "false")
        direction = xutils.get_argument("direction", "forward")
        encoding = "utf-8"
        page = 0

        if not path:
            return dict(code="fail", message="parameter path is empty")

        print("path:", path)
        path = xutils.get_real_path(path)
        print("real path:", path)

        if not os.path.exists(path):
            return dict(code="fail", message="file `%s` not exists" % path)

        basename, ext = os.path.splitext(path)
        key = "bookmark@%s@%s" % (xauth.current_name(), xutils.md5_hex(path))
        bookmark = xutils.cache_get(key, {})
        bookmark['path'] = path
        # bookmarkpath = '%s@%s.bookmark' % (xauth.get_current_name(), basename)
        # bookmark = dict()
        # if os.path.exists(bookmarkpath):
        #     try:
        #         bookmark = json.loads(xutils.readfile(bookmarkpath))
        #         if not isinstance(bookmark, dict):
        #             bookmark = dict()
        #     except:
        #         pass

        page = bookmark.get("page", 0)
        size = xutils.get_file_size(path, format=False)

        with open(path, encoding=encoding) as fp:
            text = "dummy"
            if direction == "backward":
                page = page - 1
            if direction == "forward":
                page = page + 1
            if page < 0:
                page = 0
            bookmark["page"] = page
            self.seek_page(fp, bookmark, length)
            current = fp.tell()
            text = fp.read(length)
            if read == "true":
                xutils.say(text)
            if direction in ("forward", "backward"):
                # xutils.writefile(bookmarkpath, json.dumps(bookmark))
                xutils.cache_put(key, bookmark)
        return dict(code="success",
                    data=text,
                    page=page,
                    current=current,
                    size=size)
Exemplo n.º 2
0
    def GET(self):
        path      = xutils.get_argument("path")
        length    = 1000
        read      = xutils.get_argument("read", "false")
        direction = xutils.get_argument("direction", "forward")
        page      = 0

        if not path:
            return dict(code = "fail", message = "parameter path is empty")

        debug_info("path:", path)
        path = xutils.get_real_path(path)
        debug_info("real path:", path)

        if not os.path.exists(path):
            return dict(code = "fail", message = "file `%s` not exists" % path)

        basename, ext    = os.path.splitext(path)
        key              = "bookmark@%s@%s" % (xauth.current_name(), xutils.md5_hex(path))
        bookmark         = xutils.cache_get(key, {})
        bookmark['path'] = path
        page             = bookmark.get("page", 0)
        size             = xutils.get_file_size(path, format=False)
        debug_info("bookmark info:", bookmark)

        encoding = fsutil.detect_encoding(path)
        debug_info("detected encoding:", encoding)
        
        with open(path, encoding = encoding) as fp:
            text = "dummy"
            if direction == "backward":
                page = page - 1
            if direction == "forward":
                page = page + 1
            if page < 0:
                page = 0
            try:
                bookmark["page"] = page
                seek_page(fp, bookmark, length)
                current = fp.tell()
                text    = fp.read(length)
            except UnicodeDecodeError as e:
                # xutils.print_exc()
                bookmark['page'] = 0
                seek_page(fp, bookmark, length);
                current = fp.tell()
                text    = fp.read()

            if read == "true":
                xutils.say(text)

            if direction in ("forward", "backward"):
                # xutils.writefile(bookmarkpath, json.dumps(bookmark))
                xutils.cache_put(key, bookmark)
        return dict(code="success", data=text, page=page, current=current, size=size)
Exemplo n.º 3
0
    def GET(self):
        tpl = "fs/fs_index.html"
        fslist = xutils.cache_get("fs.list")
        if fslist:
            index_size = len(fslist)
        else:
            index_size = 0

        return xtemplate.render(tpl, 
            index_size = index_size,
            show_aside = (xconfig.OPTION_STYLE == "aside"))
Exemplo n.º 4
0
    def GET(self):
        key  = xutils.get_argument("key")
        user = xauth.get_current_name()
        default_value = ""

        if key == "settings":
            default_value = DEFAULT_SETTINGS

        config = Storage(key = key, value = xutils.cache_get("%s@prop_%s" % (user, key), 
            default_value))

        if config is None:
            config = Storage(key=key, value="")
        return xtemplate.render("system/properties.html", 
            show_aside = False,
            config = config)
Exemplo n.º 5
0
def list_tag(user_name):
    t = Timer()
    t.start()
    cache_key = "%s@tag_list" % user_name
    tag_list = xutils.cache_get(cache_key)
    tag_list = None
    if tag_list is None:
        db = xtables.get_file_tag_table()
        sql = """SELECT LOWER(name) AS name, COUNT(*) AS amount FROM file_tag 
            WHERE (user=$user OR is_public=1) 
            GROUP BY LOWER(name) ORDER BY amount DESC, name ASC"""
        tag_list = list(db.query(sql, vars=dict(user=user_name)))
        xutils.cache_put(cache_key, tag_list, 60 * 10)
    t.stop()
    xutils.trace("NoteDao.ListTag", "", t.cost_millis())
    return tag_list
Exemplo n.º 6
0
def list_search_history(user_name, limit=-1):
    history = list(
        reversed(xutils.cache_get("%s@search_history" % user_name, [])))
    if limit > 0:
        return history[:limit]
    return history