Exemple #1
0
def delete():
    ids = request.form.get('id')
    if ids is None:
        return jsonify({'msg': u'请选择要删除的文件', 'status': 0})
    ids = ids.split('##')
    infos = {}
    infos['status'] = 1
    infos['delete'] = 0
    infos['fail'] = 0
    for id in ids:
        file = items.find_one({'id': id})
        name = file['name']
        # path=file['path'].replace('/'+name,'').replace(name,'')
        if file['parent'] == '':
            path = '/'
        else:
            path = items.find_one({'id': file['parent']})['path']
        key = 'has_item$#$#$#$#{}$#$#$#$#{}'.format(path, name)
        rd.delete(key)
        status = DeleteRemoteFile(id)
        if status:
            infos['delete'] += 1
        else:
            infos['fail'] += 1
    return jsonify(infos)
Exemple #2
0
def add_pan():
    if request.method == 'POST':
        title = request.form.get('title', 'PyOne')
        pan = request.form.get('pan',
                               ''.join(random.sample(string.ascii_letters, 2)))
        order = request.form.get('order', 0, type=int)
        info = {
            "client_id": "",
            "client_secret": "",
            "share_path": "/",
            "other_name": title,
            "order": order
        }
        od_users[pan] = info
        config_path = os.path.join(config_dir, 'config.py')
        with open(config_path, 'r') as f:
            old_text = f.read()
        with open(config_path, 'w') as f:
            old_od = re.findall('od_users={[\w\W]*}', old_text)[0]
            new_od = 'od_users=' + json.dumps(
                od_users, indent=4, ensure_ascii=False)
            new_text = old_text.replace(old_od, new_od, 1)
            f.write(new_text)
        flash('添加盘符[{}]成功'.format(pan))
        key = 'users'
        rd.delete(key)
        return render_template('admin/pan_manage/add_pan.html')
    return render_template('admin/pan_manage/add_pan.html')
Exemple #3
0
def edit():
    if request.method == 'POST':
        fileid = request.form.get('fileid')
        content = request.form.get('content').encode('utf-8')
        info = {}
        token = GetToken()
        app_url = GetAppUrl()
        headers = {'Authorization': 'bearer {}'.format(token)}
        url = app_url + 'v1.0/me/drive/items/{}/content'.format(fileid)
        try:
            r = requests.put(url, headers=headers, data=content, timeout=10)
            data = json.loads(r.content)
            if data.get('@microsoft.graph.downloadUrl'):
                info['status'] = 0
                info['msg'] = '修改成功'
                rd.delete('{}:content'.format(fileid))
            else:
                info['status'] = 0
                info['msg'] = data.get('error').get('message')
        except:
            info['status'] = 0
            info['msg'] = '修改超时'
        return jsonify(info)
    fileid = request.args.get('fileid')
    name = GetName(fileid)
    ext = name.split('.')[-1]
    language = CodeType(ext)
    if language is None:
        language = 'Text'
    content = _remote_content(fileid)
    return render_template('admin/edit.html',
                           content=content,
                           fileid=fileid,
                           name=name,
                           language=language)
Exemple #4
0
def edit():
    if request.method=='POST':
        fileid=request.form.get('fileid')
        content=request.form.get('content').encode('utf-8')
        info={}
        token=GetToken()
        app_url=GetAppUrl()
        headers={'Authorization':'bearer {}'.format(token)}
        url=app_url+'v1.0/me/drive/items/{}/content'.format(fileid)
        try:
            r=requests.put(url,headers=headers,data=content,timeout=10)
            data=json.loads(r.content)
            if data.get('@microsoft.graph.downloadUrl'):
                info['status']=0
                info['msg']='修改成功'
                rd.delete('{}:content'.format(fileid))
            else:
                info['status']=0
                info['msg']=data.get('error').get('message')
        except:
            info['status']=0
            info['msg']='修改超时'
        return jsonify(info)
    fileid=request.args.get('fileid')
    name=GetName(fileid)
    ext=name.split('.')[-1]
    language=CodeType(ext)
    if language is None:
        language='Text'
    content=_remote_content(fileid)
    return render_template('admin/edit.html',content=content,fileid=fileid,name=name,language=language)
