def download_task(request, task_name, gid): '''print(task_name) print(gid)''' if Control.check_session(request): usr = Control.get_username_by_session(request) try: y = Database.find_single("task", { "username": usr, "taskname": task_name, "gid": gid }) req = Aria.get_task_state(usr, y) if (req['state'] != 'complete'): return PageResponse.not_completed() filedir = Aria.get_filename(usr, task_name, gid) if (len(filedir) == 1): return PageResponse.file_download(filedir[0]["path"]) else: dirname = Const.DownloadRoot + 'aria2cdownload/' + usr + '/' os.system('tar zcvf %s.tar.gz -C %s %s' % (dirname + 'task_' + task_name, dirname, 'task_' + task_name)) return PageResponse.file_download(dirname + 'task_' + task_name + '.tar.gz') except: return PageResponse.download_tool_error() else: return PageResponse.session_failed(request)
def get_tasks_state(request): if request.is_ajax(): import json if Control.check_session(request): usr = Control.get_username_by_session(request) x = Database.find_single("account", {"username": usr}) old = x[Database.id("account", "memoryused")] memory = Control.check_memoryuse(request) if (memory >= MemoryLimit) and (old < MemoryLimit): return JsonResponse.memory_limit_exceeded() req_all = [] data = json.loads(request.POST['data']) fromurl = request.POST['from'] needreturn = {} for x in data: needreturn[x["task_name"]] = x["workid"] y = Database.get_all("task", {"username": usr}) download_cnt = 0 overall_cnt = len(y) for x in y: req = Aria.get_task_state(usr, x) if (req['state'] == 'active'): download_cnt += 1 if (re.match(r'^/index/tasks/completed/?$', fromurl)) and ( req['state'] == 'complete') and not (needreturn.__contains__( x[Database.id("task", "taskname")])): return JsonResponse.need_refresh() if (needreturn.__contains__(x[Database.id("task", "taskname")])): '''print(fromurl)''' if (re.match(r'^/index/tasks/downloading/?$', fromurl)) and (req['state'] != 'active'): return JsonResponse.need_refresh() if (needreturn.__contains__(x[Database.id("task", "taskname")])): req['workid'] = needreturn[x[Database.id( "task", "taskname")]] if (x[Database.id("task", "attr")] == 0): req['attr'] = "temporary" else: req['attr'] = "persistent" req_all.append(req) '''print(req_all)''' return JsonResponse.get_tasks_state(req_all, download_cnt, overall_cnt, memory) else: return JsonResponse.session_failed() else: return PageResponse.jump_to_not_exist()
def index(request, para1 = ""): '''print(para1)''' if Control.check_session(request): import re if re.match(r'^new(|/(newurl|newbt|newmeta))/?$', para1): return newpage(request, para1) elif re.match(r'^(|tasks(/(all|downloading|completed))?)/?$', para1): return taskpage(request, para1) else: return PageResponse.jump_to_not_exist() else: return PageResponse.session_failed(request)
def login(request): if Control.check_session(request): return PageResponse.jump_to_index() if (request.method == "POST"): try: assert request.POST.__contains__("username") assert request.POST.__contains__("password") return PageResponse.login_page(request, request.POST["username"], request.POST["password"]) except: return PageResponse.login_page(request, "", "") return PageResponse.login_page(request, "", "")
def optask(request, method): import json if request.is_ajax(): if Control.check_session(request): print(method) op = [] if (method == 'delete'): op = ['aria2.forcePause', 'aria2.forceRemove'] elif (method == 'pause'): op = ['aria2.forcePause'] elif (method == 'continue'): if (Control.check_memoryuse(request) >= Const.MemoryLimit): return JsonResponse.memory_limit_exceeded() else: op = ['aria2.unpause'] elif (method == 'switch'): op = [] usr = Control.get_username_by_session(request) y = json.loads(request.POST['jsonData']) for x in y: for xop in op: try: Aria.operate(usr, x["task_name"], x["gid"], xop) except: print('NOT ERROR') if (method == 'delete'): Control.remove_task(usr, x["task_name"], x["gid"]) if (method == 'switch'): thisone = Database.find_single( "task", { "username": usr, "gid": x["gid"], "taskname": x["task_name"] }) Database.update( "task", { "username": usr, "gid": x["gid"], "taskname": x["task_name"] }, {"attr": 1 - thisone[Database.id("task", "attr")]}) return JsonResponse.operation_success() else: return JsonResponse.session_failed() else: return PageResponse.jump_to_not_exist()
def newtask(request, para1): if request.is_ajax(): if Control.check_session(request): usr = Control.get_username_by_session(request) y = Database.get_all("task", {"username": usr}) if (Control.check_memoryuse(request) == -1): return JsonResponse.memory_limit_exceeded() if len(y) > Const.TaskNumberLimit: assert 0 elif len(y) == Const.TaskNumberLimit: return JsonResponse.task_number_exceeded() for x in y: if (x[Database.id("task", "taskname")] == request.POST["task_name"]): return JsonResponse.task_name_repeated() try: import base64 if (para1 == 'bt'): btfile = request.FILES.get("task_link", None) if not btfile: return JsonResponse.upload_btfile_empty() if (btfile.multiple_chunks()): return JsonResponse.upload_btfile_toolarge() else: bt = base64.b64encode(btfile.read()).decode("utf-8") c = Aria.add_other_task(usr, request.POST["task_name"], bt, "aria2.addTorrent") elif (para1 == 'meta'): metalink = base64.b64decode(request.POST["task_link"]) c = Aria.add_other_task(usr, request.POST["task_name"], metalink, "aria2.addMetalink") else: c = Aria.add_url_task(usr, request.POST["task_name"], request.POST["task_link"]) import time as Time Database.insert("task", {"username": usr, "gid": c["result"], "taskname": str(request.POST["task_name"]), "createtime": Time.strftime('%Y-%m-%d', Time.localtime(Time.time())), "attr": 0}) return JsonResponse.operation_success() except: return JsonResponse.download_tool_error() else: return JsonResponse.session_failed() else: return PageResponse.jump_to_not_exist()
def check_login(request): if Control.check_session(request): return PageResponse.jump_to_index() if (request.method == "POST"): try: from Download9.getmd5 import getmd5 pwd = getmd5(request.POST["password"]) x = Database.find_single("account", {"username": request.POST["username"]}) assert x[Database.id("account", "password")] == pwd request.session["MemberName"] = request.POST["username"] request.session.set_expiry(Const.LoginTime) return PageResponse.login_success(request) except: return PageResponse.login_failed(request, request.POST["username"], request.POST["password"]) else: return PageResponse.jump_to_not_exist()
def get_overall_state(request): if request.is_ajax(): import json if Control.check_session(request): usr = Control.get_username_by_session(request) x = Database.find_single("account", {"username": usr}) old = x[Database.id("account", "memoryused")] memory = Control.check_memoryuse(request) if (memory >= MemoryLimit) and (old < MemoryLimit): return JsonResponse.memory_limit_exceeded() y = Database.get_all("task", {"username": usr}) overall_cnt = len(y) return JsonResponse.get_overall_state(overall_cnt, memory) else: return JsonResponse.session_failed() else: return PageResponse.jump_to_not_exist()
def account9_login(request): if Control.check_session(request): return PageResponse.jump_to_index() if (request.method == "GET"): ''' printf('') printf('a9test_begin') ''' assert request.GET.__contains__("code") '''printf('a9test_start_get_info')''' ret = getinfo(request.GET["code"]) '''printf('a9test_end_get_info')''' try: '''printf('a9test_try_read_old_account')''' Database.find_single( "account", { "username": ret["user"]["name"], "password": ret["user"]["password"] }) '''printf('a9test_start_setting_session')''' request.session["MemberName"] = ret["user"]["name"] request.session.set_expiry(Const.LoginTime) '''printf('a9test_auth_over')''' return PageResponse.login_success(request) except: '''printf('a9test_try_inser_new_account')''' Database.insert( "account", { "username": ret["user"]["name"], "password": ret["user"]["password"], "memoryused": 0 }) '''printf('a9test_start_setting_session')''' request.session["MemberName"] = ret["user"]["name"] request.session.set_expiry(Const.LoginTime) '''printf('a9test_auth_over')''' return PageResponse.login_success(request) else: '''printf('a9test_end:get_session failed')''' return PageResponse.jump_to_not_exist()