Example #1
0
 def POST(self):
     # xutils.print_web_ctx_env()
     chunk = xutils.get_argument("chunk", 0, type=int)
     chunks = xutils.get_argument("chunks", 1, type=int)
     file = xutils.get_argument("file", {})
     dirname = xutils.get_argument("dirname", xconfig.DATA_DIR)
     dirname = dirname.replace("$DATA", xconfig.DATA_DIR)
     # print(file.__dict__)
     # print("%d/%d" % (chunk, chunks))
     filename = None
     if hasattr(file, "filename"):
         # print(" - - %-20s = %s" % ("filename", file.filename))
         xutils.log("recv {}", file.filename)
         filename = os.path.basename(file.filename)
         filename = xutils.quote_unicode(filename)
         # filename = xauth.get_current_name() + '_' + filename
         tmp_name = "%s_%d.part" % (filename, chunk)
         tmp_path = os.path.join(dirname, tmp_name)
         with open(tmp_path, "wb") as fp:
             for file_chunk in file.file:
                 fp.write(file_chunk)
     else:
         return dict(code="fail", message="require file")
     if chunk + 1 == chunks:
         self.merge_files(dirname, filename, chunks)
     return dict(code="success")
Example #2
0
    def POST(self):
        name = xutils.get_argument("username", "")
        pswd = xutils.get_argument("password", "")
        target = xutils.get_argument("target")

        # xutils.print_web_ctx_env()
        # print(web.input())
        xutils.log("USER[%s] PSWD[%s]" % (name, pswd))

        users = xauth.get_users()
        error = ""
        if name == "":
            pass
        if name in users:
            user = users[name]
            if pswd == user["password"]:
                web.setcookie("xuser", name, expires= 24*3600*30)
                pswd_md5 = xauth.get_password_md5(pswd)
                web.setcookie("xpass", pswd_md5, expires=24*3600*30)
                if target is None:
                    raise web.seeother("/")
                raise web.seeother(target)
            else:
                error = "password error"
        else:
            error = "user not exists"
        return xtemplate.render("login.html", 
            username=name, 
            password=pswd,
            error = error)
Example #3
0
    def POST(self):
        user_name = xauth.current_name()
        part_file = True
        chunksize = 5 * 1024 * 1024
        chunk = xutils.get_argument("chunk", 0, type=int)
        chunks = xutils.get_argument("chunks", 1, type=int)
        file = xutils.get_argument("file", {})
        prefix = xutils.get_argument("prefix", "")
        dirname = xutils.get_argument("dirname", xconfig.DATA_DIR)
        dirname = dirname.replace("$DATA", xconfig.DATA_DIR)
        note_id = xutils.get_argument("note_id")

        # 不能访问上级目录
        if ".." in dirname:
            return dict(code="fail", message="can not access parent directory")

        filename = None
        webpath  = ""
        origin_name = ""

        if hasattr(file, "filename"):
            origin_name = file.filename
            xutils.trace("UploadFile", file.filename)
            filename = os.path.basename(file.filename)
            filename = xutils.get_real_path(filename)
            if dirname == "auto":
                filename = generate_filename(filename, prefix)
                filepath, webpath = xutils.get_upload_file_path(user_name, filename, replace_exists=True)
                dirname  = os.path.dirname(filepath)
                filename = os.path.basename(filepath)
            else:
                # TODO check permission.
                pass

            if part_file:
                tmp_name = "%s_%d.part" % (filename, chunk)
                seek = 0
            else:
                tmp_name = filename
                seek = chunk * chunksize

            xutils.makedirs(dirname)
            tmp_path = os.path.join(dirname, tmp_name)

            with open(tmp_path, "wb") as fp:
                fp.seek(seek)
                if seek != 0:
                    xutils.log("seek to {}", seek)
                for file_chunk in file.file:
                    fp.write(file_chunk)
        else:
            return dict(code="fail", message="require file")
        if part_file and chunk+1==chunks:
            self.merge_files(dirname, filename, chunks)

        try_touch_note(note_id)
        if note_id != None and note_id != "":
            xutils.call("note.touch", note_id)
        return dict(code="success", webpath=webpath, link=get_link(origin_name, webpath))
Example #4
0
def rm_expired_files(dirname, expired_time):
    xutils.log("check dirname `%s`" % dirname)
    now = time.time()
    for fname in os.listdir(dirname):
        fpath = os.path.join(dirname, fname)
        st = os.stat(fpath)
        if now - st.st_ctime >= expired_time:
            xutils.log("%s is expired" % fname)
            xutils.rmfile(fpath)
