예제 #1
0
async def social_api_post_flipLikeStatus(request, postId=None, likeId=None):
    """
    反转点赞的状态。当 likeId 为空时,根据 postId 和 用户 cookie 创建新的已经点赞的信息;likeId 不为空,根据相关的对象反转点赞的信息
    :param request: 请求
    :param postId: 帖子Id
    :param likeId: 点赞的Id
    :return:
    """
    username = get_loggined_username_or_denied(request)
    dbm = request.__dbManager__
    if not likeId:
        # 新建新的信息
        if not postId:
            raise ValueError('请指定 postId')
        likedObj = models.Like(likId=next_id(),
                               username=username,
                               postId=postId,
                               liked=False)
        await dbm.insert(likedObj)
    else:
        # 查询已经存在的(建议加上用户名验证等信息)
        likedObj = await dbm.query(models.Like, likeId=likeId)
    # 反转
    if not likedObj:
        raise ValueError('没有指定的帖子点赞对象')
    likedObj.liked = not likedObj.liked
    # 保存到数据库
    await dbm.update(likedObj)
    return likedObj
예제 #2
0
async def social_api_post_insertComment(request,
                                        postId,
                                        comment,
                                        replyUsername=None):
    """
    插入一个帖子中的评论
    :param request:
    :param postId: 帖子 ID
    :param comment: 评论的内容(不超过200字符)
    :param replyUsername: 回复的用户名
    :return:
    """
    username = get_loggined_username_or_denied(request)
    dbm = request.__dbManager__
    # 检查评论是否存在
    postObj = await dbm.query(models.Post, postId=postId)
    if not postObj:
        return {'status': 1}
    # 新建评论
    aComment = models.Comment(commentId=next_id(),
                              username=username,
                              comment=comment,
                              postId=postId,
                              to_username=replyUsername)
    await dbm.insert(aComment)
    return {'status': 0, 'comment': aComment}
예제 #3
0
async def store_upload_images(request):
    """
    存储用户上传的文件,可以上传对各文件
    :param request: 是一个用户的请求
    :param typeName: 指定的文件类型
    :return: 返回所有上传的文件的Id. 对应的文件可以在指定的目录下找到
    """
    if not request.__user__:
        return 403
    else:
        username = request.__user__.username
    reader = await request.multipart()
    file = await reader.next()
    res = []
    while file:
        # ext = str.lower(file.filename[file.filename.rfind('.'):])
        imageId = next_id()  # 不再处理格式
        save_prefix = getImageFilePath(ImageSource.Upload, imageId)
        # 保存为完整的文件流
        # with open(target, 'wb') as f:
        #     while True:
        #         chunk = await file.read_chunk()
        #         if not chunk:
        #             break
        #         f.write(chunk)
        imageFileStream = BytesIO()  # 载入内存
        while True:
            chunk = await file.read_chunk()
            if not chunk:
                break
            imageFileStream.write(chunk)
        imageFileStream.seek(0)
        # 创建 Image
        sImg = Image.open(imageFileStream)
        w, h = sImg.size
        # 检查大小信息
        if w > UploadImageMaxWidth or h > UploadImageMaxHeight:
            # 需要按比例缩放
            ratio = float(w) / h
            newSize = (int(UploadImageMaxWidth * ratio),
                       int(UploadImageMaxHeight * ratio))
            sImg = sImg.resize(newSize, Image.ANTIALIAS)
            logging.debug('转换图片:(%d, %d) -> (%d, %d)' %
                          (w, h, newSize[0], newSize[1]))
        # 格式转换
        if sImg.mode != 'RGB':
            sImg = sImg.convert('RGB')
            logging.debug('转换图片格式到RGB')
        # 保存到文件
        imageId = imageId + '.jpg'
        sImg.save(save_prefix + '.jpg', quality=95)
        res.append(imageId)
        file = await reader.next()
    # 存储到数据库中
    dbm = request.__dbManager__
    # 上传的都是新的
    for imageId in res:
        obj = models.UploadedImage(username=username, imageId=imageId)
        await dbm.insert(obj)
    return {'imagesIds': res}
