コード例 #1
0
ファイル: file_api.py プロジェクト: adoggie/cloudfish
def delete_file(request):
    """
	"""
    cr = webapi.SuccCallReturn()
    try:

        if request.method != 'DELETE':
            return webapi.FailCallReturn(
                lemon.errors.ErrorDefs.ParameterIllegal).httpResponse()

        file_id = webapi.GET(request, 'id')
        rs = core.AppUserFile.objects.filter(user__id=request.user.get('id'),
                                             access_id=file_id)
        if not rs:
            return webapi.FailCallReturn(
                lemon.errors.ErrorDefs.ObjectNotExisted)
        auf = rs[0]

        storage_id = auf.storage_id
        r = storage_delete_file(request, storage_id)
        if r:
            auf.delete()

        log = core.LogAppUserAction(
            user=request.user.get('user_id'),
            user_role=request.user.get('node_type'),
            issue_time=datetime.datetime.now(),
            action=cloudfish.base.AppUserActionType.CLIENT_USER_DELETE_FILE,
            target=auf.file_name,
            detail=u'file:%s size:%s' % (auf.file_name, auf.file_size))
        log.save()
    except:
        traceback.print_exc()
        cr = webapi.FailCallReturn(lemon.errors.ErrorDefs.InternalException)
    return cr.httpResponse()
コード例 #2
0
ファイル: file_api.py プロジェクト: adoggie/cloudfish
def share_file(request):
    """
	"""
    cr = webapi.SuccCallReturn()
    try:

        file_id = webapi.GET(request, 'id')
        client_id = webapi.GET(request, 'client_id')

        rs = core.AppUser.objects.filter(name=client_id,
                                         app__id=request.user.get('app_id'))
        if not rs:
            return webapi.FailCallReturn(
                lemon.errors.ErrorDefs.ObjectNotExisted).httpResponse()
        target_user = rs[0]

        if core.AppUserFile.objects.filter(user__id=target_user.id,
                                           access_id=file_id).count():
            return cr.httpResponse()  # succ ,passed

        rs = core.AppUserFile.objects.filter(user__id=request.user.get('id'),
                                             access_id=file_id)
        if not rs:
            return webapi.FailCallReturn(
                lemon.errors.ErrorDefs.ObjectNotExisted).httpResponse()
        auf = rs[0]

        log = core.LogAppUserAction(
            user=request.user.get('user_id'),
            user_role=request.user.get('node_type'),
            issue_time=datetime.datetime.now(),
            action=cloudfish.base.AppUserActionType.CLIENT_USER_CREATE_FILE,
            target=auf.file_name,
            detail=u'share to: %s ,file:%s size:%s' %
            (target_user.name, auf.file_name, auf.file_size))
        log.save()

        # allocate new instance
        auf.pk = None
        auf.user = target_user
        auf.save()
    except:
        traceback.print_exc()
        cr = webapi.FailCallReturn(lemon.errors.ErrorDefs.InternalException)
    return cr.httpResponse()
コード例 #3
0
ファイル: file_api.py プロジェクト: adoggie/cloudfish
def get_file(request):
    """
	"""
    cr = webapi.SuccCallReturn()
    try:
        file_id = webapi.GET(request, 'id')
        rs = core.AppUserFile.objects.filter(user__id=request.user.get('id'),
                                             access_id=file_id)
        if not rs:
            return webapi.FailCallReturn(
                lemon.errors.ErrorDefs.ObjectNotExisted).httpResponse()
        auf = rs[0]

        filename = storage_get_file(request, auf.storage_id)

        passwd = get_encrypt_password(request.user.get('id'))

        enable = BaseAppServer.instance().getConfig().get(
            'encrypt_enable', False)
        if enable:
            filename = decrypt_file(filename, passwd)

        log = core.LogAppUserAction(
            user=request.user.get('user_id'),
            user_role=request.user.get('node_type'),
            issue_time=datetime.datetime.now(),
            action=cloudfish.base.AppUserActionType.CLIENT_USER_GET_FILE,
            target=auf.file_name,
            detail=u'file:%s size:%s' % (auf.file_name, auf.file_size))
        log.save()

        # fp = FileObject(filename,'rb')
        fp = open(filename, 'rb')
        wrapper = FileWrapper(fp)
        content_type = mimetypes.guess_type(auf.file_name)[0]
        response = StreamingHttpResponse(wrapper, content_type=content_type)
        #response['Content-Disposition'] = u"attachment; filename=%s" % auf.file_name
        response['Content-Disposition'] = u"attachment; filename=_unkown_file_"
        return response
    except:
        traceback.print_exc()
        # return webapi.FailCallReturn( lemon.errors.ErrorDefs.InternalException).httpResponse()
    return HttpResponseForbidden()
コード例 #4
0
def userLogout(request):
    """
	服务器发送用户登出
	:param request:
	:return:
	"""
    cr = webapi.SuccCallReturn()
    try:
        user = core.AppUser.objects.get(id=request.user.get('id'))
        # 日志记录
        log = core.LogAppUserAction(
            user=user.name,
            user_role=request.user.get('node_type'),
            issue_time=datetime.datetime.now(),
            action=cloudfish.base.AppUserActionType.CLIENT_USER_LOGOUT,
            target=None,
        )
        log.save()
    except:
        traceback.print_exc()
        cr = webapi.FailCallReturn(lemon.errors.ErrorDefs.InternalException,
                                   traceback.format_exc())
    return cr.httpResponse()