Example #5
0
 def POST(self):
     name     = xutils.get_argument("name")
     password = xutils.get_argument("password")
     xauth.add_user(name, password)
     added = xauth.get_user(name)
     xutils.log("Add user {}", added)
     # 先暴力解决
     xmanager.reload()
     return self.GET()
Example #6
0
 def POST(self):
     name = xutils.get_argument("name")
     password = xutils.get_argument("password")
     xauth.add_user(name, password)
     added = xauth.get_user(name)
     xutils.log("Add user {}", added)
     # 先暴力解决
     xmanager.reload()
     raise web.seeother("/system/user?name=" + name)
Example #7
0
 def GET(self):
     pagesize = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int)
     page = xutils.get_argument("page", 1, type=int)
     status = xutils.get_argument("status")
     key = xutils.get_argument("key")
     offset = (page - 1) * pagesize
     db = xtables.get_message_table()
     user_name = xauth.get_current_name()
     kw = "1=1"
     if status == "created":
         kw = "status = 0"
     if status == "done":
         kw = "status = 100"
     if status == "suspended":
         kw = "status = 50"
     kw += " AND user = $user"
     vars = dict(user=xauth.get_current_name())
     if key != "" and key != None:
         start_time = time.time()
         for item in key.split(" "):
             if item == "":
                 continue
             kw += " AND content LIKE " + fuzzy_item(item)
         # when find numbers, the sql printed is not correct
         # eg. LIKE '%1%' will be LIKE '%'
         # print(kw)
         chatlist = list(
             db.select(where=kw,
                       vars=vars,
                       order="ctime DESC",
                       limit=pagesize,
                       offset=offset))
         end_time = time.time()
         cost_time = int((end_time - start_time) * 1000)
         xutils.log("message search [%s] time %d ms" % (key, cost_time))
         if xconfig.search_history is not None:
             xconfig.search_history.put(
                 Storage(name="#message# %s - %sms" % (key, cost_time),
                         link=web.ctx.fullpath))
     else:
         chatlist = list(
             db.select(where=kw,
                       vars=vars,
                       order="ctime DESC",
                       limit=pagesize,
                       offset=offset))
     chatlist.reverse()
     amount = db.count(where=kw, vars=vars)
     page_max = math.ceil(amount / pagesize)
     chatlist = list(map(process_message, chatlist))
     return dict(code="success",
                 message="",
                 data=chatlist,
                 amount=amount,
                 page_max=page_max,
                 current_user=xauth.get_current_name())
Example #8
0
 def check_and_run(task, tm):
     if self.match(task, tm):
         put_task(request_url, task)
         try:
             xutils.trace("RunTask",  task.url)
             if task.tm_wday == "no-repeat":
                 # 一次性任务直接删除
                 xtables.get_schedule_table().delete(where=dict(id=task.id))
                 self.load_tasks()
         except Exception as e:
             xutils.log("run task [%s] failed, %s" % (task.url, e))
Example #9
0
 def check_and_run(task, tm):
     if self.match(task, tm):
         put_task(request_url, task)
         try:
             xutils.trace("RunTask", task.url)
             if task.tm_wday == "no-repeat":
                 # 一次性任务直接删除
                 dbutil.delete(task.id)
                 self.load_tasks()
         except Exception as e:
             xutils.log("run task [%s] failed, %s" % (task.url, e))
Example #10
0
def handle_signal(signum, frame):
    """处理系统消息
    :arg int signum:
    :arg object frame, current stack frame:
    """
    xutils.log("Signal received: %s" % signum)
    if signum == signal.SIGALRM:
        # 时钟信号
        return
    # 优雅下线
    xmanager.fire("sys.exit")
    exit(0)
Example #11
0
 def execute(self, ctx=None):
     if self.is_async:
         put_task(self.func, ctx)
     else:
         try:
             if self.profile:
                 start = time.time()
             self.func(ctx)
             if self.profile:
                 stop  = time.time()
                 xutils.log("EventHandler %s cost %sms" % (self.key, int((stop-start)*1000)))
         except:
             xutils.print_exc()
Example #12
0
def get_cached_files():
    count = 0
    file_cache = []
    for root, dirs, files in os.walk(xconfig.DATA_DIR):
        for item in dirs:
            path = os.path.join(root, item)
            file_cache.append(path)
            count += 1
        for item in files:
            path = os.path.join(root, item)
            file_cache.append(path)
            count += 1
    xutils.log("files count = {}", count)
    return file_cache