Exemple #5
0
def delete():
    ids = request.form.get('id')
    user = request.form.get('user')
    if ids is None:
        return jsonify({'msg': u'请选择要删除的文件', 'status': 0})
    ids = ids.split('##')
    infos = {}
    infos['status'] = 1
    infos['delete'] = 0
    infos['fail'] = 0
    for id in ids:
        print 'delete {}'.format(id)
        file = items.find_one({'id': id})
        name = file['name']
        path = file['path'].replace(name, '')
        if len(path.split('/')) > 2 and path.split('/')[-1] == '':
            path = path[:-1]
        key = 'has_item$#$#$#$#{}$#$#$#$#{}'.format(path, name)
        print('delete key:{}'.format(key))
        rd.delete(key)
        kc = '{}:content'.format(id)
        rd.delete(kc)
        status = DeleteRemoteFile(id, user)
        if status:
            infos['delete'] += 1
        else:
            infos['fail'] += 1
    return jsonify(infos)
Exemple #6
0
def setFile(filename=None):
    if request.method == 'POST':
        path = request.form.get('path')
        filename = request.form.get('filename')
        if not path.startswith('/'):
            path = '/' + path
        remote_file = os.path.join(path, filename)
        content = request.form.get('content').encode('utf-8')
        info = {}
        token = GetToken()
        app_url = GetAppUrl()
        headers = {'Authorization': 'bearer {}'.format(token)}
        url = app_url + 'v1.0/me/drive/items/root:{}:/content'.format(
            remote_file)
        try:
            r = requests.put(url, headers=headers, data=content, timeout=10)
            data = json.loads(r.content)
            AddResource(data)
            if data.get('@microsoft.graph.downloadUrl'):
                info['status'] = 0
                info['msg'] = '添加成功'
                key = 'has_item$#$#$#$#{}$#$#$#$#{}'.format(path, name)
                rd.delete(key)
            else:
                info['status'] = 0
                info['msg'] = data.get('error').get('message')
        except:
            info['status'] = 0
            info['msg'] = '超时'
        return jsonify(info)
    path = urllib.unquote(request.args.get('path'))
    _, fid, i = has_item(path, filename)
    if fid != False:
        return redirect(url_for('admin.edit', fileid=fid))
    return render_template('admin/setpass.html', path=path, filename=filename)
Exemple #7
0
def delete():
    ids = request.form.get('id')
    user = request.form.get('user')
    if ids is None:
        return jsonify({'msg': u'请选择要删除的文件', 'status': 0})
    ids = ids.split('##')
    infos = {}
    infos['status'] = 1
    infos['delete'] = 0
    infos['fail'] = 0
    for id in ids:
        print 'delete {}'.format(id)
        file = items.find_one({'id': id})
        name = file['name']
        # path=file['path'].replace('/'+name,'').replace(name,'')
        if file['parent'] == '':
            path = '/'
        else:
            path = items.find_one({'id': file['parent']})['path']
        if not path.startswith('/'):
            path = '/' + path
        path = '{}:{}'.format(user, path)
        key = 'has_item$#$#$#$#{}$#$#$#$#{}'.format(path, name)
        rd.delete(key)
        kc = '{}:content'.format(id)
        rd.delete(kc)
        status = DeleteRemoteFile(id, user)
        if status:
            infos['delete'] += 1
        else:
            infos['fail'] += 1
    return jsonify(infos)
