示例#1
0
    def post(cls):
        '''Upload user submission'''
        try:
            user_mail = get_jwt_identity()

            # check current submission counts
            daily_counts = FileModel.get_interval_upload_count_by_mail(
                email=user_mail, AOEtime=get_AOE_today(to_str=False))
            weekly_counts = FileModel.get_interval_upload_count_by_mail(
                email=user_mail, AOEtime=get_AOE_week(to_str=False))
            if (daily_counts >= configs["DAILY_SUBMIT_LIMIT"]) or (weekly_counts >= configs["WEEKLY_SUBMIT_LIMIT"]):
                return {"message": f"Exceed Submission Limit: You have submitted {daily_counts} times today and {weekly_counts} times this week."}, HTTPStatus.FORBIDDEN

            # file validation
            file = request.files['file']

            if file.filename == "":
                return {"message": "No file selected."}, HTTPStatus.FORBIDDEN
            if not file_upload.zipfile_check(file):
                return {"message": "Wrong file format."}, HTTPStatus.FORBIDDEN

            # load form data
            formData = publicFormSchema.load(request.form)

            # get file path
            upload_count = FileModel.get_upload_count_by_mail(
                email=user_mail) + 1
            folder = file_upload.create_folder(user_mail, str(upload_count))
            file_path = file_upload.get_full_path(folder, file.filename)

            # add column for db
            formData.update({"email": user_mail, "filePath": file_path})

            fileObj = FileModel(**formData)
            scoreObj = ScoreModel()
            fileObj.scores.append(scoreObj)
            fileObj.save_to_db()
            try:
                file.save(file_path)

                # start processing
                thread = Thread(target=metric_calculate_pipeline, kwargs={"file_path": file_path,
                                                                          "submitUUID": formData["submitUUID"]})
                thread.start()

                return {"message": "Upload successfully!"}, HTTPStatus.OK
            except Exception as e:
                fileObj.delete_from_db()  # Rollback
                return {"message": "Internal Server Error!"}, HTTPStatus.INTERNAL_SERVER_ERROR
        except ValidationError as e:
            return {"message": "There's something worng with your input!"}, HTTPStatus.BAD_REQUEST
        except Exception as e:
            print(e)
            return {"message": "Internal Server Error!"}, HTTPStatus.INTERNAL_SERVER_ERROR
示例#2
0
    def post(cls):
        data = _datafile_parser.parse_args()

        current_user_id = get_jwt_identity()
        user = UserModel.find_by_id(current_user_id)

        links = list()
        total_size = 0

        # "files in data[] contains a list/array of files object/dict which contains file name and size
        for file_obj in json.loads(data["files"]):
            file = FileModel.find_by_name(name=file_obj["name"],
                                          user_id=current_user_id)

            if file:
                return {"msg": "A file with that name already exists!"}, 403

            if user.files_size + file_obj["size"] >= configs.allowed_size[
                    user.account_type]:
                return {"msg": "Not enough space to upload!"}, 403

            # links is a list of upload link, so it is appended with link for each file
            links.append(
                s3_storage.get_upload_url(file_obj["name"], current_user_id,
                                          data["file_path"]))

            file = FileModel(file_obj["name"], current_user_id,
                             data["file_path"], file_obj["size"])
            file.save_to_db()

            total_size += file_obj["size"]

        # updating the total size of files the user currently has
        user.increment_files_size(total_size)

        return {"links": links}, 200