Example #13
0
    def GET(self):
        days = xutils.get_argument("days", 30, type=int)
        page = xutils.get_argument("page", 1, type=int)
        pagesize = xutils.get_argument("pagesize", PAGE_SIZE, type=int)
        page = max(1, page)

        db = xtables.get_file_table()
        t = Timer()
        t.start()
        creator = xauth.get_current_name()
        where = "is_deleted = 0 AND (creator = $creator OR is_public = 1) AND type != 'group'"

        cache_key = "recent_notes#%s#%s" % (creator, page)
        files = cacheutil.get(cache_key)
        if files is None:
            files = list(
                db.select(
                    what="name, id, parent_id, ctime, mtime, type, creator",
                    where=where,
                    vars=dict(creator=creator),
                    order="mtime DESC",
                    offset=(page - 1) * pagesize,
                    limit=pagesize))
            cacheutil.set(cache_key, files, expire=600)
        t.stop()
        xutils.log("list recent edit %s" % t.cost())

        t.start()
        groups = xutils.call("note.list_group")
        t.stop()
        xutils.log("list group %s" % t.cost())

        count = db.count(where, vars=dict(creator=xauth.get_current_name()))
        return xtemplate.render("note/view.html",
                                html_title="最近更新",
                                pathlist=[
                                    Storage(name="最近更新",
                                            type="group",
                                            url="/file/recent_edit")
                                ],
                                file_type="group",
                                files=files,
                                file=Storage(name="最近更新", type="group"),
                                page=page,
                                show_notice=True,
                                page_max=math.ceil(count / PAGE_SIZE),
                                groups=groups,
                                show_mdate=True,
                                show_groups=True,
                                page_url="/file/recent_edit?page=")
Example #14
0
 def request_url(task):
     url = task.url
     if url is None: url = ""
     quoted_url = xutils.quote_unicode(url)
     if quoted_url.startswith(("http://", "https://")):
         # 处理外部HTTP请求
         response = xutils.urlopen(quoted_url).read()
         xutils.log("Request %r success" % quoted_url)
         return response
     elif url.startswith("script://"):
         name = url[len("script://"):]
         return xutils.exec_script(name, False)
     cookie = xauth.get_user_cookie("admin")
     url = url + "?content=" + xutils.quote_unicode(str(task.message))
     return self.app.request(url, headers=dict(COOKIE=cookie))
Example #15
0
    def POST(self):
        part_file = True
        chunksize = 5 * 1024 * 1024
        chunk = xutils.get_argument("chunk", 0, type=int)
        chunks = xutils.get_argument("chunks", 1, type=int)
        file = xutils.get_argument("file", {})
        prefix = xutils.get_argument("prefix", "")
        dirname = xutils.get_argument("dirname", xconfig.DATA_DIR)
        # Fix 安全问题,不能访问上级目录
        dirname = dirname.replace("$DATA", xconfig.DATA_DIR)
        filename = None
        webpath = ""
        origin_name = ""

        if hasattr(file, "filename"):
            origin_name = file.filename
            xutils.log("recv {}", file.filename)
            filename = os.path.basename(file.filename)
            filename = xutils.quote_unicode(filename)
            if dirname == "auto":
                filepath, webpath = xutils.get_upload_file_path(
                    filename, replace_exists=True, prefix=prefix)
                dirname = os.path.dirname(filepath)
                filename = os.path.basename(filepath)

            if part_file:
                tmp_name = "%s_%d.part" % (filename, chunk)
                seek = 0
            else:
                tmp_name = filename
                seek = chunk * chunksize
            tmp_path = os.path.join(dirname, tmp_name)

            with open(tmp_path, "wb") as fp:
                fp.seek(seek)
                if seek != 0:
                    xutils.log("seek to {}", seek)
                for file_chunk in file.file:
                    fp.write(file_chunk)
        else:
            return dict(code="fail", message="require file")
        if part_file and chunk + 1 == chunks:
            self.merge_files(dirname, filename, chunks)
        return dict(code="success",
                    webpath=webpath,
                    link=get_link(origin_name, webpath))
