Exemplo n.º 1
0
    def authorize(self, code):
        """
        require token from Microsoft api by code
        :param code:
        :return:
        """
        url = self.oauth_url + "/token"
        headers = {
            'user-agent': 'User-Agent:Mozilla/5.0 (Windows NT 6.1) \
                AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        payload = {
            'client_id': helpers.config('client_id'),
            'client_secret': helpers.config('client_secret'),
            'redirect_uri': helpers.config('redirect_url'),
            'code': code,
            'grant_type': 'authorization_code'
        }
        resp = json.loads(requests.post(url, payload, headers=headers).text)
        if resp.get('error'):
            raise Exception(resp.get('error_description'))

        resp['is_install'] = True
        helpers.batch_store_config(resp)
        return resp
Exemplo n.º 2
0
    def get_token(self):
        """
        Get access_token by refresh_token
        :return:
        """

        params = {
            'client_id': helpers.config('client_id'),
            'redirect_uri': helpers.config('redirect_url'),
            'client_secret': helpers.config('client_secret'),
            'refresh_token': helpers.config('refresh_token'),
            'grant_type': 'refresh_token'
        }

        headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
        }

        url = self.oauth_url + '/token'

        resp = requests.post(url, data=params, headers=headers).text
        resp = json.loads(resp)

        if resp.get('error'):
            raise Exception(resp.get('error_description'))
        helpers.batch_store_config(resp)

        return None
Exemplo n.º 3
0
 def get_authorize_url(self):
     """
     Generate authorize url
     :return:
     """
     client_id = helpers.config('client_id')
     redirect_url = helpers.config('redirect_url')
     path = "/authorize?client_id=%s&scope=%s&response_type=code&redirect_uri=%s"
     return self.oauth_url + path % (client_id, self.scope, redirect_url)
Exemplo n.º 4
0
    def upload_small_file(self, local_path: str, remote_path: str):
        file_name = helpers.get_file_name(local_path)
        print('----------------START TO UPLOAD FILE [ %s ]-------------------------' % file_name)

        remote_path = self.prepare_remote_path(remote_path, file_name)
        access_token = helpers.config('access_token')

        if not access_token:
            raise Exception('Access token does not exists.Please refresh token.')

        headers = {
            'Authorization': 'bearer ' + access_token,
            'Content-Type': 'application/json'
        }

        with open(local_path, 'rb') as f:
            query = 'content'
            url = self.api_url + '/me/drive/root' + remote_path + query
            resp = requests.put(
                url,
                headers=headers,
                data=f
            ).text

        resp = json.loads(resp)
        if resp.get('error'):
            raise Exception(resp.get('error'))

        # return resp
        return 'FILE [ %s ] UPLOADED.' % file_name
Exemplo n.º 5
0
 def __call__(self, request):
     response = self.get_response(request)
     # Code will be execute here.
     if not helpers.config(
             'is_install') and request.path not in self.except_uris:
         return HttpResponseRedirect('admin/install')
     return response
Exemplo n.º 6
0
def install_2(request, code):
    """
    Go authorize then writen in configure file
    :param request:
    :param code:
    :return:
    """
    one = OneDrive()
    one.authorize(code)
    http_referrer = request.get_raw_uri().split(':', -1)[0] + '://'
    host = http_referrer + request.get_host()
    data = {
        'host': host,
        'password': helpers.config('password'),
        'site_name': helpers.config('site_name'),
    }
    return render(request, 'install/success.html', data)
Exemplo n.º 7
0
    def __call__(self, request):
        response = self.get_response(request)
        access_code = helpers.config('access_code')
        if request.path in self.scope_uris and access_code:
            auth_info = request.session.get('auth_info')
            if not auth_info or not helpers.compare_web_token(auth_info):
                return HttpResponseRedirect('/login')

        return response
Exemplo n.º 8
0
def login(request):
    """
    Login Page(if set access code)
    :param request:
    :return:
    """

    if request.method == 'POST':
        access_token = helpers.config('access_code')
        if request.POST.get('access_code') != access_token:
            return HttpResponseRedirect('/login')
        else:
            token, timestamp = helpers.generate_token(val=access_token)
            request.session['auth_info'] = {'token': token, 'time': timestamp}
            return HttpResponseRedirect('/')
    data = {'site_name': helpers.config('site_name')}

    return render(request, 'home/login.html', data)