コード例 #5
0
ファイル: file_api.py プロジェクト: adoggie/cloudfish
def create_file(request):
    """
		load file
		encrypt file
		save to swift
		return storage_id
	"""
    cr = webapi.SuccCallReturn()
    try:
        tmpdir = BaseAppServer.instance().getConfig().get("tmpdir")
        for name, file in request.FILES.items():
            upload_filename = file.name
            filename = lemon.utils.misc.genUUID()
            filename = u'%s/%s' % (tmpdir, filename.decode('utf-8'))

            # fw = FileObject(open(filename,'wb'),'wb')

            fw = open(filename, 'wb')

            file_size = 0
            # c = file.read()
            for chunk in file.chunks():
                file_size += len(chunk)
                fw.write(chunk)
            fw.close()

            digest = calc_file_digest(filename)

            passwd = get_encrypt_password(request.user.get('id'))
            enable = BaseAppServer.instance().getConfig().get(
                'encrypt_enable', False)
            if enable:
                filename = encrypt_file(filename, passwd)
            if not filename:
                return webapi.FailCallReturn(
                    lemon.errors.ErrorDefs.InternalException,
                    u'encrypt file failed')

            storage_id = storage_post_file(request, filename)

            user = core.AppUser.objects.get(id=request.user.get('id'))
            auf = core.AppUserFile()
            auf.user = user
            auf.create_time = datetime.datetime.now()
            auf.storage_id = storage_id
            auf.file_name = upload_filename
            auf.file_size = file_size
            auf.digest = digest
            auf.access_salt = new_salt()
            auf.access_id = get_user_file_access_id(auf)
            auf.save()

            cr.assign(auf.access_id)

            log = core.LogAppUserAction(
                user=request.user.get('user_id'),
                user_role=request.user.get('node_type'),
                issue_time=datetime.datetime.now(),
                action=cloudfish.base.AppUserActionType.
                CLIENT_USER_CREATE_FILE,
                target=auf.file_name,
                detail=u'file:%s size:%s' % (auf.file_name, auf.file_size))
            log.save()
            break
    except:
        traceback.print_exc()
        cr = webapi.FailCallReturn(lemon.errors.ErrorDefs.InternalException)
    return cr.httpResponse()
コード例 #6
0
def userLogin(request):
    """
	服务器发送用户登录
	:param request:
		access_token
		secret_key
		user_id
		is_root - 是否是server发起的登录
		timeout - token超时时间(hour),默认有效时间: 24hours

	:return:
	"""

    cr = webapi.SuccCallReturn()
    username = webapi.GET(request, 'username')

    access_token = webapi.GET(request, 'access_token')
    secret_key = webapi.GET(request, 'secret_key')

    is_root = webapi.GET(request, 'is_root', 0)
    timeout = webapi.GET(request, 'timeout', 24)

    if not access_token:
        access_token = webapi.GET(request, 'app_key')
    if not secret_key:
        secret_key = webapi.GET(request, 'app_secret')
    if not username:
        username = webapi.GET(request, 'client_id')

    try:
        user = None
        username = username.strip()
        is_root = int(is_root)
        timeout = int(timeout)

        if timeout == 0:
            timeout = 0xffff

        if not len(username):
            return webapi.FailCallReturn(
                lemon.errors.ErrorDefs.ParameterIllegal).httpResponse()

        rs = core.Application.objects.filter(access_token=access_token,
                                             secret_key=secret_key)
        if not rs:
            return webapi.FailCallReturn(
                lemon.errors.ErrorDefs.ObjectNotExisted).httpResponse()
        app = rs[0]

        rs = core.AppUser.objects.filter(name=username)
        if not rs:
            #用户不存在,将插入新的应用用户记录
            user = core.AppUser(app=app,
                                name=username,
                                last_login_time=datetime.datetime.now(),
                                create_time=datetime.datetime.now(),
                                status=cloudfish.base.STATUS_TYPE_ENABLED,
                                storage_id=normalize_user_storage_id(username))
            user.save()
        else:
            user = rs[0]

        usertype = cloudfish.base.ACCESSOR_TYPE_NORMAL
        if is_root:
            usertype = cloudfish.base.ACCESSOR_TYPE_ROOT

        user_dict = {
            'id': user.id,
            'user_id': username,
            'app_id': app.id,
            'app_sid': app.app_id,
            'app_name': app.name,
            'node_type': usertype,
            'create_time': int(time.time()),
            'expire_time': int(time.time()) + 3600 * timeout,
            'app_container': app.container_name,
            'user_storage_id': get_user_storage_id(user)
        }

        token = Token.encryptUserToken(user_dict)
        result = token

        cr.assign(result)
        user.token = token
        user.last_login_time = datetime.datetime.now()
        user.save()

        log = core.LogAppUserAction(
            user=user.name,
            user_role=usertype,
            token=token,
            issue_time=datetime.datetime.now(),
            action=cloudfish.base.AppUserActionType.CLIENT_USER_LOGIN,
            target=None,
        )
        log.save()
    except:
        traceback.print_exc()
        cr = webapi.FailCallReturn(lemon.errors.ErrorDefs.InternalException,
                                   traceback.format_exc())
    return cr.httpResponse()