Example #16
0
    def GET(self):
        days = xutils.get_argument("days", 30, type=int)
        page = xutils.get_argument("page", 1, type=int)
        page = max(1, page)

        t = Timer()
        t.start()
        db = xtables.get_file_table()
        where = "is_deleted = 0 AND (creator = $creator OR is_public = 1) AND type != 'group'"
        files = list(
            db.select(where=where,
                      vars=dict(creator=xauth.get_current_name()),
                      order="mtime DESC",
                      offset=(page - 1) * PAGE_SIZE,
                      limit=PAGE_SIZE))
        t.stop()
        xutils.log("list recent edit %s" % t.cost())

        t.start()
        groups = xutils.call("note.list_group")
        t.stop()
        xutils.log("list group %s" % t.cost())

        count = db.count(where, vars=dict(creator=xauth.get_current_name()))
        return xtemplate.render("note/view.html",
                                html_title="最近更新",
                                pathlist=[
                                    Storage(name="最近更新",
                                            type="group",
                                            url="/file/recent_edit")
                                ],
                                file_type="group",
                                files=files,
                                file=Storage(name="最近更新", type="group"),
                                page=page,
                                show_notice=True,
                                page_max=math.ceil(count / PAGE_SIZE),
                                groups=groups,
                                show_mdate=True,
                                show_groups=True,
                                page_url="/file/recent_edit?page=")
Example #17
0
    def do_search(self, key, offset, limit):
        global _rules

        category = xutils.get_argument("category", "")
        words = textutil.split_words(key)
        files = []

        start_time = time.time()
        ctx = SearchContext()
        ctx.input_text = key
        ctx.words = words
        ctx.category = category
        ctx.search_message = (category == "message")
        ctx.search_file_full = (category == "content")
        ctx.search_dict = (category == "dict")
        ctx.user_name = xauth.get_current_name()

        if ctx.search_message:
            ctx.search_file = False
            ctx.search_file_full = False
            ctx.search_tool = False
        if ctx.search_dict:
            ctx.search_file = False
        if ctx.search_file_full:
            ctx.search_tool = False
        if ctx.category == "book":
            ctx.search_file = False
            ctx.search_tool = False

        xutils.log("  key=%s" % key)

        xmanager.fire("search.before", ctx)
        xmanager.fire("search", ctx)

        for rule in _rules:
            pattern = rule.pattern
            func = rule.func
            # re.match内部已经实现了缓存
            m = re.match(pattern, key)
            if m:
                try:
                    start_time0 = time.time()
                    results = func(ctx, *m.groups())
                    cost_time0 = time.time() - start_time0
                    xutils.log("  >>> %s - %d ms" %
                               (func.modfunc, cost_time0 * 1000))
                    if results is not None:
                        files += results
                except Exception as e:
                    xutils.print_exc()
        cost_time = (time.time() - start_time) * 1000
        xutils.log("  === total - %d ms ===" % cost_time)
        xconfig.search_history.put(
            Storage(name="#search# %s - %d ms" % (key, cost_time),
                    category=category,
                    user=xauth.get_current_name(),
                    link=web.ctx.fullpath))

        xmanager.fire("search.after", ctx)
        return ctx.tools + files
Example #18
0
    def GET(self):
        t = Timer()
        t.start()
        sql = "SELECT * FROM file WHERE type = 'group' AND is_deleted = 0 AND creator = $creator ORDER BY name LIMIT 1000"
        data = list(xtables.get_file_table().query(
            sql, vars=dict(creator=xauth.get_current_name())))
        t.stop()
        xutils.log("group time: %s" % t.cost())

        t.start()
        ungrouped_count = xtables.get_file_table().count(
            where=
            "creator=$creator AND parent_id=0 AND is_deleted=0 AND type!='group'",
            vars=dict(creator=xauth.get_current_name()))
        t.stop()
        xutils.log("recent time: %s" % t.cost())

        tools = list(filter(tool_filter, list_tools()))[:4]
        return xtemplate.render("index.html",
                                ungrouped_count=ungrouped_count,
                                file_type="group_list",
                                files=data,
                                tools=tools)
Example #19
0
    def GET(self):
        t = Timer()
        t.start()
        groups = xutils.call("note.list_group")
        t.stop()
        xutils.log("group time: %s" % t.cost())

        notes = xutils.call("note.list_recent_edit", limit=10)
        t.start()
        ungrouped_count = xtables.get_file_table().count(
            where=
            "creator=$creator AND parent_id=0 AND is_deleted=0 AND type!='group'",
            vars=dict(creator=xauth.get_current_name()))
        t.stop()
        xutils.log("recent time: %s" % t.cost())

        tools = list(filter(tool_filter, list_tools()))[:4]
        return xtemplate.render("index.html",
                                ungrouped_count=ungrouped_count,
                                groups=groups,
                                notes=notes,
                                files=groups,
                                tools=tools)
Example #20
0
 def __exit__(self, type, value, traceback):
     import xutils
     self.stop()
     xutils.log("%s cost time: %s" % (self.name, self.cost()))