Exemplo n.º 9
0
def index(request):
    if request.POST:
        helpers.batch_store_config({
            'site_name':
            request.POST.get('site_name', 'ERO DRIVE'),
            'password':
            request.POST.get('password'),
            'access_code':
            request.POST.get('access_code'),
        })

    configure = {
        'site_name': helpers.config('site_name'),
        'password': helpers.config('password'),
        'access_code': helpers.config('access_code'),
        'access_token': helpers.config('access_token'),
    }
    return render(request, 'adm/index.html', configure)
Exemplo n.º 10
0
def login(request):
    """
    Login page & method
    :param request:
    :return:
    """
    if request.POST:
        c_password = str(request.POST.get('password'))
        if c_password != str(helpers.config('password')):
            return HttpResponseRedirect('/admin/login')

        token, timestamp = helpers.generate_token(val=c_password)
        request.session['admin_auth_info'] = {
            'token': token,
            'time': timestamp
        }
        return HttpResponseRedirect('/admin')

    data = {'site_name': helpers.config('site_name')}
    return render(request, 'adm/login.html', data)
Exemplo n.º 11
0
def detail(request):
    """
    Detail page to show the file type can show/play on website.
    :param request:
    :return:
    """
    file_path = request.GET.get('file_path', '/')
    file_name = request.GET.get('file_name')
    suffix = file_name.split('.').pop()
    suffix_cat = None
    if not file_name:
        return HttpResponseRedirect('/')
    one = OneDrive()
    file = one.get_item(file_path + '/' + file_name)
    file.update({'downloadUrl': file.get('@microsoft.graph.downloadUrl')})
    if not file:
        return HttpResponseRedirect('/')

    show_configure = eval(helpers.config('show'))
    for conf in show_configure:
        if suffix in conf['suffix']:
            suffix_cat = conf['category']

    data = {
        'file': file,
        'suffix_cat': suffix_cat,
        'site_name': helpers.config('site_name'),
        'file_name': file_name,
    }
    if suffix_cat == 'stream':
        data.update({
            'text':
            requests.get(file['downloadUrl']).text.encode('ascii', 'ignore')
        })

    if not suffix_cat:
        return HttpResponseRedirect('/?path=' + file_path)

    return render(request, 'home/detail/' + suffix_cat + '.html', data)
Exemplo n.º 12
0
def index(request):
    """
    List page in frontend
    :param request:
    :return:
    """
    one = OneDrive()
    path = request.GET.get('path', '/')
    item_list = helpers.list_format(one.get_list(path)['value'])

    data = {
        'item_list': item_list,
        'curr_path': '' if path == '/' else urllib.parse.quote(path),
        'path_list': helpers.path_format(path),
        'site_name': helpers.config('site_name'),
    }

    return render(request, 'home/index.html', data)
Exemplo n.º 13
0
def install(request):
    """
    Installation headquarter function
    :param request:
    :return:
    """
    # be careful data-type is string from request query string
    step = request.GET.get('step', '0')
    code = request.GET.get('code', None)

    if helpers.config(key='is_install', default=None):
        return HttpResponseRedirect('/')

    if code:
        return install_2(request, code)

    func_name = 'install_' + step
    return globals()[func_name](request)
Exemplo n.º 14
0
    def create_upload_session(self, local_path: str, remote_path: str):
        file_name = helpers.get_file_name(local_path)
        remote_path = self.prepare_remote_path(remote_path, file_name)
        access_token = helpers.config('access_token')

        if not access_token:
            raise Exception('Access token does not exists.Please refresh token.')

        headers = {
            'Authorization': 'bearer ' + access_token,
            'Content-Type': 'application/json'
        }

        query = 'createUploadSession'
        url = self.api_url + '/me/drive/root' + remote_path + query
        # No param required in this api.
        upload_session = requests.post(url=url, headers=headers).text

        return json.loads(upload_session)
Exemplo n.º 15
0
 def get_access_token():
     access_token = helpers.config('access_token')
     if not access_token:
         raise Exception('Access token does not exists.Please refresh token.')
     return access_token