예제 #4
0
async def process_styled_image(request, originalImageId, styledModel,
                               originalImageIndex):

    model = styledModel  # 定义的模型
    imageId = originalImageId  # 任何图片的Id。如果存在就不进行特殊化,直接返回指定的文件的ImageId
    _originalImageIndex = originalImageIndex
    returnData = {
        'originalImageId': imageId,
        'originalImageIndex': _originalImageIndex,
        'styledModel': None,
        'messages': '%s 不存在' % imageId,
        'imageId': None,
        'status': 1,
        'cached': False,
        'postId': None  # 关联的 postId. 只有 cached == true 才会设置
    }
    if not request.__user__:
        returnData['messages'] = '没有指定进行风格化操作的用户名'
        return returnData
    else:
        username = request.__user__.username
    try:
        model_code = methods.StyleModel[model]
    except KeyError:
        returnData['messages'] = '未知的风格化模型参数:%s' % model
        return returnData

    try:
        originalImageIndex = ImageSource(int(_originalImageIndex))
    except ValueError:
        returnData['messages'] = '图像来源类型错误:%s' % _originalImageIndex
        return returnData

    # 从数据库中读取
    dbm = request.__dbManager__
    # 检查是否存在
    mayExists = await getTargetImageByIdAndSource(dbm, imageId,
                                                  originalImageIndex,
                                                  'styledModel', model,
                                                  models.StyledImage)
    if len(mayExists) > 0:  # 保证只会存在一个
        returnData['messages'] = '使用已经创建的风格化图片'
        returnData['styledModel'] = model
        returnData['imageId'] = mayExists[0].imageId
        returnData['status'] = 0
        returnData['cached'] = True
        # 检查是否有关联的 postID
        returnData['postId'] = await get_postId(dbm, mayExists[0].imageId)
        return returnData
    # 获取图片源的位置
    originalImgFile = getImageFilePath(originalImageIndex,
                                       imageid=imageId)  # 原始图片的类型
    originalImgObj = None
    if originalImageIndex == ImageSource.Upload:
        originalImgObj = await dbm.query(models.UploadedImage, imageId=imageId)
    elif originalImageIndex == ImageSource.Colorized:
        originalImgObj = await dbm.query(models.ColorizedImage,
                                         imageId=imageId)
    else:
        originalImgObj = await dbm.query(models.StyledImage, imageId=imageId)
    # 检查是否存在原文件
    if not os.path.exists(originalImgFile) or not originalImgObj:
        returnData['messages'] = '指定的图片ID对应的文件或记录不存在:%s' % imageId
        return returnData
    # 风格
    ext = imageId[imageId.rfind('.'):]
    styledImageId = next_id() + ext
    styledImageFile = getImageFilePath(ImageSource.Styled, styledImageId)
    res = methods.styleChanger(originalImgFile,
                               styledImageFile,
                               model=model_code)
    if not res:
        returnData['messages'] = '没有生成指定的风格化图片:%s' % styledImageFile
        return returnData
    # 存入数据库
    await dbm.insert(models.StyledImage,
                     imageId=styledImageId,
                     originalImageId=imageId,
                     styledModel=model,
                     username=username,
                     imageSourceIndex=originalImageIndex.value)
    # styledImagesItem = await dbm.query(models.StyledImage, uploadImageId=imageId, styledModel=model)
    #
    # # 目标文件
    # if not styledImagesItem or not os.path.exists(targetFile):
    #     # 还没有渲染过
    #     if not os.path.exists(targetFile):
    #         methods.styleChanger(getImageFilePath(_upload_dir, imageId),
    #                              targetFile, model=model_code)
    # # 存入数据库
    # if not styledImagesItem:
    #     await dbm.insert(models.StyledImage, uploadImageId=imageId, styledModel=model)
    returnData['messages'] = '成功风格化图像'
    returnData['styledModel'] = model
    returnData['imageId'] = styledImageId
    returnData['status'] = 0
    return returnData
예제 #5
0
async def process_colorized_image(request, originalImageId, colorizedModel,
                                  originalImageIndex):

    model = colorizedModel  # 定义的模型
    imageId = originalImageId  # 任何图片的Id。如果存在就不进行特殊化,直接返回指定的文件的ImageId
    returnData = {
        'originalImageId': imageId,
        'originalImageIndex': originalImageIndex,
        'colorizedModel': None,
        'messages': '%s 不存在' % imageId,
        'imageId': None,
        'status': 1,
        'cached': False,
        'postId': None
    }
    if not request.__user__:
        returnData['messages'] = '没有指定进行上色操作的用户名'
        return returnData
    else:
        username = request.__user__.username
    # 检查彩色化的模型
    try:
        model_code = methods.ColorizeModel[model]
    except KeyError:
        returnData['messages'] = '未知的彩色化模型参数:%s' % model
        return returnData

    try:
        originalImageIndex = ImageSource(int(originalImageIndex))
    except ValueError:
        returnData['messages'] = '图像来源类型错误:%s' % originalImageIndex
        return returnData
    # 从数据库中读取
    dbm = request.__dbManager__
    # 检查是否存在
    mayExists = await getTargetImageByIdAndSource(dbm, imageId,
                                                  originalImageIndex,
                                                  'colorizedModel', model,
                                                  models.ColorizedImage)
    if len(mayExists) > 0:  # 保证只会存在一个
        returnData['messages'] = '使用已经创建的彩色化图片'
        returnData['colorizedModel'] = model
        returnData['imageId'] = mayExists[0].imageId
        returnData['status'] = 0
        returnData['cached'] = True
        # 检查是否有关联的 postID
        returnData['postId'] = await get_postId(dbm, mayExists[0].imageId)
        return returnData
    # 获取图片源的位置
    originalImgFile = getImageFilePath(originalImageIndex,
                                       imageid=imageId)  # 原始图片的类型
    originalImgObj = None
    if originalImageIndex == ImageSource.Upload:
        originalImgObj = await dbm.query(models.UploadedImage, imageId=imageId)
    elif originalImageIndex == ImageSource.Styled:
        originalImgObj = await dbm.query(models.StyledImage, imageId=imageId)
    else:
        originalImgObj = await dbm.query(models.ColorizedImage,
                                         imageId=imageId)
    # 检查是否存在原文件
    if not os.path.exists(originalImgFile) or not originalImgObj:
        returnData['messages'] = '指定的图片ID对应的文件或记录不存在:%s' % imageId
        return returnData
    # 风格
    ext = imageId[imageId.rfind('.'):]
    colorizedImageId = next_id() + ext
    colorizedImageFile = getImageFilePath(ImageSource.Colorized,
                                          colorizedImageId)
    res = methods.colorizer(originalImgFile,
                            colorizedImageFile,
                            imageModelEnum=model_code)
    if not res:
        returnData['messages'] = '没有生成指定的彩色化图片:%s' % colorizedImageFile
        return returnData
    # 存入数据库
    await dbm.insert(models.ColorizedImage,
                     imageId=colorizedImageId,
                     originalImageId=imageId,
                     colorizedModel=model,
                     username=username,
                     imageSourceIndex=originalImageIndex.value)
    returnData['messages'] = '成功彩色化图像'
    returnData['colorizedModel'] = model
    returnData['imageId'] = colorizedImageId
    returnData['status'] = 0
    return returnData