Exemple #8
0
def edit():
    if request.method == 'POST':
        fileid = request.form.get('fileid')
        user = request.form.get('user')
        content = request.form.get('content').encode('utf-8')
        info = {}
        token = GetToken(user=user)
        app_url = GetAppUrl()
        headers = {'Authorization': 'bearer {}'.format(token)}
        url = app_url + 'v1.0/me/drive/items/{}/content'.format(fileid)
        try:
            r = requests.put(url, headers=headers, data=content, timeout=10)
            data = json.loads(r.content)
            if data.get('id'):
                info['status'] = 0
                info['msg'] = '修改成功'
                rd.delete('{}:content'.format(fileid))
                file = items.find_one({'id': fileid})
                name = file['name']
                path = file['path'].replace(name, '', 1)
                if len(path.split('/')) > 2 and path.split('/')[-1] == '':
                    path = path[:-1]
                # if path=='':
                #     path='/'
                # if not path.startswith('/'):
                #     path='/'+path
                # path='{}:{}'.format(user,path)
                key = 'has_item$#$#$#$#{}$#$#$#$#{}'.format(path, name)
                print('edit key:{}'.format(key))
                rd.delete(key)
            else:
                info['status'] = 0
                info['msg'] = data.get('error').get('message')
        except Exception as e:
            print e
            info['status'] = 0
            info['msg'] = '修改超时'
        return jsonify(info)
    fileid = request.args.get('fileid')
    user = request.args.get('user')
    name = GetName(fileid)
    ext = name.split('.')[-1]
    language = CodeType(ext)
    if language is None:
        language = 'Text'
    content = _remote_content(fileid, user)
    return render_template('admin/setFile/edit.html',
                           content=content,
                           fileid=fileid,
                           name=name,
                           language=language,
                           cur_user=user)
Exemple #9
0
def setFile(filename=None):
    if request.method == 'POST':
        path = request.form.get('path')
        if path.split(':')[-1] == '':
            path = path.split(':')[0] + ':/'
        user, n_path = path.split(':')
        filename = request.form.get('filename')
        if not n_path.startswith('/'):
            n_path = '/' + n_path
        share_path = od_users.get(user).get('share_path')
        if share_path != '/':
            remote_file = os.path.join(os.path.join(share_path, n_path[1:]),
                                       filename)
        else:
            remote_file = os.path.join(n_path, filename)
        print(u'remote path:{}'.format(remote_file))
        content = request.form.get('content').encode('utf-8')
        info = {}
        token = GetToken(user=user)
        app_url = GetAppUrl()
        headers = {'Authorization': 'bearer {}'.format(token)}
        url = app_url + 'v1.0/me/drive/items/root:{}:/content'.format(
            remote_file)
        try:
            r = requests.put(url, headers=headers, data=content, timeout=10)
            data = json.loads(r.content)
            if data.get('id'):
                AddResource(data, user)
                info['status'] = 0
                info['msg'] = '添加成功'
                key = 'has_item$#$#$#$#{}$#$#$#$#{}'.format(path, filename)
                print('set key:{}'.format(key))
                rd.delete(key)
            else:
                info['status'] = 0
                info['msg'] = data.get('error').get('message')
        except Exception as e:
            info['status'] = 0
            info['msg'] = '超时'
        return jsonify(info)
    path = urllib.unquote(request.args.get('path'))
    if path.split(':')[-1] == '':
        path = path.split(':')[0] + ':/'
    user, n_path = path.split(':')
    _, fid, i = has_item(path, filename)
    if fid != False:
        return redirect(url_for('admin.edit', fileid=fid, user=user))
    return render_template('admin/setpass.html',
                           path=path,
                           filename=filename,
                           cur_user=user)
Exemple #10
0
def rm_pan():
    if request.method=='POST':
        pan=request.form.get('user')
        od_users.pop(pan)
        config_path=os.path.join(config_dir,'config.py')
        with open(config_path,'r') as f:
            old_text=f.read()
        with open(config_path,'w') as f:
            old_od=re.findall('od_users={[\w\W]*}',old_text)[0]
            new_od='od_users='+json.dumps(od_users,indent=4,ensure_ascii=False)
            new_text=old_text.replace(old_od,new_od,1)
            f.write(new_text)
        key='users'
        rd.delete(key)
        items.delete_many({'user':pan})
        data=dict(msg='删除盘符[{}]成功'.format(pan),status=1)
        return jsonify(data)
    return render_template('admin/rm_pan.html')