예제 #1
0
    def save(self, commit=True):
        bookfile = self.cleaned_data['bookfile']

        # calc md5_obj
        md5_obj = hashlib.md5()
        for chunk in bookfile.chunks():
            md5_obj.update(chunk)
        md5 = md5_obj.hexdigest()

        obj = super(BookFileCreationForm, self).save(commit=False)
        obj.md5 = md5
        # size, set to 1 if less than 1
        obj.size_kb = bookfile.size / 1024 or 1

        # raw name and ext
        raw_filename = bookfile.name
        basename, ext = os.path.splitext(raw_filename)

        obj.filenames = basename
        obj.ext = ext

        if not obj.title:
            obj.title = basename

        # upload to qiniu
        obj.qiniu_key = obj.gen_qiniu_key()
        # TODO: upload async. use celery
        upload_file(bookfile, obj.qiniu_key)

        if commit:
            obj.save()
        return obj
예제 #2
0
    def upload_avatar(self, db: Session, response: Response,
                      decode_token: dict, file: File):
        """
        上传头像
        :param decode_token:    解析之后的 token
        :param file:            头像
        :return:                用户信息, 头像地址, 提示信息
        """
        # ----------- 验证头像是否上传成功 ----------- #
        type = 1  # 类型(0=其他 ,1=图片,2=视频, 3=音频, 4=文档)
        db_file_path, title, message = uploads.upload_file(file=file,
                                                           type=type)
        if not db_file_path:
            logger.error(message)
            return None, None, message

        # ----------- 将用户头像路径存入用户表 ----------- #
        token_userid = decode_token.get("sub")
        db_user_obj = crud_user.update_avatar(db,
                                              id=token_userid,
                                              avatar=db_file_path)
        if not db_user_obj:
            message = "头像上传失败"
            response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
            logger.error(message)
            return None, None, message
        message = "头像上传成功"
        response.status_code = status.HTTP_200_OK
        avatar_url = f"{settings.FILE_BASEURL}{db_file_path}"
        return db_user_obj, avatar_url, message
예제 #3
0
    def upload_cover(self, db: Session, response: Response, *, id: int,
                     file: File):
        """
        上传/更新 封面
        :param id:          用户id
        :param file:        头像
        :return:            文件信息, 提示信息
        """
        db_file = crud_file.get_file_by_id(db, id=id)
        if not db_file:
            message = "文件不存在"
            response.status_code = status.HTTP_404_NOT_FOUND
            return None, message

        # ----------- 验证封面是否上传成功 ----------- #
        type = 1  # 类型(0=其他 ,1=图片,2=视频, 3=音频, 4=文档)
        db_file_path, title, message = uploads.upload_file(file=file,
                                                           type=type)
        if not db_file_path:
            response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
            logger.error(message)
            return None, message

        # ----------- 更新文件封面 ----------- #
        db_file_obj = crud_file.upload_cover(db, id=id, cover=db_file_path)
        if not db_file_obj:
            message = "封面上传失败"
            response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
            logger.error(message)
            return None, None, message
        message = "封面上传成功"
        response.status_code = status.HTTP_200_OK
        return db_file_obj, message
예제 #4
0
    def post(self):
        upload = upload_file()

        if upload["status"] == "error":
            return jsonify({"data": upload}), 422

        return jsonify({"data": upload})
예제 #5
0
    def post(self):
        upload = upload_file(upload_type="image")

        if upload["status"] == "error":
            return jsonify({"data": upload}), 422

        uploaded_image = PillowImage.open(upload["data"]["file"])

        image = Image(
            provider=upload["data"]["provider"],
            bucket=upload["data"]["bucket"],
            path=upload["data"]["path"],
            width=uploaded_image.size[0],
            height=uploaded_image.size[1],
        )
        db.session.add(image)
        db.session.commit()

        return jsonify(
            {"data": {"filename": upload["data"]["original_filename"], "image": image}}
        )
예제 #6
0
    def upload_file(self, db: Session, response: Response, *,
                    decode_token: dict, file: File, type: int):
        """
        上传文件
        :param decode_token:    解析之后的token
        :param type:            文件类型
        :param file:            上传的文件
        :param type:            类型(0=其他 ,1=图片,2=视频, 3=音频, 4=文档)
        :return:                文件信息, 文件完整路径, 提示信息
        """
        # ----------- 验证文件是否上传成功 ----------- #
        db_file_path, title, message = uploads.upload_file(file=file,
                                                           type=type)
        if not db_file_path:
            response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
            logger.error(message)
            return None, None, message

        # ------------ 将数据存入数据库 ------------- #
        dict_obj_in = {
            "userid": decode_token.get("sub"),
            "path": db_file_path,
            "title": title,
            "type": type,
            "belong": 0  # belong(String): 类型下的子类属性 0=未分类
        }
        db_file = crud_file.add_file(db, obj_in=dict_obj_in)
        if not db_file:
            message = "上传失败"
            response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
            logger.error(message)
            return None, None, message
        message = "上传成功"
        response.status_code = status.HTTP_201_CREATED
        file_url = f"{settings.FILE_BASEURL}{db_file_path}"
        return db_file, file_url, message
예제 #7
0
    def post(self):
        upload = upload_file(upload_type="image")

        if upload["status"] == "error":
            return jsonify({"data": upload}), 422

        uploaded_image = PillowImage.open(upload["data"]["file"])

        image = Image(
            provider=upload["data"]["provider"],
            bucket=upload["data"]["bucket"],
            path=upload["data"]["path"],
            width=uploaded_image.size[0],
            height=uploaded_image.size[1],
        )
        db.session.add(image)
        db.session.commit()

        return jsonify({
            "data": {
                "filename": upload["data"]["original_filename"],
                "image": image
            }
        })